Getting op-addition errors while using cim cmdlet

Welcome Forums General PowerShell Q&A Getting op-addition errors while using cim cmdlet

Viewing 3 reply threads
  • Author
    Posts
    • #249356
      Participant
      Topics: 16
      Replies: 23
      Points: 111
      Rank: Participant

      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:

      <br />Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named op_Addition.</p><p>At C:\Users\10044677_A\Documents\Scripts\Get-NICs\Get-Nics.ps1:25 char:21</p><p>+ $NicDetails1 += $OutputObj1}</p><p>+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~</p><p>+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException</p><p>+ FullyQualifiedErrorId : MethodNotFound</p><p>#Below is the actual script I am running…</p><p>$NicDetails1 = @()</p><p>$NicDetails2 = @()</p><p>$FinalList1 = @()</p><p>$FinalList2 = @()</p><p>$lc = 1</p><p>Write-host ‘Getting list of servers from AD, this may take a few moments…’</p><p>$Servers = (Get-ADComputer -Filter ‘operatingsystem -like “*server*”-and enabled -eq “true”‘).Name</p><p>foreach ($Server in $Servers) {</p><p>if(Test-Connection -ComputerName $Server -Count 1 -ea 0) {</p><p>write-host ‘Getting NIC details from’$server $lc ‘of’ ($Servers.count)</p><p>$NetAdapter = $null</p><p>$NicConfig = $null</p><p>$NetAdapter = get-ciminstance -classname win32_networkadapter -computername $Server -filter “NetConnectionStatus = 2” | select netconnectionid,deviceid -ea silentlycontinue</p><p>$NicConfig = get-ciminstance -classname win32_networkadapterconfiguration -computername $Server -filter “ipenabled = true” | select ipaddress,ipsubnet,defaultipgateway,index -ea SilentlyContinue</p><p>if($netadapter){ }</p><p>foreach ($Nic in $Netadapter){</p><p>$NicName = $null</p><p>$NicID = $null</p><p>$NicName = $Nic.netconnectionid</p><p>$NicID1 = $Nic.deviceid</p><p>$OutputObj1 = New-Object -Type PSObject</p><p>$OutputObj1 | Add-Member -MemberType NoteProperty -Name Sever -Value $Server</p><p>$OutputObj1 | Add-Member -MemberType NoteProperty -Name AdapterName -Value $NicName</p><p>$OutputObj1 | Add-Member -MemberType NoteProperty -Name AdapterID1 -Value $NicID1</p><p>$NicDetails1 += $OutputObj1}</p><p>}</p><p>if($NicConfig){</p><p>foreach ($Nic in $NicConfig){</p><p>$NicIP = $null</p><p>$NicSubnet = $null</p><p>$NicGW = $Null</p><p>$NicID2 = $null</p><p>$NicIP = $Nic.ipaddress</p><p>$NicSubnet = $Nic.ipsubnet</p><p>$NicGW = $Nic.defaultipgateway</p><p>$NicID2 = $Nic.index</p><p>$OutputObj2 = New-Object -Type PSObject</p><p>$OutputObj2 | Add-Member -MemberType NoteProperty -Name Server -Value $Server</p><p>$OutputObj2 | Add-Member -MemberType NoteProperty -Name IP -Value $NicIP</p><p>$OutputObj2 | Add-Member -MemberType NoteProperty -Name SubnetAddresss -Value $NicSubnet</p><p>$OutputObj2 | Add-Member -MemberType NoteProperty -Name DefaultGateway -Value $NicGW</p><p>$OutputObj2 | Add-Member -MemberType NoteProperty -Name AdapterID2 -Value $NicID2</p><p>$NicDetails2 += $OutputObj2}</p><p>}</p><p>$FinalList1 += $NicDetails1</p><p>$FinalList2 += $NicDetails2</p><p>$NicDetails1 = $Null</p><p>$NicDetails2 = $null</p><p>$lc ++</p><p>}</p><p>$FinalList1 | export-csv NicDetails1.csv -NoTypeInformation</p><p>$FinalList2 | Export-Csv NicDetails2.csv -NoTypeInformation<br />

      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!

       

      • This topic was modified 1 month, 1 week ago by kvprasoon. Reason: code formatting
    • #249377
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      The code is a bit over complicated. This returns a single result for each adapter on a system. There is a SystemName property that has the computername, otherwise, you could append that from your computer loop as well. If you assign a variable to the loop, then you don’t need to build the object with += and everything just rolls up to the variable that is returned in the loop. The results in the full script would be in the server loop, but here is an example of how you would join the adapter information:

      <br />$NetAdapter = Get-CimInstance -classname win32_networkadapter -computername $Server -filter NetConnectionStatus = 2” -Property * |<br />              Select DeviceID,<br />                     Name,<br />                     AdapterType,<br />                     NetConnectionID,<br />                     Speed,<br />                     SystemName,<br />                     ProductName</p><p>$results = foreach ( $adapter in $NetAdapter ) {<br />    $NicConfig = Get-CimInstance -classname win32_networkadapterconfiguration -computername $Server -filter ipenabled = true AND index = $($adapter.deviceId) |<br />                 Select @{Name='AdapterDeviceID';Expression={$adapter.DeviceID}},<br />                        @{Name='AdapterName';Expression={$adapter.Name}},<br />                        @{Name='AdapterType';Expression={$adapter.AdapterType}},<br />                        @{Name='AdapterNetConnectId';Expression={$adapter.NetConnectionID}},<br />                        @{Name='AdapterSpeed';Expression={$adapter.Speed}},<br />                        @{Name='AdapterSystemName';Expression={$adapter.SystemName}},<br />                        @{Name='AdapterProductName';Expression={$adapter.ProductName}},<br />                        @{Name='AdapterAdapterType';Expression={$adapter.AdapterType}},<br />                        ipaddress,<br />                        ipsubnet,<br />                        defaultipgateway,<br />                        index</p><p>    $NicConfig<br />}<br />

      • This reply was modified 1 month, 1 week ago by kvprasoon. Reason: code formatting
    • #249389
      Participant
      Topics: 7
      Replies: 557
      Points: 2,122
      Helping Hand
      Rank: Community Hero

      Why did you duplicate your post?

    • #249401
      Participant
      Topics: 16
      Replies: 23
      Points: 111
      Rank: Participant

      Thanks Rob and apologies Doug, I was changing things around in the script and getting slightly different results re-posted as I thought my earlier post may have been thought of as answered

Viewing 3 reply threads
  • You must be logged in to reply to this topic.