【问题标题】:Getting value from pipeline property in powershell从 powershell 中的管道属性中获取价值
【发布时间】:2015-09-01 12:24:52
【问题描述】:

谁能告诉我为什么我可以使用以下命令获取网站的值,但不能获取 WebServer 的值?

$DeploySetting.setting 如下:

名称-------------值

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

WebServer-----ServXX

WebServer-----ServYY

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

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

Powershell 版本:

CLR版本 2.0.50727.5485
构建版本 6.1.7601.17514
PS版本 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0、2.0}
序列化版本 1.1.0.1
PSRemotingProtocolVersion 2.1

对不起,如果它的东西很简单,对 powershell 来说是新的。

谢谢

$deploySetting.setting 的内容 |获取会员

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;}

【问题讨论】:

  • $DeploySetting.setting | Get-Member 的输出是什么?
  • 我是arco444。 $DeploySetting.setting | Get-Member 是什么?你是如何填充$DeploySetting 的? .Setting 看起来像一个哈希表数组。
  • @PetSerAl 请查看我的更新。@arco444 $DeploySetting.setting |已添加 Get-Member 结果。
  • @Aaron 当您说“获得价值”时,您的真正意思是什么?你到底想完成什么,它是如何失败的?
  • 在上面的 $DeploySetting.setting 表中,您可以看到属性的名称及其值,这是我试图从该表中获取的值,因此 ServXX 和 ServYY,它失败了检查变量不为空时的验证。

标签: arrays powershell powershell-2.0


【解决方案1】:

你问的是new feature of PowerShell V3

成员枚举

首先,我将描述一个在我开始这篇博文之前没有正式名称的小功能。我也是从这个功能开始的,因为当我们最近向 PowerShell MVP 演示时(无论如何对我来说),它是一个惊喜。

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

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

dir | % { $_.FullName }

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

(dir).FullName

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

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

或者这个:

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

【讨论】:

  • 我在发布之前尝试了这些,在博客上看到了一些类似的东西,但它不起作用。仍然为空
  • @Aaron 那么$DeploySetting.setting | Where-Object {$_.name -eq "WebServer"} 的输出是什么?它会返回一些东西吗?
  • 什么都没有,只是空/空/空白。如果我在验证之前设置一个断点并打印 '$DeploySetting.setting | Where-Object {$_.name -eq "WebServer"} ' 我得到了一个类似上面的表,但只有 WebServer 名称和值
  • 你确定你没有拼错任何地方吗?尝试开启严格模式:Set-StrictMode -Version Latest.
【解决方案2】:

在 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

【讨论】:

    猜你喜欢
    • 2017-09-16
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 2013-11-15
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    相关资源
    最近更新 更多