Powershell访问嵌入的属性

时间:2017-04-11 18:19:37

标签: powershell

我在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 | format-list *

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

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}

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

2 个答案:

答案 0 :(得分:0)

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

使用像这样的输出时,我遇到了类似的问题

get-adgroupmember -identity $name | select stuff

直到我切换到更好的方法

(get-adgroupmember -identity $name).stuff

答案 1 :(得分:-1)

你与$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,你就会进入Case-Sensitivity怪诞的世界!如果你没有正确的话,你就什么都没有!我不确定为什么。也许它与.NET的System.Collections.Generic.Dictionary类和区分大小写的字符串键有关? IE

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