Jul 09 2021 04:03 AM
Jul 09 2021 04:26 AM
Does calling it like this work?:
$abcPath = {
c:\abc\runprocess.cmd -create
}
invoke-command -computername abc -scriptblock $abcPath
Jul 09 2021 04:44 AM
Thanks for your reply @psophos but issue is not resolved. Still same behavior - basically it's not displaying anything.
Jul 09 2021 05:15 AM
Jul 09 2021 05:19 AM
Jul 09 2021 05:28 AM
Jul 09 2021 05:34 AM
$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.
Jul 09 2021 06:24 AM
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.
Jul 09 2021 03:59 PM
Jul 09 2021 05:12 PM
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.
Jul 10 2021 05:26 AM
Dec 13 2021 12:33 PM
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
Thank you in advance
Dec 14 2021 03:51 AM
@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
Dec 14 2021 08:57 AM
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.