Wednesday, 30 June 2010

Set-XMLAttribute.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script adds an attribute to an XML node 
  4. .DESCRIPTION 
  5.     This script creates a simple XML document and sets an attribute 
  6.     on a node. The script displays the before and after results, and 
  7.     is a conversion and extension of the MSDN sample.  
  8. .NOTES 
  9.     File Name  : Set-XMLAttribute.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: http://www.pshscripts.blogspot.com 
  14.     MSDN Sample posted at: http://msdn.microsoft.com/en-us/library/y1ah1zbw.aspx  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Set-XMLAttribute.ps1 
  17.     Display the Starting XML... 
  18.     <book xmlns:bk="urn:samples" bk:ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> 
  19.     Display the modified XML... 
  20.     <book xmlns:bk="urn:samples" bk:ISBN="1-861001-57-5" genre="novel"><title>Pride And Prejudice</title></book> 
  21. #> 
  22.  
  23. # Create the XML document and populate 
  24.   
  25. $Doc = New-Object System.XML.XmlDocument 
  26. $Doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>"
  27. "<title>Pride And Prejudice</title>"
  28. "</book>"
  29. $Root = $doc.DocumentElement 
  30.   
  31. # Display the XML 
  32. "Display the Starting XML..." 
  33. $doc.InnerXml 
  34.   
  35. # Add an attribute and display results 
  36. $root.SetAttribute("genre", "novel"
  37. "Display the modified XML..." 
  38. $doc.InnerXmL 

Tuesday, 29 June 2010

Clone-XMLNode.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script clones an XML element. 
  4. .DESCRIPTION 
  5.     This script creates an XML document, then creates two 
  6.     clone elements. 
  7. .NOTES 
  8.     File Name  : Clone-XMLNode.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0
  11.                  Needs PowerShell Community Extensions too! 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN Sample posted at: 
  16.         http://msdn.microsoft.com/en-us/library/system.xml.xmlelement.removeattributeat.aspx 
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .\CloneXMLNode.ps1 
  19.     <book genre="novel" ISBN="1-861001-57-5"
  20.       <title>Pride And Prejudice 
  21.       <misc publisher="WorldWide Publishing">hardcover</misc> 
  22.       <misc publisher="WorldWide Publishing">hardcover</misc> 
  23.       </title> 
  24. </book> 
  25. #> 
  26.      
  27. $Doc = New-Object System.Xml.XmlDocument 
  28. $Doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>"
  29.                 "<title>Pride And Prejudice</title>"
  30.              "</book>"            
  31.              ) 
  32.               
  33. # Create an element 
  34. $elem = $doc.CreateElement("misc"
  35. $elem.InnerText = "hardcover" 
  36. $elem.SetAttribute("publisher", "WorldWide Publishing"
  37.  
  38.  # Clone the element so we can add one to each of the book nodes. 
  39. $elem2 = $elem.CloneNode($true
  40.  
  41. # Add the new elements. 
  42. $result = $doc.DocumentElement.FirstChild.AppendChild($elem
  43. $result = $doc.DocumentElement.LastChild.AppendChild($elem2
  44.  
  45.  
  46. # Display the Modified XML..." 
  47. $doc.InnerXml | format-xml 

Remove-XMLAttribute.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script removes an an attribute from an XML element. 
  4. .DESCRIPTION 
  5.     This script creates an XML document, then removes an 
  6.     Attribute using the RemoveAttribute method. 
  7. .NOTES 
  8.     File Name  : Remove-XMLAttribute.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/z267hx58.aspx 
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Remove-XMLAttribute.ps1
  18.     Display the initial XML... 
  19.     <book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> 
  20.     Display the modified XML... 
  21.     <book ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> 
  22. #> 
  23.  
  24. # Create and load an XML Document 
  25. $Doc = New-Object System.Xml.XmlDocument 
  26. $Doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" + 
  27.                 "<title>Pride And Prejudice</title>"
  28.                 "</book>"
  29. # Set root 
  30. $Root = $Doc.DocumentElement 
  31.  
  32. # Display initial XML 
  33. "Display the initial XML..." 
  34. $Doc.InnerXml 
  35.  
  36. # Remove the genre attribute 
  37. $Return = $Root.RemoveAttribute("genre"
  38.  
  39. # Display result 
  40. "Display the modified XML..." 
  41. $Doc.InnerXml 

Monday, 28 June 2010

Remove-XMLAttributeAt.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script removed an an attribute from an XML element. 
  4. .DESCRIPTION 
  5.     This script creates an XML document, then removes an 
  6.     Attribute using the RemoveAttributeAt method. 
  7. .NOTES 
  8.     File Name  : Remove-XMLAttributeAt.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com  
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/system.xml.xmlelement.removeattributeat.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.XML\Remove-XMLAttrivuteAt.ps1' 
  18.     Display the initial XML... 
  19.     <book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> 
  20.     Display the modified XML... 
  21.     <book ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book> 
  22. #> 
  23.  
  24. # Create and load an XML Document 
  25. $Doc = New-Object System.Xml.XmlDocument 
  26. $Doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>"
  27.                 "<title>Pride And Prejudice</title>"
  28.                 "</book>"
  29. # Set root 
  30. $Root = $Doc.DocumentElement 
  31.  
  32. # Display initial XML 
  33. "Display the initial XML..." 
  34. $Doc.InnerXml 
  35.  
  36. # Remove the genre attribute 
  37. $Return = $Root.RemoveAttributeAt(0) 
  38.  
  39. # Display result 
  40. "Display the modified XML..." 
  41. $Doc.InnerXml 

Sunday, 27 June 2010

Get-XMLAttribute.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script retrieves an attribute from an XML Element  
  4. .DESCRIPTION 
  5.     This script first creates an in-memory XML document, then used the  
  6.     HasAttribute and GetAttribute to retrieve an attribute. 
  7. .NOTES 
  8.     File Name  : Get-XMLAttribute.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://pshscripts.blogspot.com/2010/06/get-xmlattributeps1.html 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/efsc3w6k.aspx
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-XMLAttribute.ps1 
  18.     Elemnt has genre: novel 
  19. #> 
  20. # Create XML document in memory 
  21. $doc = new-object System.Xml.XmlDocument 
  22. $doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>"
  23.                 "<title>Pride And Prejudice</title>"
  24.                 "</book>"); 
  25. # Set root 
  26. $root = $doc.DocumentElement; 
  27.  
  28. # Check to see if the element has a genre attribute 
  29. # Then display it 
  30. if ($root.HasAttribute("genre")){ 
  31.       $genre = $root.GetAttribute("genre"); 
  32.       "Elemnt has genre: {0}" -f $genre 
  33.    } 

Thursday, 17 June 2010

Get-XMLNode.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script re-implements an MSDN Sample in PowerShell 
  4. .DESCRIPTION 
  5.     This script creates and manipulates an XML Document 
  6. .NOTES 
  7.     File Name  : Get-XMLNode.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN Sample posted at: 
  14.         http://msdn.microsoft.com/en-us/library/system.xml.xmlelement.name.aspx 
  15. .EXAMPLE 
  16.     [ps] c:\foo> .\Get-XMLElement.ps1 
  17.     bk:ISBN = 1-861001-57-5 
  18.        namespaceURI=urn:samples 
  19. #> 
  20.  
  21. # Create new document 
  22. $doc = New-Object System.Xml.XmlDocument 
  23.   
  24. # Load XML document from text 
  25. $doc.LoadXml("<book xmlns:bk='urn:samples'>"
  26.                 "<bk:ISBN>1-861001-57-5</bk:ISBN>"
  27.                 "<title>Pride And Prejudice</title>"
  28.                 "</book>"
  29.  
  30. # Display information on the ISBN element. 
  31. $elem = $doc.DocumentElement.FirstChild; 
  32. "{0} = {1}" -f $elem.Name, $elem.InnerText 
  33. "`tnamespaceURI=" + $elem.NamespaceURI 
  34. # End of Script

Thursday, 3 June 2010

Replace-String2.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script implements an MSDN sample in PowerShell 
  4. .DESCRIPTION 
  5.     This script uses the String Replace method to replace 
  6.     all occurances of one char in a string to another.  
  7. .NOTES 
  8.     File Name  : Replace-String2.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://pshscripts.blogspot.com/2010/06/replace-string2ps1.html 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/czx8s9ts%28VS.80%29.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Replace-String2.ps1' 
  18.     Original string: "1 2 3 4 5 6 7 8 9" 
  19.     CSV string:      "1,2,3,4,5,6,7,8,9" 
  20. #> 
  21.  
  22. ##
  23. # Start of Script
  24. ##
  25.   
  26. # Create string 
  27. $Str = "1 2 3 4 5 6 7 8 9" 
  28.   
  29. # Display Original String 
  30. "Original string: `"{0}`"" -f $Str 
  31.   
  32. # Display new string, replacing " " with "," 
  33. "CSV string:      `"{0}`"" -f $Str.Replace(' ', ','
  34. # End of script 
  35. # Display Original String 
  36. "Original string: `"{0}`"" -f $Str 
  37.   
  38. # Display new string, replacing " " with "," 
  39. "CSV string:      `"{0}`"" -f $Str.Replace(' ', ','
  40. # End of script

Tuesday, 1 June 2010

Get-Bios.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets all the computers in a domain and then  
  4.     displays bios details for each one. 
  5. .DESCRIPTION 
  6.     This script uses the Active Directory module to get 
  7.     all computers. Then it enumerates this set and uses WMI 
  8.     to get bios information from each system. Note that  
  9.     script does not handle WMI errors. 
  10. .NOTES 
  11.     File Name  : Get-Bios.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14. .LINK 
  15.     This script posted to: 
  16.         http://pshscripts.blogspot.com/2010/06/get-biosps1.html
  17. .EXAMPLE 
  18.     PS [C:\FOO] .\Get-Bios.ps1 
  19.     Machine:           DC1.reskit.org 
  20.     Bios Name          BIOS Date: 03/19/09 22:51:32  Ver: 09.00.04 
  21.     Bios Manufacturer  American Megatrends Inc. 
  22.     Bios Version        
  23.     Bios Serial Number 3678-9986-1198-6272-1712-1835-12 
  24.   
  25.     Machine:           SQL1.reskit.org 
  26.     Bios Name          BIOS Date: 03/19/09 22:51:32  Ver: 09.00.04 
  27.     Bios Manufacturer  American Megatrends Inc. 
  28.     Bios Version        
  29.     Bios Serial Number 4769-0987-7689-7711-9784-0733-29 
  30.    <etc - rest snipped for brevity> 
  31. #> 
  32.  
  33. ## 
  34. # Start of Script 
  35. ## 
  36.  
  37. # Import the AD module 
  38. Import-Module activedirectory 
  39.  
  40. # Get the computers 
  41. $Computers = Get-ADComputer -Filter * 
  42.   
  43. # Visit each one and get some output 
  44. # NB: error handling should be added in for production use 
  45. foreach ($Computer in $Computers){ 
  46.     $bios = GWMI win32_bios -computer $computer.dnshostname 
  47.     "Machine:           {0}" -f $Computer.dnshostname 
  48.     "Bios Name          {0}" -f $BIOS.Name 
  49.     "Bios Manufacturer  {0}" -f $bios.manufacturer 
  50.     "Bios Version       {0}" -f $bios.SmbbiosBiosVersion 
  51.     "Bios Serial Number {0}" -f $bios.serialnumber 
  52.     "" 

Get-ComputerDomain.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Shows use of GetComputerDomain to return information about the domain 
  4. .DESCRIPTION 
  5.     Uses system.directoryservices.activedirectory.domain and GetComputerDomain 
  6.     to return domain information of the Computer. Note that this 
  7.     may be different to the domain of the logged on user.    
  8. .NOTES 
  9.     File Name  : Get-DomainController.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.    This script posted to: 
  14.         http://pshscripts.blogspot.com/2010/06/get-computerdomainps1.html 
  15.     MSDN Sample posted at: 
  16.         http://msdn.microsoft.com/en-us/library/system.directoryservices.activedirectory.domain.getcomputerdomain%28VS.80%29.aspx  
  17. .EXAMPLE 
  18.     PS c:\foo> .\Get-ComputerDomain.ps1 
  19.     Your computer is a member of the cookham.net domain 
  20.     Role Holders in this domain: 
  21.       PDC Role Holder     :  Cookham1.cookham.net 
  22.       RID Role Holder     :  Cookham1.cookham.net 
  23.       InfraM Role Holder  :  Cookham1.cookham.net 
  24. #> 
  25.   
  26. ### 
  27. # Start of script 
  28. ### 
  29.   
  30. # Get Domain information using static method 
  31. $domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()  
  32.  
  33. # Display information for current computer 
  34. "Your computer is a member of the {0} domain" -f $domain.name  
  35. "Role Holders in this domain:" 
  36. "  PDC Role Holder     :  {0}" -f $domain.PdcRoleOwner 
  37. "  RID Role Holder     :  {0}" -f $domain.RIDRoleOwner 
  38. "  InfraM Role Holder  :  {0}" -f $domain.InfrastructureRoleOwner 
  39. # End of script