Trying to stop windows services on multiple servers using PowerShell script. Below is the script I am using,
<# stop windows services #>
Set-ExecutionPolicy Bypass
$Serverlist = @('Wserver1'),
('Wserver2'),
('Wserver3')
Foreach ($server in $Serverlist) {
#Service Account Configuration
$Username = 'domain\svc_admin'
$Password = 'ABCD1234'
#Convert Credential Information for WinRM Sessions
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
$session = New-PSSession -ComputerName $server[0] -credential $Cred
Set-ExecutionPolicy Bypass
Invoke-Command -Session $session -ScriptBlock {
#List Services
$Services= @("PrintingService",
"LoggingService")
#Stop Services
Foreach ($Service in $Services) {
Stop-Service $Service -Force
write-host "Stopped $Using:Server[0] - $Service"
}
}
}
It stops the services in Wserver1 but fails to do the same in Wserver2 and Wserver3. It gives below error,
New-PSSession : [d] Connecting to remote server d failed with the following error message : WinRM cannot process the request. The following error occurred while using Kerberos authentication: Cannot find the computer d. Verify that the
computer exists on the network and that the name provided is spelled correctly. For more information, see the about_Remote_Troubleshooting Help topic.
At D:\PS Script\Stop_Services.ps1:35 char:16
- ... $session = New-PSSession -ComputerName $server[0] -credential $Cred
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
- FullyQualifiedErrorId : NetworkPathNotFound,PSSessionOpenFailed
I added breakpoint and found that the username, password, server are passing through the loop correctly, but session is empty for Wserver2 and Wserver3.
Please guide.