locked
Combining output RRS feed

  • Question

  • I am sure this is a basic question, but I seem to be struggling with the concept. Can someone show me how to combine output?

    Say I have a list of computers that I want to see the DNS settings for. I have hundreds of them and I want to create a table that shows me the DNS settings on each server.

    $strComputer = "Server1","Server2","Server3"
    $colItems = Get-wmiobject -class "Win32_NetworkAdapterConfiguration" `
        -computername $strComputer | Where{$_.IpEnabled -Match "True"}

    This part is easy, but if I make the list a lot longer, I can't tell easily which computer name goes with which output. All I want to do is add the computer name to each line of the output. Eventually I will dump it to a CSV. Here is what I tried...

    foreach ($objItem in $colItems) {
       write-host "Computer Name : " $strComputer
       write-host "MAC Address : " $objItem.MACAddress
       write-host "IPAddress : " $objItem.IPAddress
       write-host "IPAddress : " $objItem.IPEnabled
       write-host "DNS Servers : " $objItem.DNSServerSearchOrder
       Write-host ""
    }

    I get that this is wrong, but I don't see how I can add a column to $colItems with the computer name, and I can't use $strComputer later because it returns the whole array and not just the name of the server for the row I am displaying. Can someone help me understand the right method to display a single table with the Network configuration and system name together in one row for each server?

    Thursday, August 12, 2010 6:39 PM

Answers

  • Okay, this should work better.

    It's basically initializing an empty array  - $result = @(), then iterating through the server list and retrieving all the enabled IP adapter objects. Then for each adapter object, adding a custom property with the server name, then adding that object to $result.

    When we're done, we have an array of custom objects.

     

    $strComputers = "server1","server2","server3"
    $result = @()

    foreach ($strComputer in $strComputers){
        gwmi Win32_NetworkAdapterConfiguration -computername $strComputer |
         Where {$_.IpEnabled -match "True"} |
            foreach-object {
                $_ | add-member -membertype noteproperty -Name "Computer" -value $strComputer
                $result += $_
                }
            }

    $result | select computer,dhcpenabled,ipaddress,defaultipgateway,dnsdomain | ft -auto


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by Oldguard Friday, August 13, 2010 8:34 PM
    Friday, August 13, 2010 8:28 PM

All replies

  • How about:

    $strComputers = "Server1","Server2","Server3"

    $result = @()

    foreach ($strComputer in $strComputers){


    $colItems = Get-wmiobject -class "Win32_NetworkAdapterConfiguration" `
        -computername $strComputer | Where{$_.IpEnabled -Match "True"} | foreach-object {$_ | add-member -membertype noteproperty -Name "Computer" -value $strComputer}

    $result += $colItems

    }

    $result


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Proposed as answer by Marco Shaw Thursday, August 12, 2010 11:15 PM
    Thursday, August 12, 2010 7:09 PM
  • Hi,

    To save the result to a CSV file, try the code below:

    $strComputers = "Server1","Server2","Server3"

    add-content DNS.csv "ComputerName,IPAddress" # add other content based on your need

    foreach ($strComputer in $strComputers){

    $colItems = Get-wmiobject -class "Win32_NetworkAdapterConfiguration" -computername $strComputer | Where{$_.IpEnabled -Match "True"}

    If ($colItems.gettype().isarray)
    {
    foreach($item in $colItems){

    add-content D:\Test\1.txt "$strComputer,$($Item.IPAddress),$($Item.DefaultIPGateway)" # add other content based on your need
    }
    }else{

    add-content D:\Test\1.txt "$strComputer,$($colItems.IPAddress)"} # add other content based on your need

    }

    Thanks.


    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Friday, August 13, 2010 3:58 AM
  •  

    The following section didn't work for some reason... Seems like when this part of the line is in, I get no output. When I take this part out, I get the data I would expect to see. It appears I am stuck at the same point...

     | foreach-object {$_ | add-member -membertype noteproperty -Name "Computer" -value $strComputer}

    Friday, August 13, 2010 7:27 PM
  • I understand that what you are showing me is a way to get the data into a file, but I am really trying to understand arrays. It seems to me that Add-Member is suppose to do this, but my understanding of how to work with arrays might be the real issue... To much VBSCRIPT in my head and not enough PowerShell. Doing this works around my issue instead of addressing it. At times I may want to combine a number of returned fields from different queries for a single system. Maybe I want the data to go to the screen, or excel, or a file, but the real challenge is to figure out how to create an array of data for a single system combined from multiple sources. I think understanding array construction is going to make future needs easier to meet...

    Can't believe this is this difficult...

    Friday, August 13, 2010 7:34 PM
  • Okay, this should work better.

    It's basically initializing an empty array  - $result = @(), then iterating through the server list and retrieving all the enabled IP adapter objects. Then for each adapter object, adding a custom property with the server name, then adding that object to $result.

    When we're done, we have an array of custom objects.

     

    $strComputers = "server1","server2","server3"
    $result = @()

    foreach ($strComputer in $strComputers){
        gwmi Win32_NetworkAdapterConfiguration -computername $strComputer |
         Where {$_.IpEnabled -match "True"} |
            foreach-object {
                $_ | add-member -membertype noteproperty -Name "Computer" -value $strComputer
                $result += $_
                }
            }

    $result | select computer,dhcpenabled,ipaddress,defaultipgateway,dnsdomain | ft -auto


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by Oldguard Friday, August 13, 2010 8:34 PM
    Friday, August 13, 2010 8:28 PM