Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running batch file on Remote Computers using PowerShell 2.0

Tags:

powershell

I am trying to run a exe on remote machines which would basically uninstall a product agent. below is the code:

$test = Get-Content PC.txt

foreach ($a in $test)
{
   $curr = Get-Location
   Set-Location \\$a\Admin$\System32\CCMSetup
   .\ccmsetup.exe /uninstall
   Set-Location $curr
}

It doesn't work. I ended up removing the program from the host computer itself :)

Alternate Option: I created a batch file with the command line:

cd C:\Windows\System32\ccmsetup
ccmsetup /uninstall
exit

It seems the above can also be achieved using Invoke-Command.

Invoke-Command -ComputerName $client -FilePath UninstallCCM.cmd

Apparently, it does not accept batch file. I would like to keep it as simple as possible.

Currently I am using PSExec for installing and uninstalling the program. Do I need to enable PS Remoting (WinRM) on every remote machine on whom I need to execute scripts using PowerShell? Can someone please help? Thanks in advance.

like image 262
Rajiv Avatar asked Dec 11 '22 17:12

Rajiv


1 Answers

This command should execute successfully:

Invoke-Command -ComputerName $client -ScriptBlock { cd C:\Windows\System32\ccmsetup; ccmsetup /uninstall} -Credential $(Get-Credential) -Authentication CredSSP

but you will need to enable CredSSP authentication on all machines by running these two commands on each machine:

Enable-WsManCredSSP -Role Server -Force
Enable-WSManCredSSP -Role Client -DelegateComputer * -Force
like image 107
Musaab Al-Okaidi Avatar answered Feb 25 '23 05:02

Musaab Al-Okaidi