Invoke-Command ScriptBlock issue

Occasional Contributor
Hi All, am facing some issues when using invoke commamd.

Issue1: if I say invoke-command -computername abc -scriptblock{c:\abc\runprocess.cmd -create}

This is working fine.

But if I put c:\abc\runprocess.cmd -create in variable abcpath it’s not working as expected.

Command a I tried:

invoke-command -computername abc -scriptblock{$abcpath} this isn’t printing anything.

Command b I tried

invoke-command -computername abc -scriptblock{$args} -ArgumentList this isn’t printing c:\abc\runprocess.cmd -create as is but it’s not executing.

Please advice and any pointers will be helpful.

In summary I want to know how I can use variables in script block and such that command can be executed in machine -computerName I specified.
10 Replies

@SunPower 

Does calling it like this work?:

$abcPath = {
    c:\abc\runprocess.cmd -create
}
invoke-command -computername abc -scriptblock $abcPath 
Note the lack of { } after the -scriptblock parameter.

Thanks for your reply @psophos but issue is not resolved.  Still same behavior - basically it's not displaying anything.

Displaying or executing?

I would not expect it to display anything as the code is being executed on the remote computer.

@psophos 

 

I was using it like 

 

$result = Inovke-Command 

 

so I should be expecting result?

Hmm. If the .cmd file produces output then I would expect to see it in $result.

@psophos 

 

$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.

 



Please advice and any pointers will be helpful.

In summary I want to know how I can use variables in script block and such that command can be executed in machine -computerName I specified.

You could try either one of these 2 options:

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

The & is to execute the script.  I can't test this as I don't have the cmd file you are trying to execute. (I'm also on a linux system at present).

 

You may also want to have a read of the help for invoke-command 

It is a pretty complex wee beastie.

The term 'c:\abc\runprocess.cmd -create' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and try again.
+ CategoryInfo : ObjectNotFound: (c:\abc\runprocess.cmd -create:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : abc

@psophos 

 

Ok I think I understood the issue now - but don't know solution

 

$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}}}

 

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.

 

Yes.
You would need to copy your non-powershell script onto the remote machine first before you executed it. Or maybe store it in a shared UNC path and execute it from there.
Or maybe convert it to powershell.