Foreach and Test-Connection; ignoring unreachable machines

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Mark Hammonds Mark Hammonds 1 year, 8 months ago.

  • Author
    Posts
  • #31826
    Profile photo of Ed Grant
    Ed Grant
    Participant

    Hello,

    I am working on a script that I want to do the following:

    1. Grabs a list of machines from a .txt file
    2. Does a Test-Connection on each item
    3. Ignores any machines that are turned off
    4. Create a new PSSession and send a scriptblock to all machines that are reachable

    I am having issues with items 3 and 4. Here is my code so far.

    $cred = Get-Credential domain\cred
    $TargetSession = Get-Content C:\computers.txt
    
    $Session = New-PSSession $TargetSession -Credential $cred
    
     foreach
        ($Computer in $session)
    {
          if (Test-Connection -ComputerName $Computer.ComputerName -Quiet)
            {
            
                Invoke-Command -Session $Computer -ScriptBlock{
               Code here }
         
         else {Out-Null}    
    

    I still get the error message:

    Connecting to remote server OFFLINECOMPUTER failed with the following error message : WinRM cannot complete the operation. Verify that the specified computer name is valid, that the computer is accessible over the network, and that a firewall exception for the WinRM service is enabled and allows access from this computer.

    I know I am missing something, but I cannot seem to find what it is. Any help would be greatly appreciated.

    Ed

  • #31827
    Profile photo of Mark Hammonds
    Mark Hammonds
    Participant

    the issue you are having is you are trying to connect to a PSsession with a computer that is offline. you need to move the PSsession inside your check for connectivity.
    Try This
    also add ping count and buffer size so your script does not take all day. if it is a huge list of computers you might want to look into jobs

    $cred = Get-Credential domain\cred
    $TargetSession = Get-Content C:\computers.txt
    
     foreach
        ($Computer in $TargetSession)
    {
          if (Test-Connection -ComputerName $Computer.ComputerName -BufferSize 16 -Count 1 -Quiet)
            {
                $Session = New-PSSession $Computer.ComputerName -Credential $cred
                Invoke-Command -Session $Session -ScriptBlock{
               Code here }
                Remove-PSSession -Session $Session 
             }
             else {Out-Null}     
    }
    
  • #31846
    Profile photo of Ed Grant
    Ed Grant
    Participant

    Mark,

    Thanks a bunch, that did the trick. I must have been looking at my code for too long and didn't realize my mistake. Now the script works perfectly and I can finish up the rest of my tool this morning.

    This will only be running against ~10 machines so the impact isn't huge. But if I need to expand this in the future, I will look into jobs.

    Cheers,
    Ed

  • #31856
    Profile photo of Mark Hammonds
    Mark Hammonds
    Participant

    Happy I could help 😀

You must be logged in to reply to this topic.