I'm writing up powershell scripts that runs through a bunch of scheduled tasks and attempts to delete them on Windows Server 2008. When testing the schtasks.exe command in powershell, it seems to run fine. But when I put it in my script, I get the following error: (note that I removed company names and replaced them with "Companyname")
ERROR: Invalid argument/option - 'ScheduledJobs\Companyname\0.999.1-CompanynameMacStation Delivery Manager-Test'.
Removing , Exit code: 1
This is a snippet of code from the script; $version is our software version, in this case 0.999.1, while $environment is the deployment environment, in this case Test. $server is the server we are attempting to remove the scheduled task on. Something that I find really odd is that if you look at the error above, it displays the $name we pass into the schtasks, yet when i reference it on the next line for diagnosing purposes, (the "Removing , Exit code: 1") it looks like $name is now empty. This script is part of our build template, so it is run from our build server and runs the command against a remote machine. Thoughts?
function RemoveScheduledJob($server, $name)
{
Write-Host "Deleting Scheduled job $name."
$windowsVersion = (Get-CimInstance Win32_OperatingSystem).Version
$windowsVersion = $Version.Substring(0,3)
if ([double]$windowsVersion -ge 6.2)
{
Remove2012ScheduledJob($server, $name)
}
else
{
Remove2008ScheduledJob($server, $name)
}
}
function Remove2008ScheduledJob($server, $name)
{
schtasks.exe /delete /s $server /F /tn $name
Write-Host "Removing $name, Exit code: $LASTEXITCODE"
}
$taskPath = "ScheduledJobs\CompanyName"
$taskName = "$taskPath\{0}-CompanyName Delivery Manager-{1}" -f $Version, $Environment
RemoveScheduledJob $Server $taskName