Forum Discussion
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.
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 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7.1
It is a pretty complex wee beastie.
- ADumithDec 14, 2021Iron Contributor
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.xmlOffice 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.
- ADumithDec 13, 2021Iron Contributor
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 -WaitBut, nothing

Thank you in advance
- psophosJul 10, 2021Brass ContributorYes.
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. - SunPowerJul 10, 2021Copper Contributor
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.
- SunPowerJul 09, 2021Copper ContributorThe 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