我创建了一个 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 运行时完全有效。