I’m new to powershell, but I’m trying to output some simple logging in a ps I’m writing to create scheduled tasks. My code is below. It seems that it doesn’t throw an exception when you get an error with schtasks. Another SO question mentioned this with fileIO actions and suggested doing “-ea stop” but that doesn’t work with schtasks.
#create log file
$log = "\\servername\!incoming\Deploy\log.txt"
Clear-Content $log
#get input file list
$txtServerList = Gc "\\servername\!incoming\Deploy\serverlist.txt"
#loop through each server
ForEach($strServername In $txtServerList)
{
try
{
#install task from XML template
schtasks /create /s $strServername /tn InventoryServer /XML "\\servername\!incoming\Deploy\TaskTemplate.xml"
#run the task immediately
schtasks /run /s $strServername /tn InventoryServer
}
catch [exception]
{
Add-Content -path $log -value $strServername
#Add-Content -path $log -value $_.Exception
#Add-Content -path $log -value $_.Exception.Message
#Add-Content -path $log -value ""
}
}
I verified that ‘Add-Content -path “\servername!incoming\Deploy\log.txt” -value “test”‘works, so like I said I’m fairly sure it’s just not throwing an exception.