Invoke-Command ScriptBlock issue

Copper 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.
13 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.

@psophos 

 

Hello!

 

I have a similar situation with this script:

 

 

 

 

foreach ($Computer in $Computers)
{
Write-Host "Processing $Computer"
try
{
New-Item -ItemType directory -Path "\\$Computer\c$\temp\Office2019"
Copy-Item "\\tkdata\apps\Microsoft\Office Pro Plus 2019 x86-x64\*" "\\$Computer\c$\temp\Office2019" -Recurse

Write-Host "Installing Office Pro Plus 2019"

Invoke-Command -ComputerName $Computer -ScriptBlock { &cmd.exe /c "c:\temp\Office2019\Install-32.bat" }
}
catch [System.Exception]
{
write $_
return 'test'
}
}

 

 

The process stopped in the line 11 & return this message:

Connecting to remote server VA01509 failed with the following error message : The client cannot connect to the destination specified in the request.

 

I don't understand this, because it's a local installation, so, why?

 

Could you take a look I let me know what I doing wrong?

P.S.: I also tried with:

Start-Process -FilePath c:\temp\Office2019\Install-32.bat -ArgumentList "--quiet" -Verb RunAs -Wait

 But, nothing :sad:

 

Thank you in advance 

@ADumith 
I think you have to escape the $ in the Copy-Item statement.

Copy-Item "\\tkdata\apps\Microsoft\Office Pro Plus 2019 x86-x64\*" "\\$Computer\c`$\temp\Office2019" -Recurse

 

@Joachim Pichl 

 

Thank you for your reply.

I figure out where was the error, it was about the parameter -ComputerName, so I just to remove it

 

Invoke-Command -ScriptBlock {Start-Process -FilePath "c:\temp\Office2019\Install-32.bat" -ArgumentList "--quiet" -Verb RunAs -Wait}

But I got another issue....

 

I have built a small .bat for the Office 2019 installation and when I run it it works perfectly but when I call it through the PowerShell Script the installation does not run.

 

Any ideas?

 

This is the portion of the code

Invoke-Command -ScriptBlock {Start-Process -FilePath "c:\temp\Office2019\Install-32.bat" -ArgumentList "--quiet" -Verb RunAs -Wait}

 

.bat file

setup.exe /configure configuration-x86.xml

 

Office Config File

<Add OfficeClientEdition="32">
<Product ID="ProPlus2019Volume" PIDKEY="MYKEY">
<Language ID="en-us" />
</Product>
</Add>
<Display Level="None" AcceptEULA="TRUE"/>
<Property Name="AUTOACTIVATE" Value="1"/>
</Configuration>

 

Thank you so much for any advise.