Hi Guys.
I have 78 servers in domain where i have to uninstall SCCM agent manually. So i wrote a script which works fine, and luckily 62 server are done. here is my script,
$computer= Get-Content C:\SCCM\computer.txt
$session =New-PSSession -ComputerName $computer
$scriptBlock= { C:\windows\ccmsetup\ccmsetup.exe /uninstall}
Invoke-Command -Session $session -ScriptBlock $scriptBlock
However, for few servers (probably 10) the script generate error as follows -
New-PSSession : [MachineName] Connecting to remote server MachieName failed with the following error message : The client canno
t connect to the destination specified in the request. Verify that the service on the destination is running and is acceptin
g requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS o
r WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the
WinRM service: “winrm quickconfig”. For more information, see the about_Remote_Troubleshooting Help topic.
At C:\SCCM\SCCMUninstall.ps1:2 char:11
- $session =New-PSSession -ComputerName $computer
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : OpenError: (System.Manageme…RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTran
sportException - FullyQualifiedErrorId : CannotConnect,PSSessionOpenFailed
Invoke-Command : Cannot validate argument on parameter ‘Session’. The argument is null or empty. Provide an argument that is
not null or empty, and then try the command again.
At C:\SCCM\SCCMUninstall.ps1:4 char:25
- CategoryInfo : OpenError: (System.Manageme…RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTran
- Invoke-Command -Session $session -ScriptBlock $scriptBlock
-
~~~~~~~~
- CategoryInfo : InvalidData: ( [Invoke-Command], ParameterBindingValidationException
- FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand
I have already checked and WinRM service is running at destination.
Important point - if i login to machine “MachineName” and execute (Enable-PSRemoting -force) then i’m able to execute my script over remote connection.
My questions - How can i execute (Enable-PSRemoting -force) at the start of my script?
Note- I’ve just started learning powershell scripting, please excuse me for anything found stupid in my note.
Thanks,
Akhtar Nawaz