Arrays 从powershell中的管道属性获取值
有人能告诉我为什么我能使用下面的命令获取网站的值而不能获取Web服务器的值吗 $DeploySetting.setting如下所示: 名称----------值 网站------XXXXX Web服务器------ServXX Web服务器------ServYY 是否因为WebServer的Arrays 从powershell中的管道属性获取值,arrays,powershell,powershell-2.0,Arrays,Powershell,Powershell 2.0,有人能告诉我为什么我能使用下面的命令获取网站的值而不能获取Web服务器的值吗 $DeploySetting.setting如下所示: 名称----------值 网站------XXXXX Web服务器------ServXX Web服务器------ServYY 是否因为WebServer的$DeploySetting.setting中有两个值?这不应该作为字符串数组存储在varibale中吗 Powershell版本: CLR版本2.0.50727.5485 构建版本6.1.7601.175
$DeploySetting.setting
中有两个值?这不应该作为字符串数组存储在varibale中吗
Powershell版本:
CLR版本2.0.50727.5485构建版本6.1.7601.17514
PS2.0版
WSManStackVersion2.0
PSCompatibleVersions{1.0,2.0}
序列化版本1.1.0.1
PSRemotingProtocol2.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;}
你问的是:
成员枚举
首先,我将描述一个在我开始这篇博文之前没有正式名称的小功能。我也从这个功能开始,因为当我们最近向PowerShell MVP演示时,它是一个惊喜(无论如何对我来说)
如果您发现自己经常将集合管道化到ForEach对象或选择对象(或者更可能是别名%or 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 }
在PowerShell 3.0及更新版本中,跨对象集合引用共享属性名称将返回所述属性值的数组,正如您所期望的那样 但是,在PowerShell 2.0中,这将生成
$null
,因为集合本身没有您要查找的属性。相反,请使用选择对象-ExpandProperty
:
PowerShell 2.0+示例:
PowerShell 3.0+示例:
$DeploySetting.setting | Get Member
?Im与arco444的输出是什么。什么是$DeploySetting.setting |获取成员
?您是如何填充$DeploySetting
.Setting
看起来像一个哈希表数组。@PetSerAl请查看我的更新。@arco444$DeploySetting.Setting |添加成员结果。@Aaron当你说“获取值”时,你真正的意思是什么?你到底想做什么?它是如何失败的?在上面的$DeploySetting.setting表中,你可以看到属性的名称和它的值,我正试图从这个表中获取它的值,所以ServXX和ServYY,在检查变量是否为null时验证失败。我在发布之前尝试了这些,在某个地方的博客上看到了类似的东西,但它不起作用。仍然null@Aaron那么,$DeploySetting.setting | Where对象{$\u.name-eq“WebServer”}
的输出是什么呢?它返回什么吗?没有,只是空/空/空。如果我在验证之前放置一个断点并打印“$DeploySetting.setting | Where Object{$\uu0.name-eq“WebServer”}”,我会得到一个如上所述的表,但只有WebServer名称和值。您确定没有拼写错误吗?尝试打开严格模式:设置严格模式-最新版本
。
$DeploySetting.setting | Where-Object {$_.name -eq "WebServer"} | ForEach-Object { $_.value }
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
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