且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

计划任务的PowerShell脚本与字符串数组参数

更新时间:2023-02-03 09:16:40

尝试使用 - 命令开关代替 - 文件开关,然后使用调用操作符'和;。这里是与预定任务这样的一个例子的链接:

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

是这样的:

-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"

与结果:1

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

与结果:2