header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
AD Ping Script
Last Post 17 May 2009 11:08 AM by Shay Levy. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
SynJunkieUser is Offline
Basic Member
Basic Member
Posts:126
Avatar

--
25 Mar 2009 09:42 AM
    Hi guys

    I have put together a script that will take use QAD-Computer to retrieve a list of server that are in AD and then ping them.  This works well and the error that I recieve if the server isn't pingable is fine. After pinging the list of servers I query them for using WMI.  Although this works I would like to know if there is a nice way to tidy up the WMI error messaes for servers that don't respond to WMI.  Ideally I would like to have a similar (but not the same) error as to the ping failure for WMI failures.  Also is there a way to make the WMI timeouts any quicker?

    Here's my script.

    # This script looks for all servers in AD, pings them and then queries the responding servers.
    cls
    #This function gets the WMI info
    function Get-Info{
    gwmi win32_operatingsystem -comp $_.name | select CSName,SerialNumber
    trap {
    write-Warning "RPC is not responding on $computer"
    continue}
    }
    #This queries AD for Servers
    $AD_Servers = get-qadcomputer -osname '*server*' -sizelimit 0

    #This function pings each server in AD
    function Ping-Host {
    Begin { $Ping = new-object System.Net.NetworkInformation.Ping }
    Process {
    $computer = $_.name
    trap {
    write-warning "$computer is not pingable"
    continue}
    if ($Ping.Send($_.name)) { Get-Info }
    }}

    $AD_Servers | Ping-Host


    Thanks for any advice.

    Regards

    Lee
    AythUser is Offline
    Basic Member
    Basic Member
    Posts:237
    Avatar

    --
    06 May 2009 12:14 PM
    Nice script.

    For cleaning up the WMI errors one way would be to use the -ErrorAction parameter which almost all cmdlets support. For example

    gwmi win32_operatingsystem -comp $_.name -ErrorAction SilentlyContinue| select CSName,SerialNumber

    That should remove any error messages you see from your output.

    As for WMI timeouts check this article I found: http://blog.usepowershell.com/2009/...-timeouts/

    Seems pretty decent, makes use of the returnimmediately flag, I haven't used it with powershell but I have with vbscript. Cheers.
    My Blog about Powershell http://poweroftheshell.blogspot.com/ Follow me on twitter @darrinhenshaw
    tojo2000User is Offline
    New Member
    New Member
    Posts:45
    Avatar

    --
    17 May 2009 10:20 AM
    Also, your trap statement should come before the gwmi line if you want trap to catch any terminating errors created by that statement.
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    17 May 2009 11:08 AM

    Ayth is right, as far as I remember RPC errors are not trapable, they are not terminating errors so the TRAP block won't invoke.
    You should also follow tojo's suggestion, put all code after the trap call (in case the error is terminating error)

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer