我创建了一个可以从 Management Shell 完美运行的 PowerShell 脚本。我正在尝试将其设置为在 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"
Result:
Additional Directories to be included:
D:/Documents/PowerShell Scripts
D:/SomeFolder
从计划任务:
Action: 启动一个程序
程序/脚本: PowerShell.exe
论据: -文件“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 运行时它是完全有效的。