Hi all,
I have a script which uses a couple of cim cmdlets as some of our many servers are still annoyingly W2K8
The script uses two CIM cmdlets ‘win32_networkadapter’ which I need to obtain the network name (Contains vlan info in the name)
The other ‘win32_networkconfig’ pulls the Ip info
Both also pull down the nic reference Id per adapter which I will use later as a reference to tie the to sets of data together
Also as I have issues using both cmdlets with multiple nic results I need to look each returning value to an array but needed to create two arrays to contain the data from each cmdlet, hopefully the script will explain more
So, the script seems to run ok, the first few servers are collected as expected however as soon as I run into a server which is behind a firewall the connection takes some time to time out, a method of how to pre-check and avoid this would be much appreciated although not my main issue currently
After hitting this first unconnectable device something in the script goes a bit awry and on the next connectable device only the first nic is collected and then I receive the following error per connection:
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named ‘op_Addition’.
At C:\Users\10044677_A\Documents\Scripts\Get-NICs\Get-Nics.ps1:25 char:21
-
$NicDetails1 += $OutputObj1}
-
-
CategoryInfo : InvalidOperation: (op_Addition:String) , RuntimeException
-
FullyQualifiedErrorId : MethodNotFound
#Below is the actual script I am running…
$NicDetails1 = @()
$NicDetails2 = @()
$FinalList1 = @()
$FinalList2 = @()
$lc = 1
Write-host ‘Getting list of servers from AD, this may take a few moments…’
$Servers = (Get-ADComputer -Filter ‘operatingsystem -like “server”-and enabled -eq “true”‘).Name
foreach ($Server in $Servers) {
if(Test-Connection -ComputerName $Server -Count 1 -ea 0) {
write-host ‘Getting NIC details from’$server $lc ‘of’ ($Servers.count)
$NetAdapter = $null
$NicConfig = $null
$NetAdapter = get-ciminstance -classname win32_networkadapter -computername $Server -filter “NetConnectionStatus = 2” | select netconnectionid,deviceid -ea silentlycontinue
$NicConfig = get-ciminstance -classname win32_networkadapterconfiguration -computername $Server -filter “ipenabled = true” | select ipaddress,ipsubnet,defaultipgateway,index -ea SilentlyContinue
if($netadapter){ }
foreach ($Nic in $Netadapter){
$NicName = $null
$NicID = $null
$NicName = $Nic.netconnectionid
$NicID1 = $Nic.deviceid
$OutputObj1 = New-Object -Type PSObject
$OutputObj1 | Add-Member -MemberType NoteProperty -Name Sever -Value $Server
$OutputObj1 | Add-Member -MemberType NoteProperty -Name AdapterName -Value $NicName
$OutputObj1 | Add-Member -MemberType NoteProperty -Name AdapterID1 -Value $NicID1
$NicDetails1 += $OutputObj1}
}
if($NicConfig){
foreach ($Nic in $NicConfig){
$NicIP = $null
$NicSubnet = $null
$NicGW = $Null
$NicID2 = $null
$NicIP = $Nic.ipaddress
$NicSubnet = $Nic.ipsubnet
$NicGW = $Nic.defaultipgateway
$NicID2 = $Nic.index
$OutputObj2 = New-Object -Type PSObject
$OutputObj2 | Add-Member -MemberType NoteProperty -Name Server -Value $Server
$OutputObj2 | Add-Member -MemberType NoteProperty -Name IP -Value $NicIP
$OutputObj2 | Add-Member -MemberType NoteProperty -Name SubnetAddresss -Value $NicSubnet
$OutputObj2 | Add-Member -MemberType NoteProperty -Name DefaultGateway -Value $NicGW
$OutputObj2 | Add-Member -MemberType NoteProperty -Name AdapterID2 -Value $NicID2
$NicDetails2 += $OutputObj2}
}
$FinalList1 += $NicDetails1
$FinalList2 += $NicDetails2
$NicDetails1 = $Null
$NicDetails2 = $null
$lc ++
}
$FinalList1 | export-csv NicDetails1.csv -NoTypeInformation
$FinalList2 | Export-Csv NicDetails2.csv -NoTypeInformation
So this should produce two csv files, I will look into how I can match the two nic ID values via powershell later if possible as that would be sooo much better than performing a vlookup in excel
Any assistance would be much appreciated
Many thanks all!