powershellimport-csv

Why is import-csv failing when cmd runs manually?


So I have this very simple script that I am using invoke-command to execute a cmd line on a bunch of remote nodes. The csv has 2 columns, host & stub. The csv has no whitespaces or anything.

import-csv test.csv | foreach-object {
    $host = $_.host
    $stub = $_.stub

    icm -computername $host -scriptblock {cmd /c c:\path\to\executable.exe <executable switches>  $stub  }
}

where $stub is part of the input for the executable.

If I run this verbatim with values for $host & $stub from the PS prompt, it works, but if I import-csv it throws Winrm errors

What am I missing here?

Errors from script but not from running it manually from PS prompt

[system.management.automation.internal.host.internalhost] Connecting to remote server failed with the following error m
essage : WinRM cannot process the request. The following error occured while using Kerberos authentication: The network
 path was not found.
 Possible causes are:
  -The user name or password specified are invalid.
  -Kerberos is used when no authentication method and no user name are specified.
  -Kerberos accepts domain user names, but not local user names.
  -The Service Principal Name (SPN) for the remote computer name and port does not exist.
  -The client and remote computers are in different domains and there is no trust between the two domains.
 After checking for the above issues, try the following:
  -Check the Event Viewer for events related to authentication.
  -Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or us
e HTTPS transport.
 Note that computers in the TrustedHosts list might not be authenticated.
   -For more information about WinRM configuration, run the following command: winrm help config. For more information,
 see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (:) [], PSRemotingTransportException
    + FullyQualifiedErrorId : PSSessionStateBroken
Cannot overwrite variable Host because it is read-only or constant.
At C:\temp\bulk_rereg.ps1:2 char:6
+ $host <<<<  = $_.host
    + CategoryInfo          : WriteError: (Host:String) [], SessionStateUnauthorizedAccessException
    + FullyQualifiedErrorId : VariableNotWritable

Solution

  • $Stub is a local variable. It does not exist in the remote machine.

    To use a remote variable (Refrence Document)

    Option 1: "use:" You can reference a remote variable without specific delcaration by utilizing the "use:" method.

    import-csv test.csv | foreach-object {
    $host = $_.host
    $stub = $_.stub
    
    icm -computername $host -scriptblock {cmd /c c:\path\to\executable.exe <executable switches>  $using:stub  }
    }
    

    Option 2: ArgumentList parameter Use the ArgumentList parameter and declaration of the passed variable

    import-csv test.csv | foreach-object {
    $host = $_.host
    $stub = $_.stub
    
    icm -computername $host -scriptblock {
    param($stub2)
    cmd /c c:\path\to\executable.exe <executable switches>  $stub2  } -ArgumentList $stub
    }
    

    This is acts like a function. The Argument list arguments are the passed variable and the Param(variable) is how the variable name in the remote machine will be called

    ($stub is passing the value to $stub2) The name can be the same for easier use.