[Powershell] Get-Subnet

The Get-subnet function retrieves the IP and subnet mask from the active network interface.
Using the IP and Subnet mask it will calculate the Subnet for the given computer name.
This function makes use of the Get-Nicinfo function.

   1: function Get-Subnet{
   2:     [CmdletBinding()] 
   3:     PARAM 
   4:     ( 
   5:         [Parameter(Position=1,
   6:                     Mandatory=$false,
   7:                     ValueFromPipeline = $true,
   8:                     Helpmessage="The computer name")] 
   9:         [string]
  10:         $computername="."
  11:     )
  12:     BEGIN
  13:     {
  14:         #set the erroractionpreference to SilentlyContinue. 
  15:         #(We're not concerned about offline machines)
  16:         $erroractionpreference = "SilentlyContinue"
  17:         $result = @()
  18:     }
  19:     PROCESS
  20:     {
  21:         $object = new-object Object
  22:         $object| add-member Noteproperty Name -Value $computername
  23:         #makes use of the Get-Nicinfo function
  24:         $info = Get-Nicinfo -Computername $computername 
  25:         $ip =  $info.IPAddress[0]
  26:         $subnetmask = $info.Subnet[0]
  27:         $object| add-member Noteproperty IP -Value $ip
  28:         $object| add-member Noteproperty subnetMask -Value $subnetmask
  29:     
  30:         $ips = $ip.split(".")   
  31:         $subnetmasks = $subnetmask.split(".")   
  32:         $boundary = $null  
  33:         
  34:         for ($i=0; $i -lt 4; $i++ )   
  35:         {  
  36:             $boundary += [string]($ips[$i] -band $subnetmasks[$i])  
  37:             if($i -lt 3)  
  38:             {  
  39:                 $boundary += "."  
  40:             }  
  41:         } 
  42:         $object| add-member Noteproperty subnet -Value $boundary
  43:         $result +=$object
  44:     }
  45:     END
  46:     {
  47:         $result
  48:     }
  49: }
Attachment: Get-Subnet.zip
Published Saturday, February 13, 2010 2:57 PM by scallebaut
Filed under: ,

Comments

No Comments