我下面有一个Powershell脚本,该脚本在多个远程服务器上运行计划的任务:
Workflow Invoke-ParallelWorkflow
{
$ListFile='server_list.txt'
$ServerList = (get-content $ListFile) -match '^[^#\r\n].*'
ForEach ($Server in $ServerList)
{
InlineScript{ Invoke-Command -ComputerName $Server -ScriptBlock { schtasks /run /tn MM\fileOSclean }}
}
}
Invoke-ParallelWorkflow
pause
问题是它没有读取$ Server变量的值
Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At Invoke-ParallelWorkflow:15 char:15
+
+ CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand
+ PSComputerName : [localhost]
如果我删除ForEach并对该服务器名称进行硬编码,则脚本将起作用:
Workflow Invoke-ParallelWorkflow
{
parallel
{
InlineScript{ Invoke-Command -ComputerName ServerName1 -ScriptBlock { schtasks /run /tn myScheduleTask }}
InlineScript{ Invoke-Command -ComputerName ServerName2 -ScriptBlock { schtasks /run /tn myScheduleTask }}
InlineScript{ Invoke-Command -ComputerName ServerName3 -ScriptBlock { schtasks /run /tn myScheduleTask }}
}
}
Invoke-ParallelWorkflow
pause
server_list.txt具有有效的内容,我可以在ForEach命令中运行write-host $Server
时将其输出