我有几千台计算机将安全事件日志备份到服务器的共享。环境非常动态,因此需要将其自动化。调用具有多个参数和foreach循环的PowerShell Start-Job cmdlet是如此关闭
我一直在努力创建一个散列的脚本,其中每个键是一个序列,每个键的值是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}")
向脚本块的结束,显示屏将显示从主机名的列表中最后一个主机名。
如何编写脚本块,以便它将遍历脚本块中的主机名数组以处理每个作业中的每个元素?
谢谢;这根据需要工作并解决我的问题。 – GaryG