1

My task is to connect to other VM and execute certain task,.... with task parameters to be passed. i used Invoke-Expression for that,..... now when i pass static path directly in invoke-expression working fine but when I assign same to varible and appending to invoke-expression. it's not working

I tried by giving static path it's working fine and executing batch on remote vm, problem is when path is dynamic

  Static is working fine has shown below:

  $server="slc10XXX.XX.XXXX.com"
  $username="XXXXXXX"
  $password="XXXXXX@321"
  $pass = ConvertTo-SecureString -AsPlainText $password -Force
  $cred = New-Object System.Management.Automation.PSCredential - 
  ArgumentList $username, $pass
  $path="cmd.exe /c C:\Task6-MASSUOW\chay.bat"
  Invoke-Command -ComputerName $server -credential $cred -ErrorAction Stop 
  -ScriptBlock {Invoke-Expression -Command:"cmd.exe /c C:\Task6- 
  MASSUOW\chay.bat"}

  Getting issue in below code when i assign $path:

  $server="slc10XXX.XX.XXXX.com"
  $username="XXXXXXX"
  $password="XXXXXX@321"
  $pass = ConvertTo-SecureString -AsPlainText $password -Force
  $cred = New-Object System.Management.Automation.PSCredential - 
  ArgumentList $username, $pass
  $path="cmd.exe /c C:\Task6-MASSUOW\chay.bat"
  Invoke-Command -ComputerName $server -credential $cred -ErrorAction Stop 
  -ScriptBlock {Invoke-Expression -Command:"$path"}

PowerShell.exe : Cannot bind argument to parameter 'Command' because it is null. At line:1 char:1 + PowerShell.exe -ExecutionPolicy Bypass -File 'C:\Users\chaj\Documents ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (Cannot bind arg...use it is null.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError At C:\Users\chaj\Documents\String\Chat1.ps1:8 char:1 + Invoke-Command -ComputerName $server -credential $cred -ErrorAction S ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpre ssionCommand + PSComputerName : slc10XXXX.XX.XXXX.com

Prince Vegeta
  • 719
  • 6
  • 21
  • As an aside: [`Invoke-Expression` should generally be avoided](https://blogs.msdn.microsoft.com/powershell/2011/06/03/invoke-expression-considered-harmful/); definitely [don't use it to invoke an external program](https://stackoverflow.com/a/57966347/45375). – mklement0 Nov 07 '19 at 12:39

1 Answers1

1

That's because "$path" is sent as a string literal to the remote server. The expression will be evaluated there, but $path was not defined in the remote session. That's why it's null.

You can do this for example:

Invoke-Command -ComputerName $server -Credential $cred -ErrorAction Stop -ScriptBlock {
   param($path)
   Invoke-Expression -Command:$path
} -ArgumentList "cmd.exe /c C:\Task6-MASSUOW\chay.bat"

Or simpler:

Invoke-Command -Script { & $Args[0] } -Arg "C:\Task6-MASSUOW\chay.bat" -Computer $server -Cred $cred -EA Stop
mklement0
  • 382,024
  • 64
  • 607
  • 775
marsze
  • 15,079
  • 5
  • 45
  • 61