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