I have a Powershell script below that runs a scheduled task on multiple remote servers:
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
The problem is it isn't reading the value of the $Server variable
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]
The script works if I remove the ForEach and hard code each of the server name like so:
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
The server_list.txt has a valid content and I can output it when running write-host $Server
inside the ForEach command