0

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

Ruben_PH
  • 1,692
  • 7
  • 25
  • 42
  • If I had to guess, `InlineScript` is starting a new environment, but I'd have to check the doc. [See: the document](https://learn.microsoft.com/en-us/powershell/module/psworkflow/about/about_inlinescript?view=powershell-5.1) – Maximilian Burszley Mar 30 '19 at 02:42
  • https://stackoverflow.com/questions/52228537/invoke-command-path-null – Maximilian Burszley Mar 30 '19 at 02:46

0 Answers0