Author Posts

December 1, 2017 at 5:24 pm

Hi experts,

I have recently learnt PS and trying to build a script which can reduce my work. Please don't mind if any silly mistake identified.

My business Requirement – There are 78 servers where I have to uninstall SCCM agents. here is the script that works on local computer.

Set-location C:\windows\CCMsetup | ccmsetup.exe /uninstall — this command uninstall SCCM agent on local computer.

However, when i run it on remote computer i get an error that ccmsetup.exe isn't recognize as cmdlet or function etc.

$session =New-PSSession -ComputerName MachineName
Invoke-Command -Session $session -ScriptBlock {Set-Location C:\Windows\ccmsetup | ccmsetup.exe /uninstall}

how can I get it working? please help.

Thanks,
Akhtar Nawaz

December 1, 2017 at 5:43 pm

C:\Windows\ccmsetup | ccmsetup.exe /uninstall

Is not technically correct.

C:\Windows\ccmsetup ; ccmsetup.exe /uninstall

Would be better. If the error persists, then you need to confirm that Ccmsetup.exe exists at that location on the remote computers. Also note that some uninstaller don't like running in a remoting session; if this turns out to be one of those uninstallers, there's really nothing you can do about it.

December 1, 2017 at 6:10 pm

Hi Don, i'm studying your book for learning PS. so glad to see your response on my first question in this forum 🙂

Thank you for correction in script. I confirm that Ccmsetup.exe file does exist on remote computer. with a little bit more effort i found someone already uninstalled SCCM via PS remoting. I made changes accordingly in my script and it worked.

$session =New-PSSession -ComputerName MachineName
Invoke-Command -Session $session -ScriptBlock {C:\windows\ccmsetup\ccmsetup.exe /uninstall }

But i still don't know why it was not working? logically I need to understand.

Thanks,
Akhtar Nawaz

December 1, 2017 at 8:17 pm

Firstly, this..

$session =New-PSSession -ComputerName MachineName
Invoke-Command -Session $session -ScriptBlock {Set-Location C:\Windows\ccmsetup | ccmsetup.exe /uninstall}
... is not correct.

Do, something like this...

$session =New-PSSession -ComputerName MachineName
Invoke-Command -Session $session -ScriptBlock {
# Change to the executable location
Push-Location Path C:\Windows\ccmsetup

# Execute the process and wait for completion before continuing
Start-Process -FilePath -Arguments -wait

# Return to the original directory
Pop-Location
}

Secondly, running native commands / exe require a different approach.
See these articles:

PowerShell: Running Executables
'social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx'

Calling native commands from PowerShell
'powershellmagazine.com/2013/01/02/calling-native-commands-from-powershell'