Running an executable on a remote computer using Invoke-Command

I am a newbie to Powershell.

I have been trying to run an executable using the invoke-command using ScriptBlock and trying it with just -FilePath. I get the file copied to the remote computer and directory but then I get errors. I am trying to use switches with the executable /install /quiet /norestart.

I would love some help to figure out where I am going wrong.

This is my Script:
<p class=“p1”>#Variables</p>
<p class=“p2”>$computername = Get-Content servers.txt</p>
<p class=“p3”>$sourcefile = “c:\shared\install.exe”</p>
<p class=“p1”>#This section will install the software</p>
<p class=“p2”>foreach ($computer in $computername) </p>
<p class=“p4”>{</p>
<p class=“p2”> $destinationFolder = “\$computer\C$\Temp”</p>
<p class=“p1”> #It will copy $sourcefile to the $destinationfolder. If the Folder does not exist it will create it.</p>
<p class=“p2”> if (!(Test-Path -path $destinationFolder))</p>
<p class=“p4”> {</p>
<p class=“p2”> New-Item $destinationFolder -Type Directory</p>
<p class=“p4”> }</p>
<p class=“p2”> Copy-Item -Path $sourcefile -Destination $destinationFolder</p>
<p class=“p6”> Invoke-Command -ComputerName $computer -ScriptBlock {Start-Process "c:\temp\Install.exe /install /quiet /norestart "}</p>
Here is the error message. I also confirmed that the file did get copied to the remote server.
<p class=“p1”>PS C:\Users\poponova> C:\Shared\invoke3.ps1</p>
<p class=“p2”>This command cannot be run due to the error: The system cannot find the file specified.</p>
<p class=“p2”> + CategoryInfo : InvalidOperation: (:slight_smile: [Start-Process], InvalidOperationException</p>
<p class=“p2”> + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand</p>
<p class=“p2”> + PSComputerName : server1</p>
On the remote server the file is in the directory but it is not installed. I appreciate any help.

If you run: Start-Process "c:\temp\Install.exe /install /quiet /norestart " on the local machine, does it install?

Try: Invoke-Command -ComputerName $computer -ScriptBlock {Invoke-Command {‘c:\temp\Install.exe /install /quiet /norestart’}}

I think you are receiving the error because PowerShell thinks the following is a full program name or path.

"c:\temp\Install.exe /install /quiet /norestart "

Try this.

Invoke-Command -ComputerName $computer -ScriptBlock {Start-Process -FilePath 'c:\temp\Install.exe' -ArgumentList '/install /quiet /norestart'}

Yes, running Start-Process and the command locally on the remote computer does work.

When I ran your script, this is the output shown, no error, however, it did not run. The file did copy to the remote directory.
<p class=“p2”>PS C:\Users\poponova> C:\Shared\invoke3.ps1</p>
<p class=“p2”>c:\temp/install.exe /install /quiet /norestart</p>

This did not work either. The following is the error.

 
<p class=“p1”>PS C:\Users\poponova> C:\Shared\invoke3.ps1</p>
<p class=“p2”>This command cannot be run due to the error: The system cannot find the file specified.</p>
<p class=“p2”> + CategoryInfo : InvalidOperation: (:slight_smile: [Start-Process], InvalidOperationException</p>
<p class=“p2”> + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand</p>
<p class=“p2”> + PSComputerName : server1</p>

Hi,

Try this, and tell us if this will work for you :slight_smile:

Put that code inside script block of invoke-command

([WMICLASS]"\\localhost\ROOT\CIMV2:win32_process").Create("C:\app.exe /arg1 /arg2...")

thank you. This was returned. The file copied, but did not run

PSComputerName : server1
RunspaceId : 30e799f6-08fc-48b6-973d-97cc304fa453
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 2
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ProcessId : 4964
ReturnValue : 0

What do you get with this?

Invoke-Command -ComputerName $Computer -ScriptBlock {Get-Item -Path 'C:\temp\Install.exe'}

When using Invoke-Command on remote machines aren’t credentials needed?

Invoke-Command -ComputerName server01 -Credential domain01\user01 -ScriptBlock {Get-Culture}

I am still getting error with that one as well. I am sure it is a double hop error. I just can’t find my way around the problem.

I get prompted for my credentials but get an Access Denied error.

PS C:\Windows\system32> Invoke-Command -ComputerName Server01 -Credential Domain\user -ScriptBlock {Get-Culture}
[PHLWQ40A] Connecting to remote server server01 failed with the following error message : Access is denied. For more
information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (server01:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken

Try the solution provided here: https://powershell.org/forums/topic/need-to-run-exe-with-parameters-on-multiple-remote-machines/

 

This looks more like a remoting problem…

Please try this first to confirm remoting is working fine:

Enter-PSSession server01

If this works try interactively in this remote session

c:\temp\Install.exe /install /quiet /norestart

You’ve confirmed the install.exe copied over and is located in the C:\Temp folder on the remote machine?
If so, then when you run the Start-Process in the Invoke-Command you probably need to use the -Wait parameter. It’s likely the install.exe starts but is never allowed to finish because the session is closed right after the scriptblock runs.

Try this:

Invoke-Command -ComputerName $computer -ScriptBlock {
    Start-Process 'c:\temp\Install.exe' -ArgumentList '/install /quiet /norestart' -Wait
}

I could have sworn at one point I tried the -wait. I probably use the wrong parameter. It worked. Thank you so much! I spent the whole day Friday on this and should have waked away sooner so I could view with fresh eyes.

Ok, so now I am having a different issue. the command runs one at a time on each server in the server.text file. The executable that I am running can take hours to complete so I want them to run concurrently on each server in the file. I have tried commands I have found to run parallel unsuccessfully. Any suggestions? This is my script that works well one at a time.

#Variables
$computername = Get-Content c:\shared\servers.txt
$sourcefile = “c:\shared\dummy.exe”
#This section will install the software
foreach ($computer in $computername)
{
$destinationFolder = “\$computer\C$\install”
#It will copy $sourcefile to the $destinationfolder. If the Folder does not exist it will create it.

if (!(Test-Path -path $destinationFolder))
{
    New-Item $destinationFolder -Type Directory
}
Copy-Item -Path $sourcefile -Destination $destinationFolder

Invoke-Command -ComputerName $computer -ScriptBlock {
Start-Process ‘c:\install\dummy.exe’ -Wait
}

Invoke-Command has the -AsJob parameter you could try or wrap your code in the scriptblock of Start-Job.

I agree with John.

Just to illustrate:

Invoke-Command -ComputerName $computer -ScriptBlock {
Start-Process 'c:\install\dummy.exe' -Wait
} -AsJob

Then you can check the status of all jobs with:

Get-Job

As soon as all jobs are finished, retrieve the result with:

Get-Job  | Receive-Job -Keep