4

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.

2 Answers 2

11

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
Sign up to request clarification or add additional context in comments.

5 Comments

Is CredSSP really required?
Try it without the credential and authentication options, you might get an authentication error.
is it saying it's disabled? you'll probably need to run "winrm quickconfig" to enable it
@Rajiv - The reason this will work (once you have remoting properly set up) and your initial script does not is that your initial script does not actually execute anything on the remote host - rather, you're pointing to a UNC path and telling your local system to execute the EXE found there. Instead, you must execute the program on that remote system itself.
Yes, win was disabled. Running winrm quickconfig should do the trick.
2

I highly recommend downloading PSTools. There is a command in there called "psexec"

PSexec is so simple, you call it like this:

psexec \\myserver C:\Windows\System32\ccmsetup /uninstall

3 Comments

Yes, I am using PSExec to install and uninstall the program. I was wondering if I have to configure PowerShell Remoting (WinRM) on every remote machine that I want to execute the script from my Admin Machine.
Yes, you must enable PowerShell Remoting on every computer on you wish to execute PowerShell remotely.
Yes, you must enable it on every machine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.