Search code examples
powershellpowershell-2.0powershell-remoting

powershell script - different behavior after start with windows scheduler


I have this script to check if there is mapped disk on remote server. When I run it in command line, it works perfectly, from PowerGUI and ISE it works perfectly, but when I shedule it in Windows task scheduler, I get a mail message (mail sending part of script is not included), that disk is not mapped - the "else" is executed in spite of disk is mapped.

if(Invoke-Command -ComputerName sdebt -ScriptBlock { Get-WmiObject win32_logicaldisk -ComputerName sdebt -Filter "DeviceID = 'L:'"}) {
        Write-Host -ForegroundColor Green "L: is OK"} else {
                Write-Host -ForegroundColor Magenta "L: is NOT OK" 
                $subject = "CHYBA: Disk L is not mapped"
                $body += "Disk L is not mapped `r" }

Thank you.


Solution

  • Probably is related with permissions.

    • When you run it from your commandline/ISE/PowerGUI you are using your credentials

    • When you run it from schedule task by default you run it with system credentials

    If I were you I would:

    1. Try running it with your credentials to see if this make a difference.
    2. Try running it with highest privileges

    If after 1) and 2) still fails at least you know for sure that is not a permission issue :-)