Getting value from pipeline property in powershell

7.1k views Asked by At

Can anyone tell me why I am able to get the value for the Website but not the WebServer using the below command?

$DeploySetting.setting is as follows:

Name-------------Value

Website----------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

Is it because there are two values in $DeploySetting.setting for WebServer? shouldn't this just store in the varibale as a string array?

Powershell Version:

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

Sorry if its somthing simple, new to powershell.

Thanks

Contents of $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

There are 2 answers

4
user4003407 On

What you asking is a new feature of PowerShell V3:

Member Enumeration

To start, I’m going to describe a little feature that had no official name until I started this blog post. I’m also starting with this feature because it was a surprise hit (to me anyway) when we demonstrated to the PowerShell MVPs recently.

If you find yourself frequently piping a collection to ForEach-Object or Select-Object (or more likely the aliases % or select) to get a single property, then you’ll love this feature.

Say you want a list of full paths to some files. In PowerShell V2.0, you’d write:

dir | % { $_.FullName }

In PowerShell V3.0, you can write:

(dir).FullName

As you using PowerShell V2, you have to write it like this:

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

or this:

$DeploySetting.setting | Where-Object {$_.name -eq "WebServer"} | ForEach-Object { $_.value }
0
Mathias R. Jessen On

In PowerShell 3.0 and newer, a reference to a shared property name across a collection of objects will return an array of said property values, just like you expect.

In PowerShell 2.0 however, this will produce $null, since the collection itself doesn't have the property you're looking for. Instead, use Select-Object -ExpandProperty:

PowerShell 2.0+ example:

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+ example:

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