1

I want to build a "~table" that holds information about various computer (e.g. Name, IP Address and MAC Address though more information could be added later). Here is what I have so far:

$Computers = "Server1", "Server2", "Server" #Eventually will get from file
$a = $Computers.length

$User = Read-Host "Enter Username"
$Cred = Get-Credential $User
cls

$ComputerInfo = @{NAME = 1..$a; IP_ADDR = 1..$a; MAC_ADDR = 1..$a}

$i = 0
ForEach ($Computer in $Computers) {
   $wmiquery = get-wmiobject win32_networkadapterconfiguration -computer $Computer -filter "IPEnabled='True'" -Credential $Cred

   $ComputerInfo.NAME[$i] = $Computer
   $ComputerInfo.IP_ADDR[$i] = $wmiquery.IPAddress[0]
   $ComputerInfo.MAC_ADDR[$i] = $wmiquery.MACAddress

   $i++
}

The code above provides me with the information I want. However, I'm having trouble exporting to csv file / displaying in table. If I simply type: $ComputerInfo. I get:

Name Value

MAC_ADDR {MAC1, MAC2, MAC3}

IP_ADDR {IP1, IP2, IP3}

NAME {Server1, Server2, Server3}

Not exactly what I'm looking for. I want something more like:

NAME IP_ADDR MAC_ADDR

[value] [value] [value]

[value] [value] [value]

[value] [value] [value]

If I export $ComputerInfo to CSV file it gets even goofier. I get

TYPE System.Collections.Hashtable "IsReadOnly","IsFixedSize","IsSynchronized","Keys","Values","SyncRoot","Count" "False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","3"

1

Overall I would make the following adjustments below. A few things I changed around:

  1. change your credential prompt to one line since Get-Credential can provide a message
  2. Created the table as a psobject, it makes it much easier to work with and referencing columns to set values.
  3. In using the psobject it removes the need to index into the array (e.g. using column[0])
  4. I added in testing if the computer was on the network before you try connecting to get information (just good practice).

When you want to add more columns to your table just expand the $props variable with your columns, and populate it in your foreach loop.

Output of this script just with my local computer: enter image description here enter image description here

Then to just show what it will output if I just ConvertTo-Csv: enter image description here

$UserCred = (Get-Credential -Message "Enter username")

$props = [ordered]@{NAME="";IP_ADDR="";MAC_ADDR=""}
$ComputerInfo = New-Object psobject -property $props

ForEach ($Computer in $Computers) {
    if (Test-Connection $computer) {
        $wmiquery = get-wmiobject win32_networkadapterconfiguration -computer $Computer -filter "IPEnabled='True'" -Credential $Cred
        $ComputerInfo.NAME = $computer
        $ComputerInfo.IP_ADDR = $wmiquery.IPAddress[0]
        $ComputerInfo.MAC_ADDR = $wmiquery.MACAddress
    }
    else{
        Write-Warning "$($computer) not found on the network"
    }
}

$ComputerInfo

EDIT

One thing to just add that you will see a difference with your $computerinfo versus mine above is the TypeName. If you pipe it to Get-Member your object will return as System.Collections.Hashtable, where mine is going to return as a System.Management.Automation.PSCustomObject. The PSCustomObject will return each column in your table as a NoteProperty type that you can use in your script as you build onto it (e.g. querying the domain for Computer information).

| improve this answer | |
  • Your solutions does provide me the information I want in the desired format but now $ComputerInfo only holds information on a single computer even when $ComputerInfo contains multiple computers. I will tweak a bit and see what I can come up with. – Delonte Johnson Aug 19 '15 at 13:24
  • I have modified your code slightly do what I need it to do. Also, accepted your answer as solution. – Delonte Johnson Aug 19 '15 at 13:41
0

Code below does what I need it to do. Slightly modified version of code provided by Shawn Melton. Changed $ComputerInfo to array, ForEach updated to add data into $ComputerInfo array.

$Computers = "Server1", "Server2, Server3"
$a = $Computers.length
$Cred = (Get-Credential)
$i=0
$ComputerInfo = 1..$a
$props = @{NAME = ""; IP_ADDR = ""; MAC_ADDR = ""}

ForEach ($Computer in $Computers) {

    $ComputerInfo[$i] = New-Object psobject -property $props
    $wmiquery = get-wmiobject win32_networkadapterconfiguration -computer $Computer -filter "IPEnabled='True'" -Credential $Cred
    $ComputerInfo[$i].NAME = $computer
    $ComputerInfo[$i].IP_ADDR = $wmiquery.IPAddress[0]
    $ComputerInfo[$i].MAC_ADDR = $wmiquery.MACAddress
    $i++
}

$ComputerInfo
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.