Scheduled Task for PowerShell Script with String Array Parameter

前端 未结 3 798
我在风中等你
我在风中等你 2021-01-19 02:09

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 uns

3条回答
  •  长发绾君心
    2021-01-19 02:54

    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

提交回复
热议问题