从powershell中的管道属性中获取价值

时间:2015-06-16 15:20:53

标签: arrays powershell powershell-v2.0

有谁可以告诉我为什么我能够使用以下命令获取网站的价值而不是WebServer?

$ DeploySetting.setting如下:

命名-------------值

网站---------- XXXXX

Web服务器------ ServXX

Web服务器------ ServYY

$script:webSite = ($DeploySetting.setting | Where-Object {$_.name -eq  "Website"}).value
$script:webServers = ($DeploySetting.setting | Where-Object {$_.name -eq "WebServer"}).value

是因为WebServer的$DeploySetting.setting中有两个值吗?这不应该只是作为字符串数组存储在varibale中吗?

Powershell版本:

CLRVersion 2.0.50727.5485
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0,2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1

很抱歉,如果它很简单,那就是PowerShell的新功能。

谢谢

$ deploySetting.setting |的内容Get-Member

TypeName: System.Xml.XmlElement

Name                 MemberType            Definition                                                                                                                                                      
----                 ----------            ----------                                                                                                                                                      
ToString             CodeMethod            static string XmlNode(psobject instance)                                                                                                                        
AppendChild          Method                System.Xml.XmlNode AppendChild(System.Xml.XmlNode newChild)                                                                                                     
Clone                Method                System.Xml.XmlNode Clone()                                                                                                                                      
CloneNode            Method                System.Xml.XmlNode CloneNode(bool deep)                                                                                                                         
CreateNavigator      Method                System.Xml.XPath.XPathNavigator CreateNavigator()                                                                                                               
Equals               Method                bool Equals(System.Object obj)                                                                                                                                  
GetAttribute         Method                string GetAttribute(string name), string GetAttribute(string localName, string namespaceURI)                                                                    
GetAttributeNode     Method                System.Xml.XmlAttribute GetAttributeNode(string name), System.Xml.XmlAttribute GetAttributeNode(string localName, string namespaceURI)                          
GetElementsByTagName Method                System.Xml.XmlNodeList GetElementsByTagName(string name), System.Xml.XmlNodeList GetElementsByTagName(string localName, string namespaceURI)                    
GetEnumerator        Method                System.Collections.IEnumerator GetEnumerator()                                                                                                                  
GetHashCode          Method                int GetHashCode()                                                                                                                                               
GetNamespaceOfPrefix Method                string GetNamespaceOfPrefix(string prefix)                                                                                                                      
GetPrefixOfNamespace Method                string GetPrefixOfNamespace(string namespaceURI)                                                                                                                
GetType              Method                type GetType()                                                                                                                                                  
HasAttribute         Method                bool HasAttribute(string name), bool HasAttribute(string localName, string namespaceURI)                                                                        
InsertAfter          Method                System.Xml.XmlNode InsertAfter(System.Xml.XmlNode newChild, System.Xml.XmlNode refChild)                                                                        
InsertBefore         Method                System.Xml.XmlNode InsertBefore(System.Xml.XmlNode newChild, System.Xml.XmlNode refChild)                                                                       
Normalize            Method                System.Void Normalize()                                                                                                                                         
PrependChild         Method                System.Xml.XmlNode PrependChild(System.Xml.XmlNode newChild)                                                                                                    
RemoveAll            Method                System.Void RemoveAll()                                                                                                                                         
RemoveAllAttributes  Method                System.Void RemoveAllAttributes()                                                                                                                               
RemoveAttribute      Method                System.Void RemoveAttribute(string name), System.Void RemoveAttribute(string localName, string namespaceURI)                                                    
RemoveAttributeAt    Method                System.Xml.XmlNode RemoveAttributeAt(int i)                                                                                                                     
RemoveAttributeNode  Method                System.Xml.XmlAttribute RemoveAttributeNode(System.Xml.XmlAttribute oldAttr), System.Xml.XmlAttribute RemoveAttributeNode(string localName, string namespaceURI)
RemoveChild          Method                System.Xml.XmlNode RemoveChild(System.Xml.XmlNode oldChild)                                                                                                     
ReplaceChild         Method                System.Xml.XmlNode ReplaceChild(System.Xml.XmlNode newChild, System.Xml.XmlNode oldChild)                                                                       
SelectNodes          Method                System.Xml.XmlNodeList SelectNodes(string xpath), System.Xml.XmlNodeList SelectNodes(string xpath, System.Xml.XmlNamespaceManager nsmgr)                        
SelectSingleNode     Method                System.Xml.XmlNode SelectSingleNode(string xpath), System.Xml.XmlNode SelectSingleNode(string xpath, System.Xml.XmlNamespaceManager nsmgr)                      
SetAttribute         Method                System.Void SetAttribute(string name, string value), string SetAttribute(string localName, string namespaceURI, string value)                                   
SetAttributeNode     Method                System.Xml.XmlAttribute SetAttributeNode(System.Xml.XmlAttribute newAttr), System.Xml.XmlAttribute SetAttributeNode(string localName, string namespaceURI)      
Supports             Method                bool Supports(string feature, string version)                                                                                                                   
WriteContentTo       Method                System.Void WriteContentTo(System.Xml.XmlWriter w)                                                                                                              
WriteTo              Method                System.Void WriteTo(System.Xml.XmlWriter w)                                                                                                                     
Item                 ParameterizedProperty System.Xml.XmlElement Item(string name) {get;}, System.Xml.XmlElement Item(string localname, string ns) {get;}                                                  
name                 Property              System.String name {get;set;}                                                                                                                                   
value                Property              System.String value {get;set;}

2 个答案:

答案 0 :(得分:1)

你问的是new feature of PowerShell V3

  

会员枚举

     

首先,我要描述一个没有正式名称的小功能,直到我开始撰写这篇博文。我也开始使用这个功能,因为当我们最近向PowerShell MVP演示时,这对我来说是一个惊喜(对我而言)。

     

如果您发现自己经常将一个集合传递给ForEach-Object或Select-Object(或者更可能是别名%或select)以获得单个属性,那么您会喜欢这个功能。

     

假设您需要一些文件的完整路径列表。在PowerShell V2.0中,您可以编写:

package require Thread
package require tdom

proc proc1 {response xpath} {
    set doc ""
    set root ""
    set node ""
    .....
}

proc proc2 {response xpath} {
    set doc ""
    set root ""
    set node ""
   .....
}

proc proc3 {one two} {
    set res1 proc1 $one
    set res2 proc2 $two
    .....
}
set startMyProc {
    proc download {one two three four} {
        set res [proc3 $one $two $three $four]
    }
}

proc Threaded {one two three four} {
    variable startMyProc
    set pool [tpool::create -maxworkers 10 -initcmd $startMyProc]

    # Sent the work into the pool as distinct jobs
    set jobs [list]
    for {set i 0} {$i < [llength $one]} {incr i} {
        lappend  jobs [tpool::post -nowait $pool [list download [lindex $one $] $two $three $four]]
    }
    while {[llength $jobs] > 0} {
        tpool::wait $pool $jobs jobs
    }
    tpool::release $pool
}

Threaded xxx xxx xxx xxxx
     

在PowerShell V3.0中,您可以编写:

dir | % { $_.FullName }

当您使用PowerShell V2时,您必须这样写:

(dir).FullName

或者这个:

$DeploySetting.setting | Where-Object {$_.name -eq "WebServer"} | Select-Object -ExpandProperty value

答案 1 :(得分:0)

在PowerShell 3.0及更高版本中,对一组对象的共享属性名称的引用将返回所述属性值的数组,就像您期望的那样。

然而,在PowerShell 2.0中,这将产生$null,因为集合本身没有您正在寻找的属性。相反,请使用Select-Object -ExpandProperty

PowerShell 2.0+示例:

PS C:\> $a = New-Object psobject -Property @{value="1"}
PS C:\> $b = New-Object psobject -Property @{value="2"}
PS C:\> @($a,$b) | Select-Object -ExpandProperty Value
1
2

PowerShell 3.0+示例:

PS C:\> $a = New-Object psobject -Property @{value="1"}
PS C:\> $b = New-Object psobject -Property @{value="2"}
PS C:\> @($a,$b).Value
1
2