【问题标题】:Powershell accessing embedded propertiesPowershell 访问嵌入式属性
【发布时间】:2017-12-14 00:57:40
【问题描述】:

我在 Powershell 中工作,一些查询的结果返回一个像这样的集合成员(请注意,这是从实际输出中缩短的):

SmsProviderObjectPath : SMS_SCI_SysResUse.FileType=2,ItemName="[\"Display=\\\\SERVER1.MYDOMAIN.ORG\\\"]MSWNET:[\"SMS_SITE=MBC\"]\\\\SERVER1.MYDOMAIN.ORG\\,SMS Distribution Point",ItemType="System 
                        Resource Usage",SiteCode="MBC"
FileType              : 2
ItemName              : ["Display=\\SERVER1.MYDOMAIN.ORG\"]MSWNET:["SMS_SITE=MBC"]\\SERVER1.MYDOMAIN.ORG\,SMS Distribution Point
ItemType              : System Resource Usage
NALPath               : ["Display=\\SERVER1.MYDOMAIN.ORG\"]MSWNET:["SMS_SITE=MBC"]\\SERVER1.MYDOMAIN.ORG\
NALType               : Windows NT Server
NetworkOSPath         : \\SERVER1.MYDOMAIN.ORG
PropLists             : {BindExcept, Protected Boundary, SourceDistributionPoints, SourceDPRanks...}
Props                 : {BITS download, Server Remote Name, PreStagingAllowed, SslState...}
RoleCount             : 2
RoleName              : SMS Distribution Point
SiteCode              : MBC
SslState              : 0
Type                  : 8
PSComputerName        : prim-serv.MYDOMAIN.org
PSShowComputerName    : False
ManagedObject         : \\prim-serv\root\sms\site_MBC:SMS_SCI_SysResUse.FileType=2,ItemName="[\"Display=\\\\SERVER1.MYDOMAIN.ORG\\\"]MSWNET:[\"SMS_SITE=MBC\"]\\\\SERVER1.MYDOMAIN.ORG\\,SMS 
                        Distribution Point",ItemType="System Resource Usage",SiteCode="MBC"
OverridingObjectClass : SMS_SCI_SysResUse
RegMultiStringLists   : {}
SecurityVerbs         : -1
ObjectClass           : SMS_SCI_SysResUse
Properties            : 
                        instance of SMS_EmbeddedProperty
                        {
                            PropertyName = "IsPXE";
                            Value = 0;
                            Value1 = "";
                            Value2 = "";
                        }, 
                        instance of SMS_EmbeddedProperty
                        {
                            PropertyName = "IsActive";
                            Value = 0;
                            Value1 = "";
                            Value2 = "";
                        }, 
                        instance of SMS_EmbeddedProperty
                        {
                            PropertyName = "IsPullDP";
                            Value = 0;
                            Value1 = "";
                            Value2 = "";
                        }, 
                        instance of SMS_EmbeddedProperty
                        {
                            PropertyName = "IsMulticast";
                            Value = 0;
                            Value1 = "";
                            Value2 = "";
                        }, 
                        instance of SMS_EmbeddedProperty
                        {
                            PropertyName = "LastIISConfigCheckTime";
                            Value = 1490896883;
                            Value1 = "";
                            Value2 = "";
                        }};
                            RoleCount = 2;
                            RoleName = "SMS Distribution Point";
                            SiteCode = "MBC";
                            SslState = 0;
                            Type = 8;
                        };

PropertyNames         : {FileType, ItemName, ItemType, NALPath...}
MethodNames           : 
MethodList            : {}
PropertyList          : {[FileType, 2], [ItemName, ["Display=\\SERVER1.MYDOMAIN.ORG\"]MSWNET:["SMS_SITE=MBC"]\\SERVER1.MYDOMAIN.ORG\,SMS Distribution Point], [ItemType, System Resource Usage], 
                        [NALPath, ["Display=\\SERVER1.MYDOMAIN.ORG\"]MSWNET:["SMS_SITE=MBC"]\\SERVER1.MYDOMAIN.ORG\]...}
UniqueIdentifier      : 16242167-3aac-4b79-ad5b-2c8030922ba5
ParentResultObject    : 
GlobalDisplayString   : 
AutoCommit            : False
AutoRefresh           : False
UserDataObject        : 
ConnectionManager     : Microsoft.ConfigurationManagement.PowerShell.Provider.CmdletWqlConnectionManager
TraceProperties       : True
NamedValueDictionary  : {[AllProviderLocations, System.Collections.Generic.List`1[System.Management.ManagementBaseObject]], [ProviderLocation, 
                        \\prim-serv\ROOT\sms:SMS_ProviderLocation.Machine="prim-serv.MYDOMAIN.org",SiteCode="MBC"], [ProviderMachineName, prim-serv.MYDOMAIN.org], [Connection, 
                        \\prim-serv.MYDOMAIN.org\root\sms\site_MBC]...}
AsyncOperationData    : 
RetainObjectLock      : False

我可以使用如下代码访问许多列出的项目,例如“NetworkOSPath”和“RoleName”:

$myDP = $DP.NetworkOSPath

我不知道如何引用属性区域中列出的项目,例如:

IsPXE、IsPullDP 以及与它们关联的值。

我可以使用以下命令获取它们的列表:$dp.EmbeddedProperties |格式列表 *

这会产生一个键和值列表:

Key   : AllowInternetClients
Value : 
        instance of SMS_EmbeddedProperty
        {
            PropertyName = "AllowInternetClients";
            Value = 0;
            Value1 = "";
            Value2 = "";
        };


Key   : BITS download
Value : 
        instance of SMS_EmbeddedProperty
        {
            PropertyName = "BITS download";
            Value = 1;
            Value1 = "";
            Value2 = "";
        };

为了只列出一个特定的键,我尝试了以下方法但没有成功:

foreach ($DP in $DPList) {$DP.EmbeddedProperties | select-object -expandproperty IsPXE }

foreach ($DP in $DPList) {$DP.EmbeddedProperties | Select-Object where Name = "IsPXE"}

foreach ($DP in $DPList) {$DP.EmbeddedProperties | Select-Object IsPXE}

有没有办法引用键及其关联值,以便我可以将它们分配给脚本内的变量?

【问题讨论】:

  • 看起来$DP$DP.Properties 都是实现IResultObject interface 的对象,而后者包含SMS_EmbeddedProperty 实例。您可以运行$DP.Properties.GetType() 来查看该属性的运行时类型吗?你试过$DP.Properties['IsPXE']$DP.Properties | ? PropertyName -eq 'IsPXE'吗?另外,在你上次的 sn-p 中,你为什么使用 EmbeddedProperties 而不是 Properties

标签: powershell


【解决方案1】:

看起来属性部分只是文本。 ($dp.EmbeddedProperties | format-list *) 的内容在添加到 $dp 时是否用引号括起来?使脚本的前面部分不会将文本转储到对象中。尽可能保持对象为对象。

我在处理这样的输出时遇到了这样的问题

get-adgroupmember -identity $name | select stuff

直到我改用更好的方法

(get-adgroupmember -identity $name).stuff

【讨论】:

    【解决方案2】:

    您与$dp.EmbeddedProperties 如此亲密。只要继续打点,你就会到达那里。获得所需的值后,您可以将其添加到对象中。这是一个例子:

    $dp = Get-CMDistributionPoint
    $NewObj = @()
    ForEach ($d in $dp)
    {
        $ServerName = $d.NetworkOSPath
        $IsPXE = $d.embeddedproperties.IsPXE.Value
        $NewObj += [pscustomObject]@{ ServerName = $ServerName; IsPXE = $IsPXE }
    }
    $NewObj | Out-GridView
    

    小心 - 一旦您通过 EmbeddedProperties 点注,您就进入了大小写敏感的怪异世界!如果你没有正确,你什么都得不到!我不确定为什么。也许它与.NET 的 System.Collections.Generic.Dictionary 类和区分大小写的字符串键有关? 浏览器

    $d.embeddedproperties.IsPXE.Value ##This works.
    $d.embeddedproperties.IsPXe.Value ##This does not work.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-12
      相关资源
      最近更新 更多