4 Replies Latest reply on Aug 2, 2013 1:54 PM by aLTeReGo

    SNIPPET: PowerShell Monitors that require a Server Name (Not IP)

    KMSigma


      I've had all kinds of issues trying to get the computer name from an IP since SAM only sends an IP to the scripts.  I've found a very easy solution to this after picking my brain for WAY too long.  Some PowerShell functions require that you pass the computer name (Exchange being a key player), so getting this information into the script was really important.

       

      I used to pull the information from the System Information WMI Class, but that's time consuming and prone to timeouts.

       

      Here's what I've done instead:

      if ( '${IP}' -like "*.*.*.*" )
      {     
       $ServerIP = '${IP}'
      }
      else
      {
           # Use this IP if this is not being run from within the SAM Script Editor
          $ServerIP = "10.5.5.20"
      }
      $FQDNServerName = ([System.Net.Dns]::GetHostbyAddress($ServerIP)).HostName
      $ServerName     = $FQDNServerName.Split(".")[0].ToUpper()


      Since I normally use an alternate editor for my PowerShell Scripts (PowerGUI), this was the best way to handle the server name lookup.  It does have a restriction that it uses DNS to handle this lookup.  If you don't have DNS available, you may have to use another method.