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

Hey PowerShell guy !, The Games Are Afoot! Oh, and Some XML, Too

In TechNet Magazine you can find the Scripting Guy article  The Games Are Afoot! Oh, and Some XML, Too

I translated the scripts given for this article into PowerShell

 

# Create 

$xml = New-Object xml

$root = $xml.CreateElement("ITChecklist")
[void]$xml.AppendChild($root)

$Record = $xml.CreateElement("ComputerAudit")
[void]$root.AppendChild($Record)

$Name = $xml.CreateElement("ComputerName")
$Name.PSBase.InnerText = "atl-ws-001"
[void]$Record.AppendChild($name)

$Date = $xml.CreateElement("Auditdate")
$Date.PSBase.InnerText = (get-date)
[void]$Record.AppendChild($date)

$xml.save("c:\scripts\Audits.xml")
get-content c:\scripts\Audits.xml

# Load and Append 

Sleep 5

$xml = [xml] (get-content c:\scripts\Audits.xml)
$root = $xml.ITChecklist

$Record = $xml.CreateElement("ComputerAudit")
[void]$root.AppendChild($Record)

$Name = $xml.CreateElement("ComputerName")
$Name.PSBase.InnerText = "atl-ws-100"
[void]$Record.AppendChild($name)

$Date = $xml.CreateElement("Auditdate")
$Date.PSBase.InnerText = (get-date)
[void]$Record.AppendChild($date)

$xml.save("c:\scripts\Audits.xml")

# Display result

$xml = [xml] (get-content c:\scripts\Audits.xml)
$xml.ITChecklist.ComputerAudit


# Change audit dates 

$xml.SelectNodes("/ITChecklist/ComputerAudit/Auditdate") |% {$_.PSBase.innertext = get-date}
$xml.save([console]::out);""

$xml | format-Custom

# Remove node

$nodes = $xml.SelectNodes("/ITChecklist/ComputerAudit [ComputerName = 'atl-ws-100']")
$root = $xml.ITChecklist
[void]$root.RemoveChild($nodes.item(0))
$xml.ITChecklist.ComputerAudit

You can see there is not that much difference, only in PowerShell working with XML as object is easier, I do show some different ways to format the output in the PowerShell examples

The results are below :

PoSH> # Create                                                                                                          
PoSH>                                                                                                                   
PoSH> $xml = New-Object xml                                                                                             
PoSH>                                                                                                                   
PoSH> $root = $xml.CreateElement("ITChecklist")                                                                         
PoSH> [void]$xml.AppendChild($root)                                                                                     
PoSH>                                                                                                                   
PoSH> $Record = $xml.CreateElement("ComputerAudit")                                                                     
PoSH> [void]$root.AppendChild($Record)                                                                                  
PoSH>                                                                                                                   
PoSH> $Name = $xml.CreateElement("ComputerName")                                                                        
PoSH> $Name.PSBase.InnerText = "atl-ws-001"                                                                             
PoSH> [void]$Record.AppendChild($name)                                                                                  
PoSH>                                                                                                                   
PoSH> $Date = $xml.CreateElement("Auditdate")                                                                           
PoSH> $Date.PSBase.InnerText = (get-date)                                                                               
PoSH> [void]$Record.AppendChild($date)                                                                                  
PoSH>                                                                                                                   
PoSH> $xml.save("c:\scripts\Audits.xml")                                                                                
PoSH> get-content c:\scripts\Audits.xml                                                                                 
<ITChecklist>                                                                                                           
  <ComputerAudit>                                                                                                       
    <ComputerName>atl-ws-001</ComputerName>                                                                             
    <Auditdate>02/01/2008 21:01:44</Auditdate>                                                                          
  </ComputerAudit>                                                                                                      
</ITChecklist>                                                                                                          
PoSH> 
                                                                                                                  
PoSH> # Load and Append                                                                                                 
PoSH>                                                                                                                   
PoSH> Sleep 5                                                                                                           
PoSH>                                                                                                                   
PoSH> $xml = [xml] (get-content c:\scripts\Audits.xml)                                                                  
PoSH> $root = $xml.ITChecklist                                                                                          
PoSH>                                                                                                                   
PoSH> $Record = $xml.CreateElement("ComputerAudit")                                                                     
PoSH> [void]$root.AppendChild($Record)                                                                                  
PoSH>                                                                                                                   
PoSH> $Name = $xml.CreateElement("ComputerName")                                                                        
PoSH> $Name.PSBase.InnerText = "atl-ws-100"                                                                             
PoSH> [void]$Record.AppendChild($name)                                                                                  
PoSH>                                                                                                                   
PoSH> $Date = $xml.CreateElement("Auditdate")                                                                           
PoSH> $Date.PSBase.InnerText = (get-date)                                                                               
PoSH> [void]$Record.AppendChild($date)                                                                                  
PoSH>                                                                                                                   
PoSH> $xml.save("c:\scripts\Audits.xml")                                                                                
PoSH>
                                                                                                                   
PoSH> # Display result                                                                                                  
PoSH>                                                                                                                   
PoSH> $xml = [xml] (get-content c:\scripts\Audits.xml)                                                                  
PoSH> $xml.ITChecklist.ComputerAudit                                                                                    
                                                                                                                        
ComputerName                                                Auditdate                                                   
------------                                                ---------                                                   
atl-ws-001                                                  02/01/2008 21:01:44                                         
atl-ws-100                                                  02/01/2008 21:01:49                                         
                                                                                                                        
                                                                                                                        
PoSH>                                                                                                                   
PoSH>  
                                                                                                                 
PoSH> # Change audit dates                                                                                              
PoSH>                                                                                                                   
PoSH> $xml.SelectNodes("/ITChecklist/ComputerAudit/Auditdate") |% {$_.PSBase.innertext = get-date}                      
PoSH> $xml.save([console]::out);""                                                                                      
<?xml version="1.0" encoding="IBM437"?>                                                                                 
<ITChecklist>                                                                                                           
  <ComputerAudit>                                                                                                       
    <ComputerName>atl-ws-001</ComputerName>                                                                             
    <Auditdate>02/01/2008 21:01:49</Auditdate>                                                                          
  </ComputerAudit>                                                                                                      
  <ComputerAudit>                                                                                                       
    <ComputerName>atl-ws-100</ComputerName>                                                                             
    <Auditdate>02/01/2008 21:01:49</Auditdate>                                                                          
  </ComputerAudit>                                                                                                      
</ITChecklist>                                                                                                          
PoSH>                                                                                                                   
PoSH> $xml | format-Custom                                                                                              
                                                                                                                        
class XmlDocument                                                                                                       
{                                                                                                                       
  ITChecklist =                                                                                                         
    class XmlElement                                                                                                    
    {                                                                                                                   
      ComputerAudit =                                                                                                   
        [                                                                                                               
          class XmlElement                                                                                              
          {                                                                                                             
            ComputerName = atl-ws-001                                                                                   
            Auditdate = 02/01/2008 21:01:49                                                                             
          }                                                                                                             
          class XmlElement                                                                                              
          {                                                                                                             
            ComputerName = atl-ws-100                                                                                   
            Auditdate = 02/01/2008 21:01:49                                                                             
          }                                                                                                             
        ]                                                                                                               
                                                                                                                        
    }                                                                                                                   
}                                                                                                                       
                                                                                                                        
                                                                                                                        
                                                                                                                        
PoSH>    
                                                                                                               
PoSH> # Remove node                                                                                                     
PoSH>                                                                                                                   
PoSH> $nodes = $xml.SelectNodes("/ITChecklist/ComputerAudit [ComputerName = 'atl-ws-100']")                             
PoSH> $root = $xml.ITChecklist                                                                                          
PoSH> [void]$root.RemoveChild($nodes.item(0))                                                                           
PoSH> $xml.ITChecklist.ComputerAudit                                                                                    
                                                                                                                        
ComputerName                                                Auditdate                                                   
------------                                                ---------                                                   
atl-ws-001                                                  02/01/2008 21:01:49                                         
                                                                                                                        
                                                                                                                        
PoSH>                                                                                    

 

Enjoy,

Greetings /\/\o\/\/

Published Friday, February 01, 2008 3:13 PM by MoW
Filed under: , ,

Comments

# Interesting Finds: February 2, 2008

Saturday, February 02, 2008 12:02 PM by Jason Haley

# Changeing a value in an XML file | keyongtech

Sunday, January 18, 2009 12:22 PM by Changeing a value in an XML file | keyongtech
Anonymous comments are disabled