PowerShell script fails

Hari 26 Reputation points
2022-11-02T01:07:56.61+00:00

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.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,449 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,641 Reputation points
    2022-11-02T01:37:27.493+00:00

    Change the array.

    $Serverlist = @('Wserver1','Wserver2','Wserver3')  
    

    Get rid of the [0]. Change line 10 to:

     $session = New-PSSession -ComputerName $server -credential $Cred    
    

    and line 22 to:

    write-host "Stopped $Using:Server   -  $Service"   
    
    
      
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Hari 26 Reputation points
    2022-11-02T01:48:32.163+00:00

    It worked, thanks.

    0 comments No comments

  2. Rich Matheisen 45,671 Reputation points
    2022-11-02T02:09:38.983+00:00

    There's no need to create PSSessions. You aren't sharing data between multiple Invoke-Commands to the same machine. Just use the -ComputerName parameter with the list of server names and you'll also gain the advantage of concurrent execution. There's also no need to use the name of the machine from your list of servers in that Write-Host; just get the name of the machine from the $ENV variable.

    # stop windows services #  
      
    $Serverlist = 'Wserver1', 'Wserver2', 'Wserver3'  
      
    #Service Account Configuration  
    $Username = 'domain\svc_admin'  
    $Password = 'ABCD1234'  
          
    # Convert Credential Information  
    $pass = ConvertTo-SecureString -AsPlainText $Password -Force  
    $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $pass  
    Invoke-Command -ComputerName $ServerList  -Credential $cred -ScriptBlock {  
        #List Services   
        $Services = "PrintingService","LoggingService"  
              
        #Stop Services  
        Foreach ($Service in $Services) {  
            Stop-Service $Service -Force  
            Write-Host "Stopped $($Env:ComputerName)   -  $Service"   
        }   
    }  
    

  3. Hari 26 Reputation points
    2022-11-02T02:48:20.15+00:00

    Thanks Rich, iam getting below error.

    Invoke-Command : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ComputerName'. Specified method is not supported.