powershell - Creating a 2 dimentional system array FROM SCRATCH -


i fall trap. latest example. have code extracts versions of sccm2007 installed components in system array:

$envcomputername = $env:computername [system.management.managementobject]$agentguid = get-wmiobject -computer $envcomputername -namespace root\ccm -class ccm_client -erroraction stop [system.management.managementobject]$scansource = get-wmiobject -computer $envcomputername -namespace root\ccm\softwareupdates\wuahandler -class ccm_updatesource -erroraction stop [system.array]$installedcomponents = get-wmiobject -computer $envcomputername -namespace root\ccm -class ccm_installedcomponent -erroraction stop $highestcomponentver = $installedcomponents | sort version | select version -last 1 [array]$myinstalledcomponents = $installedcomponents | sort displayname | select displayname,name,version 

what neat system array columns labelled.

#displayname                                                  name                                                         version       #-----------                                                  ----                                                         -------       #ccm framework                                                ccmframework                                                 4.0.6487.2196 #ccm policy agent                                             ccmpolicyagent                                               4.0.6487.2000 #ccm status agent                                             ccmstatusagent                                               4.0.6487.2000 #sms client core components                                   smsclient                                                    4.0.6487.2000 #sms operating system deployment                              smsosdeployment                                              4.0.6487.2000 #sms outofband management agent                               smsoutofbandmanagement                                       4.0.6487.2000 #sms power management agent                                   smspowermanagement                                           4.0.6487.2157 #sms remote control agent                                     smsremotetools                                               4.0.6487.2000 #sms shared components                                        smscommon                                                    4.0.6487.2000 #sms software distribution agent                              smssoftwaredistribution                                      4.0.6487.2160 #sms software metering agent                                  smssoftwaremetering                                          4.0.6487.2000 #sms software updates agent                                   smssoftwareupdate                                            4.0.6487.2188 #sms source list update agent                                 smssourceupdateagent                                         4.0.6487.2000 #sms task sequence                                            smstasksequence                                              4.0.6487.2179 

heck, works: $myinstalledcomponents.version

i want compare versions , need create system array compare against. best can this:

$sccm2007componentminversions =  @("ccm framework","ccmframework","4.0.6487.2196"), ("ccm policy agent","ccmpolicyagent","4.0.6487.2000"), ("ccm status agent","ccmstatusagent","4.0.6487.2000"), ("sms client core components","smsclient","4.0.6487.2000"), ("sms inventory agent","smsinventory","4.0.6487.2184"), ("sms operating system deployment","smsosdeployment","4.0.6487.2000"), ("sms outofband management agent","smsoutofbandmanagement","4.0.6487.2000"), ("sms power management agent","smspowermanagement","4.0.6487.2157"), ("sms remote control agent","smsremotetools","4.0.6487.2000"), ("sms shared components","smscommon","4.0.6487.2000"), ("sms software distribution agent","smssoftwaredistribution","4.0.6487.2160"), ("sms software metering agent","smssoftwaremetering","4.0.6487.2000"), ("sms software updates agent","smssoftwareupdate","4.0.6487.2188"), ("sms source list update agent","smssourceupdateagent","4.0.6487.2000"), ("sms task sequence","smstasksequence","4.0.6487.2179") 

but system array of system arrays. , don't labels columns either.

is there way create scratch or labels when powershell command or wmi returns array? , if so, why? proper way this?

the result wmi-query array of objects have properties displayname, name , version. that's why have columns. make similar array minimum compnent versions, use:

$sccm2007componentminversions = @' "displayname","name","version" "ccm framework","ccmframework","4.0.6487.2196" "ccm policy agent","ccmpolicyagent","4.0.6487.2000" "ccm status agent","ccmstatusagent","4.0.6487.2000" "sms client core components","smsclient","4.0.6487.2000" "sms inventory agent","smsinventory","4.0.6487.2184" "sms operating system deployment","smsosdeployment","4.0.6487.2000" "sms outofband management agent","smsoutofbandmanagement","4.0.6487.2000" "sms power management agent","smspowermanagement","4.0.6487.2157" "sms remote control agent","smsremotetools","4.0.6487.2000" "sms shared components","smscommon","4.0.6487.2000" "sms software distribution agent","smssoftwaredistribution","4.0.6487.2160" "sms software metering agent","smssoftwaremetering","4.0.6487.2000" "sms software updates agent","smssoftwareupdate","4.0.6487.2188" "sms source list update agent","smssourceupdateagent","4.0.6487.2000" "sms task sequence","smstasksequence","4.0.6487.2179" '@ | convertfrom-csv | select-object displayname, name, @{n="version";e={ $_.version -as [version] }} 

sample output:

ps c:\users\frode> $sccm2007componentminversions  displayname                     name                    version       -----------                     ----                    -------       ccm framework                   ccmframework            4.0.6487.2196 ccm policy agent                ccmpolicyagent          4.0.6487.2000 ccm status agent                ccmstatusagent          4.0.6487.2000 

this converts version version-object can compared other version-objects (you need convert string-valus ccm_installedcomponents, see sample below). personally, use hashtable , not afor minimumversions-list it's easier compare against after. ex:

#create hashtable minimumversions $sccm2007componentminversions = @{}  #fill hashtable csv-data @' "displayname","name","version" "ccm framework","ccmframework","4.0.6487.2196" "ccm policy agent","ccmpolicyagent","4.0.6487.2000" "ccm status agent","ccmstatusagent","4.0.6487.2000" "sms client core components","smsclient","4.0.6487.2000" "sms inventory agent","smsinventory","4.0.6487.2184" "sms operating system deployment","smsosdeployment","4.0.6487.2000" "sms outofband management agent","smsoutofbandmanagement","4.0.6487.2000" "sms power management agent","smspowermanagement","4.0.6487.2157" "sms remote control agent","smsremotetools","4.0.6487.2000" "sms shared components","smscommon","4.0.6487.2000" "sms software distribution agent","smssoftwaredistribution","4.0.6487.2160" "sms software metering agent","smssoftwaremetering","4.0.6487.2000" "sms software updates agent","smssoftwareupdate","4.0.6487.2188" "sms source list update agent","smssourceupdateagent","4.0.6487.2000" "sms task sequence","smstasksequence","4.0.6487.2179" '@ | convertfrom-csv | foreach-object { $sccm2007componentminversions.add($_.name, ($_.version -as [version])) }  #sample wmi-output $installedcomponents = @' "displayname","name","version" "ccm framework","ccmframework","4.0.6487.2195" "ccm policy agent","ccmpolicyagent","4.0.6487.2000" '@ | convertfrom-csv    #find old components $installedcomponents | where-object { ($_.version -as [version]) -lt $sccm2007componentminversions[$_.name] } 

output:

displayname   name         version -----------   ----         ------- ccm framework ccmframework 4.0.6487.2195 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -