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



需要从Get-Package SwidTagText对象更新安装日期。该对象是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                    :

如果您尝试从 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:UsersSkuld> $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 挑选出特定的日期属性。

我的示例将只给出所有日期的列表,但如果您需要额外的信息,您可以对其进行调整。

您的代码有效,现在我创建了一个函数,用于在 SwidTagText 中搜索特定项目

真的,Powershell是神奇的

#Use 尽可能多的

细节。还不能处理数组....

#Example = 获取包名称">应用程序名称" | Where-Object { $_.提供程序名称 -eq "程序" } |佛罗里达州*

#Example = 获取包名称">应用程序名称" | Where-Object { $_.提供程序名称 -eq "msi" } |佛罗里达州*

#Returns XML SwidTagText 中的任何变量

function SwidTagText-XML-Variable ( [string] $PackageName , [string] $XMLName, [string] $PackageType ) 
{
    $PackageObject = Get-Package -name $PackageName |  Where-Object { $_.ProviderName -eq $PackageType }
    
    $xml= $PackageObject.SwidTagText
     
        foreach ($item in $xml)
            {
            $XMLValue = $(
                $(
                    [xml]$item | Select-Object "InnerXml"
                ).InnerXml | Select-Xml -XPath "//*[@$XMLName]"
            ).Node.$XMLName
            }
    
    return $XMLValue
}
#Example of Ubisoft Program and check for UninstallString
SwidTagText-XML-Variable "*ubisoft*" "UninstallString" "Programs"

因此,该函数将在 SwidTagText 中搜索一个 XML 项。解析和从中获取 XML 项非常有用

最新更新