Invoke-Command to run a Powershell script on a remote computer

Hello,

I am trying to automate a process to uninstall an application on several servers. I have tried running the following commands remotely and they do not seem to work, however, when running locally it seems to run OK and I am not sure why it won’t work. PSRemoting is enabled.

$serverName = Server1, Server2, Server3

Invoke-Command -ComputerName $serverName -ScriptBlock {
    Start-Process c:\windows\temp\uninstallApp.ps1

The Powershell command it is attempting to run does the following

cd 'C:\Program Files\App\Agent\x86\'
.\FrmInst.exe /forceuninstall /silent

Any ideas where I might be going wrong?

I’d recommend to use Invoke-Command with the parameter -FilePath instead of -ScriptBlock. This way you can provide a local PowerShell script to run it on remote computers.
Please read the help completely including the examples to learn how to use it.

Hi Olaf,

Thank you very much for this, managed to change my code entirely and it is working a treat. To help others if they experience the same issue I thought I would show my code.

#Server names
$serverName = "Server1"

#Establish Session
$session = New-PSSession -ComputerName $serverName

#Uninstall McAfee
Invoke-Command -Session $session -FilePath "M:\software\uninstallApp1.ps1"

#Install SentinelOne
Invoke-Command -Session $session -FilePath "M:\software\installApp2.ps1"

I’m glad it helped. :slightly_smiling_face:
Thanks for sharing. :+1:t4: