Ping Script Help

This topic contains 3 replies, has 2 voices, and was last updated by  John K 2 years, 8 months ago.

  • Author
    Posts
  • #12930

    John K
    Participant

    Hi,

    I've written the script below to check AD for win 2k3 servers then create a text file listing the servers. After that I would like to run a ping against them.

    The problem I've had is initially I used "format-list" before outputting to text file. This adds a "Name : ..." before the server name causing problems when trying to use the text in the next part. I changed from "format-list" to "form-table" and tried to remove the "name" title through formatting. This is messy but kind of worked.

    When it comes to pinging the servers, they all reply "No ping response", which I know is incorrect. I should add the ping part of the script I found else where and change some text.

    Any help welcome, please bear in mind I'm a noob so go easy 🙂

    # attempting to remove "name" field from text output.
    $fmtname =@{label=" ";Expression={$_.name};}

    # Searching AD for WIN2k3 servers and outputting only server name to text.
    Get-ADComputer -Filter * -SearchBase "OU=Non-Regulated,OU=Edinburgh-UK,OU=Servers,DC=CR,DC=LOCAL" -Properties operatingsystem |`
    Where-Object -filter {$_.OperatingSystem -eq 'Windows Server 2003'} | select-object -property name | format-table $fmtname | out-file c:\VCC-04_WIN2K3_SRV.txt

    # Added waiting period for text file to populate.
    Start-Sleep -s 10

    # Ping servers in VCC-04_WIN2K3_SRV.txt.
    $ServerName = Get-content c:\VCC-04_WIN2K3_SRV.txt

    foreach ($Server in $ServerName) {

    if (test-Connection -ComputerName $Server -Quiet ) {

    write-Host "$Server is alive and Pinging " -ForegroundColor Green

    } else

    { Write-Warning "$Server No ping response"

    }
    }

  • #12933

    John K
    Participant

    Thanks Dave, that's worked a treat. I'm not familiar with the "ExpandProperty" parameter so I'll need to find out more about it.

    I guess there was something wrong with my text file that was stopping the ping section working properly.

  • #22539

    John K
    Participant

    Hi again,

    I've not bothered to start another thread as this is really a continuation of the above.

    what I'd like to achieve:

    Ping servers listed in a text file – say every 5 minutes.
    If the ping response fails once add to a count, it fails on the next round send an email.

    I have a script that I used to ping servers and I output the results to text. I also have a script I wrote for sending emails, however this script was triggered as a scheduled task. I'm trying to work out the best way to have the failure/error count trigger the email?

    I'm not looking to have someone write a script for me, I'd like to learn and understand how it works, so just some suggestions or guidance would be welcome.

    Thanks.

  • #12932

    Dave Wyatt
    Moderator

    If your goal is just to create a text file containing computer names, you don't need to use the Format cmdlets at all; you can just use Select-Object -ExpandProperty and pipe the strings straight to Out-File:

    On a side note, there's no need to use backticks as a line continuation character after a pipe. Certain characters automatically allow line continuation in PowerShell, and the pipe is one of them. I've also moved your OperatingSystem filter into the Get-ADComputer command, for better performance:

    Get-ADComputer -Filter 'OperatingSystem -eq "Windows Server 2003"' -SearchBase "OU=Non-Regulated,OU=Edinburgh-UK,OU=Servers,DC=CR,DC=LOCAL" |
    Select-Object -ExpandProperty Name |
    Out-File -FilePath c:\VCC-04_WIN2K3_SRV.txt
    

    Edit: Strictly speaking, it's possible for a computer's hostname to be different from its "Name" (which is actually the CN portion of its distinguished name) in Active Directory. Usually they match, but keep that in mind if you have some weird results.

You must be logged in to reply to this topic.