2

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
Community
  • 1
  • 1
  • Running external programs with arguments that contain spaces can get pretty messy in PowerShell. Try defining the arguments to `schtasks.exe` as an array (`$params = '/delete', '/s', "$server", '/f', '/tn', "$name"`) and run the program like this: `& schtasks.exe $params`. – Ansgar Wiechers May 05 '15 at 21:03
  • I may make this change later on just to clean things up. – Marshall Wilson May 05 '15 at 23:04
  • possible duplicate of [How do I pass multiple parameters into a function in PowerShell?](http://stackoverflow.com/questions/4988226/how-do-i-pass-multiple-parameters-into-a-function-in-powershell) – Andrew Savinykh May 05 '15 at 23:10

1 Answers1

0

Figured this out; it's how I'm calling the functions.

I'm calling the function "Remove2008ScheduledJob" like it's a c# or javascript function, which works differently.

I'm calling it like so:

 Remove2008ScheduledJob($server, $name)

When I should be calling it like this:

Remove2008ScheduledJob $server $name

Because of this, $server basically becomes $server, whitespace followed by $name, and $name is blank. This explains why it's claiming that the $name is an invalid argument/option.