Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've created a PowerShell script that runs perfectly from the Management Shell. I'm trying to get it setup to work in a scheduled task in Windows Server 2008 R2 and am unsure how to pass the parameters for my string array parameter.

Here is the relevant portion of my script:

[CmdletBinding()]
param(
    [Parameter(Mandatory=$true)]
    [String]
        $BaseDirectory,
    [String]
        $BackupMethod = "Full",
    [Int]
        $RemoveOlderThanDays = 0,
    [String]
        $LogDirectory,
    [Int]
        $LogKeepDays = 7,
    [String[]]
        $AdditionalDirectories
)

if ($AdditionalDirectories -and $AdditionalDirectories.Count -gt 0) {
    Write-Host "  Additional Directories to be included:" -ForegroundColor Green
    $AdditionalDirectories | ForEach-Object {
        Write-Host "     $_" -foregroundcolor green
    }
}

The parameter giving the trouble is that last one, $AdditionalDirectories.

From the Shell:

If I run the script from the Management Shell like this, it works perfectly:

.\FarmBackup.ps1 \\SomeServer\Backups Full 0 D:\Logs\Backups 0 "D:\Documents\PowerShell Scripts","D:\SomeFolder"

Result:

   Additional Directories to be included:
      D:/Documents/PowerShell Scripts
      D:/SomeFolder

From Scheduled Task:

Action: Start a program

Program/script: PowerShell.exe

Arguments: -File "D:\Documents\PowerShell Scripts\FarmBackup.ps1" \\SomeServer\Backups Full 0 D:\Logs\Backups 0 "D:\Documents\PowerShell Scripts","D:\SomeFolder"

Result: (From Log File)

  Additional Directories to be included:
     D:\Documents\PowerShell Scripts,D:\SomeFolder

I've tried a couple of different methods for those parameters but I can't seem to get them to be seen as 2 separate strings in the string array. I'm hardcoding them for now, but it seems like there must be a way to make this work since it's totally valid when run from the shell.

share|improve this question

2 Answers

up vote 3 down vote accepted

Try using the -Command switch instead of the -File switch, and then use the invocation operator '&'. Here is a link to an example of doing this with scheduled tasks:

http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/12/schedule-powershell-scripts-that-require-input-values.aspx

Something like:

-Command "& 'D:\Documents\PowerShell Scripts\FarmBackup.ps1' '\\SomeServer\Backups' 'Full' 0 'D:\Logs\Backups' 0 'D:\Documents\PowerShell Scripts','D:\SomeFolder'"

I tested this solution by creating a script with the contents:

param([string[]] $x)
Write-Host $x.Count

Then called it in the following two ways:

powershell -File ".\TestScript.ps1" "what1,what2"

with result : 1

and

powershell -Command "& .\TestScript.ps1 what1,what2"

with result: 2

share|improve this answer
Worked perfectly, thanks! – theChrisKent Oct 2 '12 at 17:31

Another option, when the options get too complex and you're tired of fiddling with quotes, backticks, etc is to use the underused -EncodedCommand parameter on PowerShell.exe e.g.:

C:\PS> $cmd = "c:\temp\foo.ps1 'D:\Documents\PowerShell Scripts','D:\SomeFolder'"
C:\PS> $cmd
c:\temp\foo.ps1 'D:\Documents\PowerShell Scripts','D:\SomeFolder'
C:\PS> $bytes = [Text.Encoding]::Unicode.GetBytes($cmd)
C:\PS> $encodedCmd = [Convert]::ToBase64String($bytes)
C:\PS> $encodedCmd
YwA6AFwAdABlAG0AcABcAGYAbwBvAC4AcABzADEAIAAnAEQAOgBcAEQAbwBjAHUAbQBlAG4AdABzAFwAUABvAHcAZQByAFMAaABlAGwAbAAgAFMAYwByAGkAcAB0AHMAJwAsACcARAA6AFwAUwBvAG0AZQBGAG8AbABkAGUAcgAnAA==
C:\PS> powershell.exe -encodedCommand YwA6AFwAdABlAG0AcABcAGYAbwBvAC4AcABzADEAIAAnAEQAOgBcAEQAbwBjAHUAbQBlAG4AdABzAFwAUABvAHcAZQByAFMAaABlAGwAbAAgAFMAYwByAGkAcAB0AHMAJwAsACcARAA6AFwAUwBvAG0AZQBGAG8AbABkAGUAcgAnAA==
param1[0] is D:\Documents\PowerShell Scripts
param1[1] is D:\SomeFolder

Admittedly, not something that would be exactly readable/understandable by someone else. :-) You'd have to doc the command in the description of the scheduled task.

share|improve this answer
This is an extremely interesting solution. However, it's important that the parameters are clearly understandable for others who have to maintain the scheduled task. However, I will definitely remember this technique in the future. Thanks! – theChrisKent Oct 2 '12 at 17:33
1  
Yeah, understandable it definitely is not but occasionally "understandable" starts to look less appealing after you've banged your head too long trying to get the parameters to work. :-) – Keith Hill Oct 2 '12 at 17:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.