【发布时间】:2018-01-31 07:35:58
【问题描述】:
我有几千台计算机将安全事件日志备份到服务器的共享中。环境非常动态,因此需要自动化。
我一直在编写一个创建哈希的脚本,其中每个键是一个序列,每个键的值是 N 台计算机。我将键和值传递给另一个脚本,该脚本将运行 n 个作业来备份日志; n 取决于我可以在每个作业中包含多少台机器,并且仍然可以有效地处理备份。
脚本 1 有这个块:
foreach ($key in ($arrayAll.Keys | Sort-Object)) {
Job-EvtLog.ps1 $key @($data)
}
脚本 2 有:
Param(
[Parameter[Mandatory=$true, ValueFromPipeline=$true)]
[string[]] $Key,
[Parameter[Mandatory=$true, ValueFromPipeline=$true)]
[Array[]] $Computers
)
function job_process($key) {
#...stuff...including casting the @($Computers) to the array: $MyComputers
$jobCommand = [ScriptBlock]::Create("foreach(`$d in $MyComputers) {Add-Content -Path $somewhere -Value `$d}")
Start-Job -Name $key $jobCommand -Args $somewhere $MyComputers
}
我正在通过尝试将计算机数组写入文件来测试这一点,因此Add-Content。
我显然在创建脚本块时做错了。 Get-Job | %{$_.Command} 显示:
foreach ($d in my_List_of_Hostnames) {Add-Content -Path myCorrectpath -Value $d}
没有向 myCorrectPath 写入任何内容。
如果我写:
... -Value `$d}")
在脚本块的末尾,显示屏会显示主机名列表中的最后一个主机名。
如何编写脚本块,使其遍历脚本块中的主机名数组以处理一个作业中的每个元素?
【问题讨论】:
标签: powershell start-job scriptblock