Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://www.reddit.com/r/PowerShell/comments/47r1kp/help_cleaning_up_a_nic_inventory_script/
- $error.clear()
- $ErrorActionPreference = 'Stop'
- $list = @([pscustomobject]@{Server = $env:COMPUTERNAME})
- $nictotal = @()
- foreach ($server in $list) {
- $hash = [ordered]@{}
- $sname = $server.Server + '.group.wan'
- $hash['Server'] = $sname
- if ((Test-Connection -comp $sname -count 2 -quiet) -eq $true) {
- try {
- $NICS = gwmi -cn $sname win32_networkadapterconfiguration -Filter ipenabled=true
- } catch {
- $a = $_.Exception.Message
- }
- if ($NICS) {
- # Check to see if the machine is a VM.
- $vmcheck = gwmi -cn $sname win32_computersystem -Filter "model like '%virtual%'"
- $i = 1
- foreach ($NIC in $NICS) {
- $NICString = "NIC$i"
- $i++
- $ips = @($NIC.IPAddress | ? {$_ -notmatch ':'} | ? {$_}) -join ','
- $hash["$NICString IPAddress"] = $ips
- $hash["$NICString Default Gateway"] = @($NIC.defaultipgateway | ? {$_}) -join ','
- $hash["$NICString DNS"] = @($NIC.DNSServerSearchOrder | ? {$_}) -join ','
- if ($vmcheck) {
- $vmnics = get-scvirtualnetworkadapter -vm $sname
- foreach ($vnic in $vmnics) {
- $vip = $vnic.IPv4Addresses
- if ($vip -eq $ips) {
- $vlanid = $vnic.vlanid
- }
- }
- $hash["$NICString VLAN"] = $vlanid
- } else {
- $hash["$NICString VLAN"] = 'Default'
- }
- }
- }
- } else {
- <#
- $hash["$NICString IPAddress"] = ''
- $hash["$NICString Default Gateway"] = ''
- $hash["$NICString DNS"] = ''
- $hash["$NICString VLAN"] = ''
- #>
- "Can't ping $sname"
- }
- $nictotal += New-Object psobject -Property $hash
- }
- $nictotal
Add Comment
Please, Sign In to add comment