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-Contentservers.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 -TypeDirectory</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: ( [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.
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: ( [Start-Process], InvalidOperationException</p>
<p class=“p2”>+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand</p>
<p class=“p2”>+ PSComputerName: server1</p>
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
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.
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.