Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

Hey,PowerShell Guy! Chasing Cars…and XML

In the TechNet Magizine Februari Edition  There is an excelent Hey, Scripting Guy! Column about parsing XML Chasing Cars…and XML

It shows how to filter and select data from a XML file 

I will give the translations of the examples given in PowerShell for the explanations see the Orignal article.

 

PoSH> Get-Content scripts.xml                                                                                           
<?xml version="1.0" encoding="ISO-8859-1"?>                                                                             
<Repository>                                                                                                            
  <Script>                                                                                                              
    <Category>Microsoft Office</Category>                                                                               
    <Subcategory>Microsoft Access</Subcategory>                                                                         
    <Keyword>databases</Keyword>                                                                                        
    <Title>How Can I Print a Microsoft Access Report?</Title>                                                           
    <URL>http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct06/hey1020.mspx</URL>                         
  </Script>                                                                                                             
  <Script>                                                                                                              
    <Category>Microsoft Office</Category>                                                                               
    <Subcategory>Microsoft Access</Subcategory>                                                                         
    <Keyword>databases</Keyword>                                                                                        
    <Title>How Can I Compact a Microsoft Access Database?</Title>                                                       
    <URL>http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct06/hey1009.mspx</URL>                         
  </Script>                                                                                                             
  <Script>                                                                                                              
    <Category>Microsoft Office</Category>                                                                               
    <Subcategory>Microsoft Word</Subcategory>                                                                           
    <Keyword>hyperlinks</Keyword>                                                                                       
    <Title>How Can I Change an Existing Hyperlink in a Microsoft Word Document?</Title>                                 
    <URL>http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct06/hey1016.mspx</URL>                         
  </Script>                                                                                                             
  <Script>                                                                                                              
    <Category>Enterprise Servers</Category>                                                                             
    <Subcategory>Microsoft SQL Server</Subcategory>                                                                     
    <Keyword>databases</Keyword>                                                                                        
    <Title>How Can I Create a Table in a SQL Server Database?</Title>                                                   
    <URL>http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct06/hey1016.mspx</URL>                         
  </Script>                                                                                                             
</Repository>                                                                                                           
                                                                                                                        
PoSH> [xml]( Get-Content scripts.xml )                                                                                  
                                                                                                                        
xml                                                         Repository                                                  
---                                                         ----------                                                  
                                                            Repository                                                  
                                                                                                                        
                                                                                                                        
PoSH> $scripts = [xml]( Get-Content scripts.xml )                                                                       
PoSH> $scripts.Repository                                                                                               
                                                                                                                        
Script                                                                                                                  
------                                                                                                                  
{Script, Script, Script, Script}                                                                                        
                                                                                                                        
                                                                                                                        
PoSH> $scripts.Repository.Script                                                                                        
                                                                                                                        
                                                                                                                        
Category    : Microsoft Office                                                                                          
Subcategory : Microsoft Access                                                                                          
Keyword     : databases                                                                                                 
Title       : How Can I Print a Microsoft Access Report?                                                                
URL         : http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct06/hey1020.mspx                          
                                                                                                                        
Category    : Microsoft Office                                                                                          
Subcategory : Microsoft Access                                                                                          
Keyword     : databases                                                                                                 
Title       : How Can I Compact a Microsoft Access Database?                                                            
URL         : http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct06/hey1009.mspx                          
                                                                                                                        
Category    : Microsoft Office                                                                                          
Subcategory : Microsoft Word                                                                                            
Keyword     : hyperlinks                                                                                                
Title       : How Can I Change an Existing Hyperlink in a Microsoft Word Document?                                      
URL         : http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct06/hey1016.mspx                          
                                                                                                                        
Category    : Enterprise Servers                                                                                        
Subcategory : Microsoft SQL Server                                                                                      
Keyword     : databases                                                                                                 
Title       : How Can I Create a Table in a SQL Server Database?                                                        
URL         : http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct06/hey1016.mspx                          
                                                                                                                        
                                                                                                                        
                                                                                                                        
PoSH> $scripts.SelectNodes("/Repository/Script/*")                                                                      
                                                                                                                        
#text                                                                                                                   
-----                                                                                                                   
Microsoft Office                                                                                                        
Microsoft Access                                                                                                        
databases                                                                                                               
How Can I Print a Microsoft Access Report?                                                                              
http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct06/hey1020.mspx                                        
Microsoft Office                                                                                                        
Microsoft Access                                                                                                        
databases                                                                                                               
How Can I Compact a Microsoft Access Database?                                                                          
http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct06/hey1009.mspx                                        
Microsoft Office                                                                                                        
Microsoft Word                                                                                                          
hyperlinks                                                                                                              
How Can I Change an Existing Hyperlink in a Microsoft Word Document?                                                    
http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct06/hey1016.mspx                                        
Enterprise Servers                                                                                                      
Microsoft SQL Server                                                                                                    
databases                                                                                                               
How Can I Create a Table in a SQL Server Database?                                                                      
http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct06/hey1016.mspx                                        
                                                                                                                        
                                                                                                                        
PoSH> $scripts.SelectNodes("/Repository/Script/Title")                                                                  
                                                                                                                        
#text                                                                                                                   
-----                                                                                                                   
How Can I Print a Microsoft Access Report?                                                                              
How Can I Compact a Microsoft Access Database?                                                                          
How Can I Change an Existing Hyperlink in a Microsoft Word Document?                                                    
How Can I Create a Table in a SQL Server Database?                                                                      
                                                                                                                        
                                                                                                                        
PoSH> $scripts.Repository.Script | select Title,URL                                                                     
                                                                                                                        
Title                                                       URL                                                         
-----                                                       ---                                                         
How Can I Print a Microsoft Access Report?                  http://www.microsoft.com/technet/scriptcenter/resources/... 
How Can I Compact a Microsoft Access Database?              http://www.microsoft.com/technet/scriptcenter/resources/... 
How Can I Change an Existing Hyperlink in a Microsoft Wo... http://www.microsoft.com/technet/scriptcenter/resources/... 
How Can I Create a Table in a SQL Server Database?          http://www.microsoft.com/technet/scriptcenter/resources/... 
                                                                                                                        
                                                                                                                        
PoSH> $scripts.Repository.Script | select Title,URL,Keyword                                                             
                                                                                                                        
Title                                   URL                                     Keyword                                 
-----                                   ---                                     -------                                 
How Can I Print a Microsoft Access R... http://www.microsoft.com/technet/scr... databases                               
How Can I Compact a Microsoft Access... http://www.microsoft.com/technet/scr... databases                               
How Can I Change an Existing Hyperli... http://www.microsoft.com/technet/scr... hyperlinks                              
How Can I Create a Table in a SQL Se... http://www.microsoft.com/technet/scr... databases                               
                                                                                                                        
                                                                                                                        
PoSH> $scripts.Repository.Script | where {$_.Keyword -eq 'databases'}| select Title,URL,Keyword                         
                                                                                                                        
Title                                   URL                                     Keyword                                 
-----                                   ---                                     -------                                 
How Can I Print a Microsoft Access R... http://www.microsoft.com/technet/scr... databases                               
How Can I Compact a Microsoft Access... http://www.microsoft.com/technet/scr... databases                               
How Can I Create a Table in a SQL Se... http://www.microsoft.com/technet/scr... databases                               
                                                                                                                                                                                                                                               
   
PoSH> $scripts.Repository.Script | where {$_.Keyword -eq 'databases' -and $_.Subcategory -eq `Microsoft SQL Server'}    
                                                                                                                        
                                                                                                                        
Category    : Enterprise Servers                                                                                        
Subcategory : Microsoft SQL Server                                                                                      
Keyword     : databases                                                                                                 
Title       : How Can I Create a Table in a SQL Server Database?                                                        
URL         : http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct06/hey1016.mspx                          
                                                                                                                        
                                                                                                                        
                                                                                                                        
PoSH>                                                     

you can see the powerShell wrapper helps a bit ;-)

 Enjoy,

Greetings /\/\o\/\/

Published Wednesday, February 14, 2007 12:53 PM by admin
Filed under: ,

Comments

# re: Hey,PowerShell Guy! Chasing Cars…and XML

Thanks for sharing info. Here reading the XML is covered, what are your views on writing data to a XML file?

Thursday, February 15, 2007 8:10 AM by Nikhil
Anonymous comments are disabled