5

我创建了一个 PowerShell 脚本,可以从 Management Shell 完美运行。我正在尝试将其设置为在 Windows Server 2008 R2 中的计划任务中工作,并且不确定如何为我的字符串数组参数传递参数。

这是我的脚本的相关部分:

[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
    }
}

给麻烦的参数是最后一个,$AdditionalDirectories.

从壳牌:

如果我像这样从 Management Shell 运行脚本,它会完美运行:

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

结果:

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

从计划任务:

行动: 启动程序

程序/脚本: PowerShell.exe

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

结果:(来自日志文件)

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

我已经为这些参数尝试了几种不同的方法,但我似乎无法让它们被视为字符串数组中的 2 个单独的字符串。我现在正在对它们进行硬编码,但似乎必须有一种方法来完成这项工作,因为它在从 shell 运行时完全有效。

4

3 回答 3

6

尝试使用 -Command 开关而不是 -File 开关,然后使用调用运算符“&”。这是一个使用计划任务执行此操作的示例的链接:

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

我通过创建包含以下内容的脚本来测试此解决方案:

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

然后通过以下两种方式调用它:

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

结果:1

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

结果:2

于 2012-10-02T14:52:43.057 回答
2

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 eg:

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

诚然,这不是其他人完全可以阅读/理解的东西。:-) 您必须在计划任务的描述中记录命令。

于 2012-10-02T15:12:26.777 回答
1

我一直在为 SharePoint 使用 PowerShell,并创建了几个计划任务以在不同的时间间隔触发它们。

我就是这样做的,它一直对我有用。

语法:[您的脚本的路径] -Param1Name 'Value1' -Param2Name 'Value2' -Param3Name 'Value3'

这是一个真实的例子:

D:\Scripts\Global.ps1 -DataLocation 'D:\Scripts' -DeploymentParameters 'Deploymentparameters' -Deployments 'Deployments' -GlobalParameters 'GlobalParameters' -SiteUrl ' https://my.sp.company.com '

于 2014-03-11T15:38:00.550 回答