Get-Package命令包含xml字符串-需要转换为PSObject

时间:2019-07-09 21:34:53

标签: powershell xml-parsing powershell-v5.1

需要Get-Package SwidTagText对象的Windows Update安装日期。该对象为XML格式,我尝试转换的所有内容均不起作用。

我正试图从WMI切换,因为它的回退速度非常慢。

尝试了ConvertFrom-XML函数。也尝试过ConvertFrom-String

Get-Package -ProviderName msu | Select-Object *
PropertyOfSoftwareIdentity : PropertyOfSoftwareIdentity
FastPackageReference       : Definition Update for Windows Defender Antivirus - KB2267602 (Definition 1.297.486.0)
ProviderName               : msu
Source                     : 
Status                     : Installed
SearchKey                  : 
FullPath                   : ?
PackageFilename            : ?
FromTrustedSource          : False
Summary                    : Install this update to revise the definition files that are used to detect viruses, spyware, and other potentially 
                             unwanted software. Once you have installed this item, it cannot be removed.
SwidTags                   : {Definition Update for Windows Defender Antivirus - KB2267602 (Definition 1.297.486.0)}
CanonicalId                : msu:Definition Update for Windows Defender Antivirus - KB2267602 (Definition 1.297.486.0)
Metadata                   : {summary,SupportUrl,Date,ResultCode}
SwidTagText                : <?xml version="1.0" encoding="utf-16" standalone="yes"?>
                             <SoftwareIdentity
                               name="Definition Update for Windows Defender Antivirus - KB2267602 (Definition 1.297.486.0)" 
                             xmlns="http://standards.iso.org/iso/19770/-2/2015/schema.xsd">
                               <Meta
                                 summary="Install this update to revise the definition files that are used to detect viruses, spyware, and other 
                             potentially unwanted software. Once you have installed this item, it cannot be removed."
                                 SupportUrl="https://go.microsoft.com/fwlink/?LinkId=52661"
                                 Date="7/5/2019 6:17:09 PM"
                                 ResultCode="2" />
                             </SoftwareIdentity>
Dependencies               : {}
IsCorpus                   : 
Name                       : Definition Update for Windows Defender Antivirus - KB2267602 (Definition 1.297.486.0)
Version                    :

1 个答案:

答案 0 :(得分:0)

如果您尝试从XML中获取原始日期,则可以执行以下操作:

$xml = ((Get-Package -ProviderName msu) | Select-Object *).SwidTagText
foreach ($item in $xml)
    {
    $(
        $(
            [xml]$item | Select-Object "InnerXml"
        ).InnerXml | Select-Xml -XPath "//*[@Date]"
    ).Node.Date
    }

这使您可以像这样使用每个条目:

PS C:\Users\Skuld> $xml[0]
<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<SoftwareIdentity
  name="Update for Windows Defender Antivirus antimalware platform - KB4052623 
(Version 4.18.1906.3)" xmlns="http://standards.iso.org/iso/19770/-2/2015/schema.xsd">
  <Meta
   summary="This package will update Windows Defender Antivirus antimalware 
platform’s components on the user machine."
    SupportUrl="https://go.microsoft.com/fwlink/?linkid=862339"
    Date="09/07/2019 10:46:52"
    ResultCode="2" />
</SoftwareIdentity>

然后使用Select-XML / XPath来选择特定的日期属性。

我的示例仅列出了所有日期,但是如果需要更多信息,可以进行调整。