PowerShell Data Basics

PowerShell Data Basics: File Based Data


PowerShell Data Basics: XML


Using Select-Xml With XmlNamespaces

Using Select-Xml With XmlNamespaces

#  Select-XML is a nifty cmdlet included in PowerShell V2,             
#   and it lets you perform xPath queries.            
#  It also makes a pair of scenarios that are             
#   otherwise a little tricky with xpath fairly simple.            
#  Read below, or check out this video             
#   that walks thru Select-XMl and namespaces            
#  Let's look at a quick example.             
#  Here's a sample service definition for Azure:            

$xml=[xml]@"<?xml version="1.0" encoding="utf-16"?>
<ServiceDefinition name="foobar" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
  <WebRole name="WebRole1" enableNativeCodeExecution="true">
      <Site name="Web1">
          <Binding endpointName="DefaultWebSiteEndPoint" name="Binding1" hostHeader="foo.bar.com" />
      <Site name="Web2">
          <Binding endpointName="DefaultWebSiteEndPoint" name="Binding1" hostHeader="bar.baz.com" />
      <InputEndpoint name="DefaultWebSiteEndPoint" protocol="http" port="80" />

#  If I want to find all of the Bindings in this XML with xPath,
#   .NET has some surprises in store.
#  That is: // in Xpath grabs a node at any depth,             
#   but only if you specify a namespace.            

$xml.SelectNodes("//Binding")            # results in nothing!            

#  The .NET code to deal with namespaces is pretty tedious,            
#   and you do not need to know it.            
#  Select-Xml can make it a lot easier with the -Namespace parameter.             
#  -Namespace is a hashtable, and you put a shortname             
#   for the way you want to find the XML.            
#  XMLNS is reserved, so I'll use "Azure"            
#  Using this, it's quick and easy to zoom right in             
#   on the piece of information we're after            

Select-Xml -Xml $xml -Namespace @{                        
} -XPath //Azure:Binding |                        
    Select-Object -ExpandProperty Node |                        
    Select-Object -ExpandProperty HostHeader                            

# And That Works !                

XML to Word

XML to Word


XPath Tutorial

XPath Tutorial


Persist Objects, with CliXml

Persist Objects, with CliXml


Exporting and Importing Credentials

Exporting and Importing Credentials


Adding File Age to file objects

Adding File Age to file objects