Lately, I've been playing around with custom objects in PowerShell. I'm getting the hang of it except I don't understand why PowerShell displays my objects in a seemingly random order. For example
$i = 0; $a = @()
$Disks = Get-WMIObject -Class Win32_LogicalDisk
ForEach($Disk in $Disks){
$props = @{'Disk' = $Disks.DeviceID[$i];
'Size GB' = "{0:F1}" -f ($Disks.size[$i] / 1GB)}
$a += New-Object psobject -Property $props
$i++
}
$a
Output is shown as 'Size GB' then 'Disk'. I want 'Disk' then 'Size GB'. Example 2:
$Computers = 'Server1', 'Server2', 'Server3'
$ComputerInfo = 1..$Computers.Length
$props = @{NAME = ""; MAC_ADDR = ""; IP_ADDR = "" }
$Cred = Get-Credential -Message "Enter Credentials"
$i=0
#Gather requested information for each $computer in $computers
ForEach ($Computer in $Computers) {
Write-Host "Gathering information on $computer..."
$ComputerInfo[$i] = New-Object psobject -property $props
Try {
$wmiquery = get-wmiobject win32_networkadapterconfiguration -computer $Computer -filter "IPEnabled='True'" -Credential $Cred -ErrorAction SilentlyContinue
$ComputerInfo[$i].NAME = $computer
$ComputerInfo[$i].MAC_ADDR = $wmiquery.MACAddress
$ComputerInfo[$i].IP_ADDR = $wmiquery.IPAddress[0]
} Catch {
Write-Host "Unable to query: $computer"
}
$i++
}
#Display information
$ComputerInfo
Output is shown as 'MAC_ADDR' then 'IP_ADDR' then 'NAME'. I want 'NAME' then 'MAC_ADDR' then 'IP_ADDR'.