Today on PowerShell.com, I helped out a new user to PowerShell extract information from his remote clients. Two things that struck me.
1. He needed to use the legacy Get-WMIObject cmdlet. That means no PowerShell remoting, which is what I prefer.
2. He needed to be able to write the information to a CSV file. Normally there is nothing wrong with that except that one piece of information had the potential to return multiple MAC addresses. A CSV could not handle that on its own so I had to write up some simple code to address this data and put it in a form that Export-CSV could utilize.
First off, the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| Function Get-ComputerInfo { [CmdletBinding()] Param ( [parameter(Mandatory=$true, ValueFromPipeline=$true)] [String[]] $ComputerName )
BEGIN { # Creates a custome object that can be easily modified. # Since you want a CSV file, this will create the maximum # opportunities for reuse of the data. You can see how to # add memory for more adapters. Also look in the # SWITCH statement if you add more adapters. Function New-ComputerObject { $Obj = New-Object -TypeName psobject -Property @{ 'ComputerName' = $computer 'Model' = $cs.model 'Serial' = $ser.serialnumber 'User' = $cs.username 'Online' = $False 'TotalNICAdapters' = 0 'MAC0' = $Null 'MAC1' = $Null 'MAC2' = $Null 'MAC3' = $Null 'MAC4' = $Null 'MAC5' = $Null 'MAC6' = $Null 'MAC7' = $Null 'MAC8' = $Null 'MAC9' = $Null 'MAC10' = $Null 'MAC11' = $Null 'MAC12' = $Null 'MAC13' = $Null 'MAC14' = $Null 'MAC15' = $Null
}
# Send the Object to the calling statement. Write-Output $Obj }
} # END: BEGIN BLOCK
PROCESS {
# Grab a copy of the custom object. $CO = New-ComputerObject ForEach($C in $ComputerName) {
# Place a fresh compy of the custom object in memory. $Obj = $CO
# Assign the computer name. $Obj.ComputerName = $c
Try { # If you cannot connect to the client, it will still record the attempt # to connect and an online status of FALSE. $cs = GWMI -Class Win32_computerSystem -Property Model,UserName -ComputerName $C -ErrorAction Stop $ser = GWMI -Class Win32_BIOS -Property SerialNumber -ComputerName $C $mac = GWMI -Class Win32_NetworkAdapterConfiguration -Property MACaddress -ComputerName $C
$Obj.Model = $CS.Model $Obj.Serial = $Ser.SerialNumber $Obj.User = $CS.UserName $Obj.Online = $True $Obj.TotalNICAdapters = $Mac.Count
# Now the tricky part. Handling an unknown number of MAC addresses. # Normally, I would not go this route. Hopefully this will produce. # What you want.
$Count = 1 ForEach ($M in $Mac) { Switch ($Count) { 0 {$Obj.MAC0 = $M.MACAddress} 1 {$Obj.MAC1 = $M.MACAddress} 2 {$Obj.MAC2 = $M.MACAddress} 3 {$Obj.MAC3 = $M.MACAddress} 4 {$Obj.MAC4 = $M.MACAddress} 5 {$Obj.MAC5 = $M.MACAddress} 6 {$Obj.MAC6 = $M.MACAddress} 7 {$Obj.MAC7 = $M.MACAddress} 8 {$Obj.MAC8 = $M.MACAddress} 9 {$Obj.MAC9 = $M.MACAddress} 10 {$Obj.MAC10 = $M.MACAddress} 11 {$Obj.MAC11 = $M.MACAddress} 12 {$Obj.MAC12 = $M.MACAddress} 13 {$Obj.MAC13 = $M.MACAddress} 14 {$Obj.MAC14 = $M.MACAddress} 15 {$Obj.MAC15 = $M.MACAddress} }
$Count++
}
} Catch { Write-Host "Cannot contact $C" -ForegroundColor Red -BackgroundColor DarkRed }
# Send the data to the pipeline. Write-Output $Obj } } # END: PROCESS BLOCK END {} # END: END BLOCK
<# .SYNOPSIS Recovers information from a remote client.
.DESCRIPTION Recovers information from a remote client using the GET-WMIObject cmdlets.
.PARAMETER ComputerName The name of the clients to recover information from.
.EXAMPLE Get-ComputerInfo -ComputerName SVR1, Cl1 | Export-Csv -Path c:\temp\data.csv
Gets the computer information from svr1 and cl1 and sends it to a CSV file.
.EXAMPLE Get-Content ComputerNames.txt | Get-ComputerInfo
Uses a text file called ComputerNames.txt to supply a list of computer to recover information from.
.EXAMPLE Get-ADComputer -filter * | Select-Object -ExpandProperty name | Get-ComputerInfo
Uses the Windows PowerShell ActiveDirectory module to get a list of all computer names listed in active directory. It then attempts to gather information from each client.
You must have the ActiveDirectory module for Windows PowerShell available for this method to work.
.NOTES =============================================================================== == Cmdlet: Get-ComputerInfo == == Author: Jason A. Yoder == == Company: MCTExpert of Arizona == == Date: February 20, 2015 == == Copyright: All rights reserved. == == Version: 1.0.0.0 == == Legal: The user assumes all responsibility and liability for the usage of == == this PowerShell code. MCTExpert of Arizona, Its officers, shareholders, == == owners, and their relatives are not liable for any damages. As with all == == code, review it and understand it prior to usage. It is recommended that == == this code be fully tested and validated in a test environment prior to == == usage in a production environment. == == == == Does this code make changes: NO == =============================================================================== #>
} # END: Function Get-ComputerInfo |
To make the CSV requirement work, I had to add a property to the output object for each potential network adapter. You can see those properties as MAC1, MAC2, etc… Also take a look at the Switch statement starting on line 89. I am sure that I could have found a unique way to code that one better. Hey, it was free help. Anyway, I instructed the user to add code if they needed to go beyond 16 NICs in these two areas. Looking forward to his feedback.
Comments