- <#
- .SYNOPSIS
- This script adds an attribute to an XML node
- .DESCRIPTION
- This script creates a simple XML document and sets an attribute
- on a node. The script displays the before and after results, and
- is a conversion and extension of the MSDN sample.
- .NOTES
- File Name : Set-XMLAttribute.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to: http://www.pshscripts.blogspot.com
- MSDN Sample posted at: http://msdn.microsoft.com/en-us/library/y1ah1zbw.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Set-XMLAttribute.ps1
- Display the Starting XML...
- <book xmlns:bk="urn:samples" bk:ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book>
- Display the modified XML...
- <book xmlns:bk="urn:samples" bk:ISBN="1-861001-57-5" genre="novel"><title>Pride And Prejudice</title></book>
- #>
- # Create the XML document and populate
- $Doc = New-Object System.XML.XmlDocument
- $Doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
- "<title>Pride And Prejudice</title>" +
- "</book>")
- $Root = $doc.DocumentElement
- # Display the XML
- "Display the Starting XML..."
- $doc.InnerXml
- # Add an attribute and display results
- $root.SetAttribute("genre", "novel")
- "Display the modified XML..."
- $doc.InnerXmL
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Wednesday, 30 June 2010
Set-XMLAttribute.ps1
Tuesday, 29 June 2010
Clone-XMLNode.ps1
- <#
- .SYNOPSIS
- This script clones an XML element.
- .DESCRIPTION
- This script creates an XML document, then creates two
- clone elements.
- .NOTES
- File Name : Clone-XMLNode.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- Needs PowerShell Community Extensions too!
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.xml.xmlelement.removeattributeat.aspx
- .EXAMPLE
- PSH [C:\foo]: .\CloneXMLNode.ps1
- <book genre="novel" ISBN="1-861001-57-5">
- <title>Pride And Prejudice
- <misc publisher="WorldWide Publishing">hardcover</misc>
- <misc publisher="WorldWide Publishing">hardcover</misc>
- </title>
- </book>
- #>
- $Doc = New-Object System.Xml.XmlDocument
- $Doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
- "<title>Pride And Prejudice</title>" +
- "</book>"
- )
- # Create an element
- $elem = $doc.CreateElement("misc")
- $elem.InnerText = "hardcover"
- $elem.SetAttribute("publisher", "WorldWide Publishing")
- # Clone the element so we can add one to each of the book nodes.
- $elem2 = $elem.CloneNode($true)
- # Add the new elements.
- $result = $doc.DocumentElement.FirstChild.AppendChild($elem)
- $result = $doc.DocumentElement.LastChild.AppendChild($elem2)
- # Display the Modified XML..."
- $doc.InnerXml | format-xml
Remove-XMLAttribute.ps1
- <#
- .SYNOPSIS
- This script removes an an attribute from an XML element.
- .DESCRIPTION
- This script creates an XML document, then removes an
- Attribute using the RemoveAttribute method.
- .NOTES
- File Name : Remove-XMLAttribute.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/z267hx58.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Remove-XMLAttribute.ps1
- Display the initial XML...
- <book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book>
- Display the modified XML...
- <book ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book>
- #>
- # Create and load an XML Document
- $Doc = New-Object System.Xml.XmlDocument
- $Doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
- "<title>Pride And Prejudice</title>" +
- "</book>")
- # Set root
- $Root = $Doc.DocumentElement
- # Display initial XML
- "Display the initial XML..."
- $Doc.InnerXml
- # Remove the genre attribute
- $Return = $Root.RemoveAttribute("genre")
- # Display result
- "Display the modified XML..."
- $Doc.InnerXml
Labels:
Power,
powershell,
PowerShell scripts,
System.XML.XMLDocument
Monday, 28 June 2010
Remove-XMLAttributeAt.ps1
- <#
- .SYNOPSIS
- This script removed an an attribute from an XML element.
- .DESCRIPTION
- This script creates an XML document, then removes an
- Attribute using the RemoveAttributeAt method.
- .NOTES
- File Name : Remove-XMLAttributeAt.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.xml.xmlelement.removeattributeat.aspx
- .EXAMPLE
- PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.XML\Remove-XMLAttrivuteAt.ps1'
- Display the initial XML...
- <book genre="novel" ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book>
- Display the modified XML...
- <book ISBN="1-861001-57-5"><title>Pride And Prejudice</title></book>
- #>
- # Create and load an XML Document
- $Doc = New-Object System.Xml.XmlDocument
- $Doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
- "<title>Pride And Prejudice</title>" +
- "</book>")
- # Set root
- $Root = $Doc.DocumentElement
- # Display initial XML
- "Display the initial XML..."
- $Doc.InnerXml
- # Remove the genre attribute
- $Return = $Root.RemoveAttributeAt(0)
- # Display result
- "Display the modified XML..."
- $Doc.InnerXml
Sunday, 27 June 2010
Get-XMLAttribute.ps1
- <#
- .SYNOPSIS
- This script retrieves an attribute from an XML Element
- .DESCRIPTION
- This script first creates an in-memory XML document, then used the
- HasAttribute and GetAttribute to retrieve an attribute.
- .NOTES
- File Name : Get-XMLAttribute.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/06/get-xmlattributeps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/efsc3w6k.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-XMLAttribute.ps1
- Elemnt has genre: novel
- #>
- # Create XML document in memory
- $doc = new-object System.Xml.XmlDocument
- $doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
- "<title>Pride And Prejudice</title>" +
- "</book>");
- # Set root
- $root = $doc.DocumentElement;
- # Check to see if the element has a genre attribute
- # Then display it
- if ($root.HasAttribute("genre")){
- $genre = $root.GetAttribute("genre");
- "Elemnt has genre: {0}" -f $genre
- }
Labels:
PowerShell scripts,
System.XML.XMLDocument,
xml
Thursday, 17 June 2010
Get-XMLNode.ps1
- <#
- .SYNOPSIS
- This script re-implements an MSDN Sample in PowerShell
- .DESCRIPTION
- This script creates and manipulates an XML Document
- .NOTES
- File Name : Get-XMLNode.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.xml.xmlelement.name.aspx
- .EXAMPLE
- [ps] c:\foo> .\Get-XMLElement.ps1
- bk:ISBN = 1-861001-57-5
- namespaceURI=urn:samples
- #>
- # Create new document
- $doc = New-Object System.Xml.XmlDocument
- # Load XML document from text
- $doc.LoadXml("<book xmlns:bk='urn:samples'>" +
- "<bk:ISBN>1-861001-57-5</bk:ISBN>" +
- "<title>Pride And Prejudice</title>" +
- "</book>")
- # Display information on the ISBN element.
- $elem = $doc.DocumentElement.FirstChild;
- "{0} = {1}" -f $elem.Name, $elem.InnerText
- "`tnamespaceURI=" + $elem.NamespaceURI
- # End of Script
Thursday, 3 June 2010
Replace-String2.ps1
- <#
- .SYNOPSIS
- This script implements an MSDN sample in PowerShell
- .DESCRIPTION
- This script uses the String Replace method to replace
- all occurances of one char in a string to another.
- .NOTES
- File Name : Replace-String2.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/06/replace-string2ps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/czx8s9ts%28VS.80%29.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Replace-String2.ps1'
- Original string: "1 2 3 4 5 6 7 8 9"
- CSV string: "1,2,3,4,5,6,7,8,9"
- #>
- ##
- # Start of Script
- ##
- # Create string
- $Str = "1 2 3 4 5 6 7 8 9"
- # Display Original String
- "Original string: `"{0}`"" -f $Str
- # Display new string, replacing " " with ","
- "CSV string: `"{0}`"" -f $Str.Replace(' ', ',')
- # End of script
- # Display Original String
- "Original string: `"{0}`"" -f $Str
- # Display new string, replacing " " with ","
- "CSV string: `"{0}`"" -f $Str.Replace(' ', ',')
- # End of script
Labels:
powershell,
PowerShell scripts,
System.String
Tuesday, 1 June 2010
Get-Bios.ps1
- <#
- .SYNOPSIS
- This script gets all the computers in a domain and then
- displays bios details for each one.
- .DESCRIPTION
- This script uses the Active Directory module to get
- all computers. Then it enumerates this set and uses WMI
- to get bios information from each system. Note that
- script does not handle WMI errors.
- .NOTES
- File Name : Get-Bios.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/06/get-biosps1.html
- .EXAMPLE
- PS [C:\FOO] .\Get-Bios.ps1
- Machine: DC1.reskit.org
- Bios Name BIOS Date: 03/19/09 22:51:32 Ver: 09.00.04
- Bios Manufacturer American Megatrends Inc.
- Bios Version
- Bios Serial Number 3678-9986-1198-6272-1712-1835-12
- Machine: SQL1.reskit.org
- Bios Name BIOS Date: 03/19/09 22:51:32 Ver: 09.00.04
- Bios Manufacturer American Megatrends Inc.
- Bios Version
- Bios Serial Number 4769-0987-7689-7711-9784-0733-29
- <etc - rest snipped for brevity>
- #>
- ##
- # Start of Script
- ##
- # Import the AD module
- Import-Module activedirectory
- # Get the computers
- $Computers = Get-ADComputer -Filter *
- # Visit each one and get some output
- # NB: error handling should be added in for production use
- foreach ($Computer in $Computers){
- $bios = GWMI win32_bios -computer $computer.dnshostname
- "Machine: {0}" -f $Computer.dnshostname
- "Bios Name {0}" -f $BIOS.Name
- "Bios Manufacturer {0}" -f $bios.manufacturer
- "Bios Version {0}" -f $bios.SmbbiosBiosVersion
- "Bios Serial Number {0}" -f $bios.serialnumber
- ""
- }
Get-ComputerDomain.ps1
- <#
- .SYNOPSIS
- Shows use of GetComputerDomain to return information about the domain
- .DESCRIPTION
- Uses system.directoryservices.activedirectory.domain and GetComputerDomain
- to return domain information of the Computer. Note that this
- may be different to the domain of the logged on user.
- .NOTES
- File Name : Get-DomainController.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/06/get-computerdomainps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.directoryservices.activedirectory.domain.getcomputerdomain%28VS.80%29.aspx
- .EXAMPLE
- PS c:\foo> .\Get-ComputerDomain.ps1
- Your computer is a member of the cookham.net domain
- Role Holders in this domain:
- PDC Role Holder : Cookham1.cookham.net
- RID Role Holder : Cookham1.cookham.net
- InfraM Role Holder : Cookham1.cookham.net
- #>
- ###
- # Start of script
- ###
- # Get Domain information using static method
- $domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()
- # Display information for current computer
- "Your computer is a member of the {0} domain" -f $domain.name
- "Role Holders in this domain:"
- " PDC Role Holder : {0}" -f $domain.PdcRoleOwner
- " RID Role Holder : {0}" -f $domain.RIDRoleOwner
- " InfraM Role Holder : {0}" -f $domain.InfrastructureRoleOwner
- # End of script
Subscribe to:
Posts (Atom)