Hi, I'm a reader of the blog, but first time poster.
So I've been working a little with WMI queries, trying to stock-take our network, including which CPU's I've got. As a result I'm using it as an excuse to get better with Powershell. I created a script that queries different aspects of a server's information, and now would like to apply a sort field, with a unique option.
try { Import-Module ActiveDirectory -ErrorAction Stop} catch { Write-Warning "Failed to import Active Directory module. Exiting" return}
try {
Import-Module ActiveDirectory -ErrorAction Stop
} catch {
Write-Warning "Failed to import Active Directory module. Exiting"
return
}
$output
try { $Hypervs = Get-ADObject -Filter 'ObjectClass -eq "serviceConnectionPoint" -and Name -eq "Microsoft Hyper-V"' -ErrorAction Stop } catch { Write-Error "Failed to query active directory. More details : $_" return}foreach($Hyperv in $Hypervs) {
$Hypervs = Get-ADObject -Filter 'ObjectClass -eq "serviceConnectionPoint" -and Name -eq "Microsoft Hyper-V"' -ErrorAction Stop
Write-Error "Failed to query active directory. More details : $_"
foreach($Hyperv in $Hypervs) {
$temp = $Hyperv.DistinguishedName.split(",") $HypervDN = $temp[1..$temp.Count] -join "," $Comp = Get-ADComputer -Id $HypervDN -Prop * $isvmhost = get-vmhost -ComputerName $comp.Name if ($isvmhost -like "*vmhost") { $Comphw = Get-WmiObject win32_processor -ComputerName $comp.name #$CPU_Model = ($comphw.name -match '(?:Xeon|Core).*?(?:i|e)\d\-\d{4}.{0,1}') $OutputObj = [PScustomobject]@{ HyperVName = $Comp.Name OSVersion = $comp.operatingSystem CPU_Model = $Comphw.name } $OutputObj }
$temp = $Hyperv.DistinguishedName.split(",")
$HypervDN = $temp[1..$temp.Count] -join ","
$Comp = Get-ADComputer -Id $HypervDN -Prop *
$isvmhost = get-vmhost -ComputerName $comp.Name
if ($isvmhost -like "*vmhost")
{
$Comphw = Get-WmiObject win32_processor -ComputerName $comp.name
#$CPU_Model = ($comphw.name -match '(?:Xeon|Core).*?(?:i|e)\d\-\d{4}.{0,1}')
$OutputObj = [PScustomobject]@{
HyperVName = $Comp.Name
OSVersion = $comp.operatingSystem
CPU_Model = $Comphw.name
$OutputObj
So, the script above outputs precisely what I'm after. But as you can see, I'm using a hash table to build my output.
The problem I have is that the output itself does not come in alphabetical order. Now, of course, I could export the data to excel and manipulate it in there. But it defeats my purpose of learning more about powershell.
Output:
HyperVName OSVersion CPU_Model ---------- --------- --------- ATUIN Windows Server 2012 R2 Datacenter Intel(R) Xeon(R) CPU E5-2640 v3 @ 2.60GHz STEPUP Windows Server 2012 R2 Datacenter {Intel(R) Xeon(R) CPU X5450 @ 3.00GHz, Intel...SUPPORT Windows Server 2012 R2 Datacenter {Intel(R) Xeon(R) CPU E5320 @ 1.86GHz, Intel...GITS-TEST Windows Server 2016 Datacenter TP5 Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz FILESERVER Windows Server 2012 R2 Datacenter {Intel(R) Xeon(R) CPU E5530 @ 2.40GHz, Intel...
Ultimately, I'd like to change the output. However, if I do something like:
$OutputObj | Sort-Object -Property unique
I get the following:
HyperVName OSVersion CPU_Model ---------- --------- --------- ATUIN Windows Server 2012 R2 Datacenter Intel(R) Xeon(R) CPU E5-2640 v3 @ 2.60GHz
STEPUP Windows Server 2012 R2 Datacenter {Intel(R) Xeon(R) CPU X5450 @ 3.00GHz, Intel...
SUPPORT Windows Server 2012 R2 Datacenter {Intel(R) Xeon(R) CPU E5320 @ 1.86GHz, Intel...
GITS-TEST Windows Server 2016 Datacenter TP5 Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz
FILESERVER Windows Server 2012 R2 Datacenter {Intel(R) Xeon(R) CPU E5530 @ 2.40GHz, Intel...
BB-8 Windows Server 2012 R2 Datacenter {Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz, Intel(R) Xe...
SOL Windows Server 2012 R2 Datacenter {Intel(R) Xeon(R) CPU E5430 @ 2.66GHz, Intel...
Not very alphabetical.
So, my question is, where have I gone wrong?
when I am working with hash tables and want thing to display in a particular order (e.g. always the order I set them in the hash table, I user and ordered hash table (PowerShell 3 and above)
$OutputObj = [PScustomobject][ordered]@{HyperVName = $Comp.NameOSVersion = $comp.operatingSystemCPU_Model = $Comphw.name
to change to order, change the order of the items in the hash table
Thanks
Yep. I do this as well, but I wanted to focus my response on the sort aspect vs the hash table.
But I should have provided both. Thanks for jumping in, and picking up what I failed to add.