Invoke-Command ScriptBlock issue

$result = invoke-command -computername abc -scriptblock{c:\abc\runprocess.cmd -create}
$result

am getting expected output with above.

command a I tried.

$abcpath = "c:\abc\runprocess.cmd -create" 
$result =  invoke-command -computername abc -scriptblock{$abcpath}
$result 

this isn’t printing anything.

Command b I tried

$abcpath = "c:\abc\runprocess.cmd -create" 
$result = invoke-command -computername abc -scriptblock{$args} -ArgumentList $abcpath $result

This is printing c:\abc\runprocess.cmd -create as is but it’s not executing.

if I run

$computerNames = @("C1","C2")
$where_criteria = "NSK-Service-Ba*"
$status_to_check = "Running"
foreach ($computerName in $computerNames) {
Invoke-Command -ComputerName $computerName -ScriptBlock {Get-Service $Using:where_criteria | Where-Object{$_.status -eq $Using:status_to_check}}}

it’s working fine.

so basically to standard powershell commands if I pass variables those are working fine. but if I have to run non-powershell commands like the one’s in my example then only am noticing this behavior.

Hi nssk475,

That’s interesting. Let me see if I can try something similar in a lab and I’ll get back to you.