我总是陷入这个陷阱。这只是最新的例子。 我有这段代码在系统数组中提取SCCM2007已安装组件的版本:
$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
这个系统阵列真正的优点是列标记为。
#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
哎呀,即使这样也有效:$MyInstalledComponents.version
我想比较版本并需要创建另一个系统数组来进行比较。我能做的最好的就是:
$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")
但这是系统阵列的System数组。我也没有获得我的专栏标签。
有没有办法从头创建这个或者只在PowerShell命令或WMI返回数组时才获得标签?如果是这样,为什么? 这样做的正确方法是什么?
答案 0 :(得分:2)
WMI查询的结果是一个对象数组,它们都具有属性DisplayName
,Name
和Version
。这就是你拥有专栏的原因。为了快速为您制作类似的数组,我会使用:
$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] }}
示例输出:
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
这也将Version
转换为Version对象,可以轻松地将其与其他版本对象进行比较(您需要从CCM_InstalledComponents
转换String-valus,请参阅下面的示例)。但就个人而言,我会使用散列表,而不是你的最小转换列表,因为它更容易与之比较。例如:
#Create hashtable for minimumversions
$SCCM2007ComponentMinVersions = @{}
#Fill hashtable with 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] }
输出:
DisplayName Name Version
----------- ---- -------
CCM Framework CcmFramework 4.0.6487.2195
答案 1 :(得分:1)
您的调用返回的值不是二维数组,它是具有属性的对象数组,这就是您获取列名并使用点表示法的原因。
您可以从[PSObject]
创建[hashtable]
并拥有一组数据:
$SCCM2007ComponentMinVersions = @(
@{
DisplayName = "CCM Framework"
Name = "CcmFramework"
Version = "4.0.6487.2196"
},
@{
DisplayName = "CCM Policy Agent"
Name = "CcmPolicyAgent"
Version = "4.0.6487.2000"
}
)
这会产生一个[hashtable]
的数组,这可能已经足够了。制作[PSObjects]
:
$SCCM2007ComponentMinVersions = @(
[PSCustomObject]@{
DisplayName = "CCM Framework"
Name = "CcmFramework"
Version = "4.0.6487.2196"
},
[PSCustomObject]@{
DisplayName = "CCM Policy Agent"
Name = "CcmPolicyAgent"
Version = "4.0.6487.2000"
}
)
答案 2 :(得分:0)
最好的办法是制作一个具有命名属性的对象数组。
$var = @()
制作数组$var = @{Key="Value"}
制作哈希表$var = new-object psobject -property $hashtable
创建一个具有与给定哈希表相对应的命名属性的新对象考虑到这一点,请查看:
$SCCM2007ComponentMinVersions = @(
(new-object psobject -property @{DisplayName="CCM Framework";Name="CcmFramework";Version="4.0.6487.2196"}),
(new-object psobject -property @{DisplayName="CCM Policy Agent";Name="CcmPolicyAgent";Version="4.0.6487.2000"})
)
这应该会为您提供一系列可以很好地操作的对象。
PS H:\> $SCCM2007ComponentMinVersions
DisplayName Version Name
----------- ------- ----
CCM Framework 4.0.6487.2196 CcmFramework
CCM Policy Agent 4.0.6487.2000 CcmPolicyAgent
作为一个额外的好处,这种类型的数据结构在通过管道传输到Export-CSV
(或ConvertTo-Csv
,如果您只想在控制台中测试它)和类似的显示表格数据的方法时效果很好。