gangstanthony

NIC Inventory

Feb 26th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # https://www.reddit.com/r/PowerShell/comments/47r1kp/help_cleaning_up_a_nic_inventory_script/
  2.  
  3. $error.clear()
  4. $ErrorActionPreference = 'Stop'
  5.  
  6. $list = @([pscustomobject]@{Server = $env:COMPUTERNAME})
  7.  
  8. $nictotal = @()
  9. foreach ($server in $list) {
  10.     $hash = [ordered]@{}
  11.     $sname = $server.Server + '.group.wan'
  12.     $hash['Server'] = $sname
  13.  
  14.     if ((Test-Connection -comp $sname -count 2 -quiet) -eq $true) {
  15.         try {
  16.             $NICS = gwmi -cn $sname win32_networkadapterconfiguration -Filter ipenabled=true
  17.         } catch {  
  18.             $a = $_.Exception.Message
  19.         }
  20.         if ($NICS) {
  21.             # Check to see if the machine is a VM.
  22.             $vmcheck = gwmi -cn $sname win32_computersystem -Filter "model like '%virtual%'"
  23.  
  24.             $i = 1
  25.             foreach ($NIC in $NICS) {
  26.                 $NICString = "NIC$i"
  27.                 $i++
  28.                 $ips = @($NIC.IPAddress | ? {$_ -notmatch ':'} | ? {$_}) -join ','
  29.                 $hash["$NICString IPAddress"] = $ips
  30.                 $hash["$NICString Default Gateway"] = @($NIC.defaultipgateway | ? {$_}) -join ','
  31.                 $hash["$NICString DNS"] = @($NIC.DNSServerSearchOrder | ? {$_}) -join ','
  32.                 if ($vmcheck) {
  33.                     $vmnics = get-scvirtualnetworkadapter -vm $sname
  34.                     foreach ($vnic in $vmnics) {
  35.                         $vip = $vnic.IPv4Addresses
  36.                         if ($vip -eq $ips) {
  37.                             $vlanid = $vnic.vlanid
  38.                         }
  39.                     }
  40.                     $hash["$NICString VLAN"] = $vlanid
  41.                 } else  {
  42.                     $hash["$NICString VLAN"] = 'Default'
  43.                 }
  44.             }
  45.         }
  46.     } else {
  47.         <#
  48.         $hash["$NICString IPAddress"] = ''
  49.         $hash["$NICString Default Gateway"] = ''
  50.         $hash["$NICString DNS"] = ''
  51.         $hash["$NICString VLAN"] = ''
  52.         #>
  53.         "Can't ping $sname"
  54.     }
  55.     $nictotal += New-Object psobject -Property $hash
  56. }
  57.  
  58. $nictotal
Add Comment
Please, Sign In to add comment