Strange method invocation issue using nested arrays…

Welcome Forums General PowerShell Q&A Strange method invocation issue using nested arrays…

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

      Hi all,

      I have the following powershell script I have put together to pull remote server adapter details, it is written using v2 as some of our servers are unfortunately still using Windows Sever 2008

      I am receiving a ‘Method invocation failed because [system.management.automation.psobject] does not contain a method named ‘op_addition’

      I believe this may be due to trying to add the results of two psobject values to the same array variable but am not sure if this is true or how to work around the issue

      <br />$NicDetails = @()</p><p>$FinalList = @()</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</p><p>}</p><p>foreach ($Nic in $Netadapter){</p><p>$NicName = $null</p><p>$NicID = $null</p><p>$NicName = $Nic.netconnectionid</p><p>$NicID = $Nic.deviceid</p><p>$OutputObj = New-Object -Type PSObject</p><p>$OutputObj | Add-Member -MemberType NoteProperty -Name AdapterName -Value $NicName</p><p>$OutputObj | Add-Member -MemberType NoteProperty -Name AdapterID1 -Value $NicID</p><p>$NicDetails += $OutputObj}</p><p>foreach ($Nic in $NicConfig){</p><p>$NicIP = $null</p><p>$NicSubnet = $null</p><p>$NicGW = $Null</p><p>$NicIND = $null</p><p>$NicIP = $Nic.ipaddress</p><p>$NicSubnet = $Nic.ipsubnet</p><p>$NicGW = $Nic.defaultipgateway</p><p>$OutputObj = New-Object -Type PSObject</p><p>$OutputObj | Add-Member -MemberType NoteProperty -Name IP -Value $NicIP</p><p>$OutputObj | Add-Member -MemberType NoteProperty -Name SubnetAddresss -Value $NicSubnet</p><p>$OutputObj | Add-Member -MemberType NoteProperty -Name DefaultGateway -Value $NicGW</p><p>$OutputObj | Add-Member -MemberType NoteProperty -Name AdapterID2 -Value $NicIND</p><p>$NicDetails += $OutputObj}</p><p>write-host $NicDetails</p><p>$FinalList += $NicDetails</p><p>$NicDetails = $Null</p><p>$lc ++</p><p>}</p><p>$FinalList | export-csv NicDetails.csv<br />

      Many thanks for any help!

      • This topic was modified 1 month, 1 week ago by kvprasoon. Reason: code formattig
    • #249170
      Participant
      Topics: 16
      Replies: 23
      Points: 111
      Rank: Participant

      Apologies, there were errors in my script, corrected now but results still the same:

      </p><p>$NicDetails = @()<br />$FinalList = @()<br />$lc = 1<br />Write-host 'Getting list of servers from AD, this may take a few moments...'<br />$Servers = (Get-ADComputer -Filter 'operatingsystem -like "*server*"-and enabled -eq "true"').Name<br />foreach ($Server in $Servers) {<br />if(Test-Connection -ComputerName $Server -Count 1 -ea 0) {<br />write-host 'Getting NIC details from'$server $lc 'of' ($Servers.count)<br />$NetAdapter = $null<br />$NicConfig = $null<br />$NetAdapter = get-ciminstance -classname win32_networkadapter -computername $Server -filter "NetConnectionStatus = 2" | select netconnectionid,deviceid -ea silentlycontinue<br />$NicConfig = get-ciminstance -classname win32_networkadapterconfiguration -computername $Server -filter "ipenabled = true" | select ipaddress,ipsubnet,defaultipgateway,index<br />}<br />foreach ($Nic in $Netadapter){<br />$NicName = $null<br />$NicID = $null<br />$NicName = $Nic.netconnectionid<br />$NicID1 = $Nic.deviceid<br />$OutputObj  = New-Object -Type PSObject<br />$OutputObj | Add-Member -MemberType NoteProperty -Name AdapterName -Value $NicName<br />$OutputObj | Add-Member -MemberType NoteProperty -Name AdapterID1 -Value $NicID1<br />$NicDetails += $OutputObj}<br />foreach ($Nic in $NicConfig){<br />$NicIP = $null<br />$NicSubnet = $null<br />$NicGW = $Null<br />$NicID2 = $null<br />$NicIP = $Nic.ipaddress<br />$NicSubnet = $Nic.ipsubnet<br />$NicGW = $Nic.defaultipgateway<br />$NicID2 = $Nic.index<br />$OutputObj  = New-Object -Type PSObject<br />$OutputObj | Add-Member -MemberType NoteProperty -Name IP -Value $NicIP<br />$OutputObj | Add-Member -MemberType NoteProperty -Name SubnetAddresss -Value $NicSubnet<br />$OutputObj | Add-Member -MemberType NoteProperty -Name DefaultGateway -Value $NicGW<br />$OutputObj | Add-Member -MemberType NoteProperty -Name AdapterID2 -Value $NicID2<br />$NicDetails += $OutputObj}<br />write-host $NicDetails<br />$FinalList += $NicDetails<br />$NicDetails = $Null<br />$lc ++<br />}<br />$FinalList | export-csv NicDetails.csv</p><p>

      Thanks

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

      Hi Martin,

      The issue is you are trying to combine two lists with different types of objects with +=. Take this example

      [/crayon]

      You will see a familiar message

      [/crayon]

      How would that CSV look anyways, with the different objects? Please clarify what you are trying to achieve, there is likely an easier approach/answer. Also you are using several outdated and slow practices with the array collection and Add-Member. Do you have PSremoting available? If so you could really improve the speed of this type of task.

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

      Hi Doug,

      Many thanks for your reply,

      So basically I need to produce a list of remote machines active network adapters basic IP,subnet and gateway as well as the actual network name which contains the numerical value of the vlan used

      Do to the additional annoyance of a small random portion of my company’s large infrastructure being Windows Server 2008 I need to use the two earlier  objects ‘win32_networkadapter’ and ‘win32_networkadapterconfiguration’

      I want to make a list of servername,netconnectionid,ipaddress,ipsubnet,defaultipgateway

      however as I have two sets of objects my next hurdle would be to match the deviceid.index and then work out how to join the objects from both

       

      Yeah its a mess and to be honest Doug, I could really do with your help

      Thanks again

       

       

    • #249371
      Participant
      Topics: 7
      Replies: 557
      Points: 2,122
      Helping Hand
      Rank: Community Hero

      Here’s how I would handle it. The invoke-command cmdlet will run asynchronously against the servers, 32 at a time by default. You can raise or lower this amount by adding the parameter -ThrottleLimit and your desired maximum. You won’t get a play by play of “which server is processing” but even better you should almost instantly see the results on the console. This is achieved by using the parameter -OutVariable so that the results are captured to a variable and we can see them. We also don’t need to run a ping test, if the server is unreachable you’ll see a warning about it. If you need to log failures, simply add some logging to the catch block. If you’re unable to use PS remoting, you can still call the script block passing in the servers name to achieve the same result. A simple foreach loop could be used but for best performance/time – use PSRemoting like shown below.

      [/crayon]
    • #249398
      Participant
      Topics: 16
      Replies: 23
      Points: 111
      Rank: Participant

      Frickin eck Doug!

      May I say that you are in no doubt my PowerShell hero from hence forth lol

      Worked exactly as I needed it bang on first time mate, although tbh it is still running as there are like I mentioned quite a few servers

      I daren’t really ask for more however might I ask ;), there are a lot of retries going on still for what are probably some servers which are behind a firewall

      Do the retries stop once reached the failed state and also can I pull the failure result into a log perhaps filtering the error result by ‘failed’

       

      The whole exercise is to take this info and compare against some persistent route data I took a few days back which was a much surprisingly a much easier scripting exercise than this one, for my standards anyway.

      Some poor so and so has to compare and remove the static routes which are no longer needed, not me fortunately but I’m sure ill get pulled back into it due to the scale of servers involved

       

      Anyway, again thank you so much for your help, you guys are totally invaluable to us all!

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