Power-XML.psm1
<#
.NOTES -------------------------------------------------------------------------------- Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2015 v4.2.98 Generated on: 07-Dec-15 2:20 PM Generated by: Organization: -------------------------------------------------------------------------------- .DESCRIPTION Script generated by PowerShell Studio 2015 #> <# =========================================================================== Created with: SAPIEN Technologies, Inc., PowerShell Studio 2015 v4.2.97 Created on: 02-Dec-15 3:52 PM Created by: Marc Collins Filename: Power-XML.psm1 ------------------------------------------------------------------------- Module Name: Power-XML =========================================================================== .DESCRIPTION Functions for XML Creation and data manipulation. Usage: #-New-XML Returns the Root Node, to be used throughout the Script $XMLRootNode = New-XML #-NewNode returns a Subnode of the parent node provided $XMLSubNode = New-XMLNode -Name "Test" -Parent $XMLRootNode #-Add-XMLAttribute adds a Name/Value Pair to the node specified Add-XMLAttribute -Name "Creation_Date" -Value ((Get-date).ToString()) -Node $XMLRootNode Add-XMLAttribute -Name "RNAttName" -Value "RNAttValue" -Node $XMLRootNode Add-XMLAttribute -Name "SNAttName" -Value "SNAttValue" -Node $XMLSubNode #-Export-XML saves the Provided Root Node to the Destination Export-XML $XMLRootNode -FileDestination C:\temp\ccc.xml #> # .ExternalHelp Power-XML.psm1-help.xml function New-XML { [OutputType([System.Object])] param () #---Creates a XML Object--- $XMLObject = New-Object XML #---Sets XML Object Declarations--- $xmldecl = $XMLObject.CreateXmlDeclaration("1.0", "UTF-8", "yes") #---Creates Root Element in XML--- $XMLRootNode = $XMLObject.CreateElement("root") #---Appends the date to the header of the XML File $XMLRootNode.SetAttribute("Creation_Date", (Get-date).ToString("s")) #---Puts Declaration and Root Element into XML Object--- $XMLObject.InsertBefore($xmldecl, $XMLObject.DocumentElement) | Out-Null return $XMLRootNode } # .ExternalHelp Power-XML.psm1-help.xml function Export-XML { [CmdletBinding()] [OutputType([System.Object])] param ( [Parameter(Position = 0)] [Object]$XMLObject, [Parameter(Position = 1)] [String]$FileDestination ) Test-PathValidation -Path $FileDestination -Create $FileDestinationInfo = [system.io.directoryinfo]$FileDestination if ($FileDestinationInfo.Extension -ne ".xml") { if (!$FileDestination.EndsWith('\')) { $FileDestination = $FileDestination + "\" } $XMLOutputfile = $FileDestination + $env:COMPUTERNAME + ".xml" } else { $XMLOutputfile = $FileDestination } $XMLObject.OwnerDocument.AppendChild($XMLObject) | Out-Null #--Save XMLOutput to file-- $XMLObject.OwnerDocument.Save($XMLOutputfile) } # .ExternalHelp Power-XML.psm1-help.xml function Add-XMLNode { [CmdletBinding()] [OutputType([System.Object])] param ( [Parameter(Position = 0)] [Object]$Name, [Parameter(Position = 1)] [Object]$Parent ) $NewXMLNode = $Parent.OwnerDocument.CreateElement($Name) $Parent.AppendChild($NewXMLNode) | Out-Null Return $NewXMLNode } # .ExternalHelp Power-XML.psm1-help.xml function Add-XMLAttribute { [OutputType([System.Object])] param ( [Parameter(Position = 0)] [Object]$Node, [Parameter(Position = 1)] [String]$Name, [Parameter(Position = 2)] [String]$Value ) $Node.SetAttribute($Name, $Value) } function Add-ComplexXML ($Object, $NodeName, $Depth, $XMLObject) { if ($Depth -eq $null) { $Depth = 1 } $ConvertedXML = convertto-xml -as Document -inputObject ($Object) -depth $Depth -NoTypeInformation $NewNamedNode = Add-XMLNode -Name $NodeName -Parent $XMLObject $NewNamedNode.InnerXml = $ConvertedXML.Objects.object.InnerXml } # .ExternalHelp Power-XML.psm1-help.xml function Test-PathValidation { [CmdletBinding()] [OutputType([System.Object])] param ( [Parameter(Position = 0)] [Object]$Path, [Parameter(Position = 1)] [switch]$Create, [Parameter(Position = 2)] [switch]$Return ) #$info = Get-Item $Path -ErrorAction SilentlyContinue [system.io.directoryinfo]$info = $path if ($info.Extension -eq "") { if (! $info.FullName.EndsWith('\')) { $Path = $Path + "\" } } else { if (! $info.Parent.FullName.EndsWith('\')) { $Path = $info.Parent.FullName + "\" } } #$folderinfo = Get-Item $Path $folderinfo = [system.io.directoryinfo]$Path if ($Create -and (!$folderinfo.Exists)) { New-Item -Type directory -Path $Path | Out-Null } else { if (!$Return -and (!$folderinfo.Exists)) { write-host "Directory $path Does Not Exist Use Did you want to Create?" Break } } if ($Return) { return $path } } function Export-ADObjectXML ($Objects, $FileDestination, $NodeName) { $XMLDataObject = New-XML Add-ObjectProperties -Objects $Objects -NodeName $NodeName -XMLDataObject $XMLDataObject Export-XML -XMLObject $XMLDataObject -FileDestination $FileDestination } function Export-ObjectXML ($Objects, $FileDestination, $NodeName) { $XMLDataObject = New-XML Add-ObjectCIMProperties -Objects $Objects -NodeName $NodeName -XMLDataObject $XMLDataObject Export-XML -XMLObject $XMLDataObject -FileDestination $FileDestination } function Add-ObjectCIMProperties ($Objects, $NodeName, $XMLDataObject) { $i = 1 $ObjCount = $Objects.count foreach ($Object in $Objects) { $complete = [math]::Round($i/$ObjCount * 100, 2) Write-progress -activity "Processing $nodename" -status "$complete% Complete:" -percentcomplete $complete; $i++ $ObjectNode = Add-XMLNode -Name $NodeName -Parent $XMLDataObject foreach ($Property in $Object.CimInstanceProperties.name) { $Prop = $Object.$($Property) $Value = "" if ($Prop -ne $null) { if ($Prop.GetType().name -eq "DateTime") { $Value = $Prop.ToString("s") } } if ($Value -eq "") { $Value = [string]$Object.$($Property) } Add-XMLAttribute -Node $ObjectNode -Name $Property -Value $Value } } } function Add-ObjectProperties ($Objects, $NodeName, $XMLDataObject) { $i = 1 $ObjCount = $Objects.count foreach ($Object in $Objects) { $complete = [math]::Round($i/$ObjCount * 100, 2) Write-progress -activity "Processing $nodename" -status "$complete% Complete:" -percentcomplete $complete; $i++ $ObjectNode = Add-XMLNode -Name $NodeName -Parent $XMLDataObject foreach ($Property in $Object.PropertyNames) { $ObjectProp = $Object.$($Property) $Value = "" if ($ObjectProp -ne $null) { if ($ObjectProp.GetType().name -eq "DateTime") { $Value = $ObjectProp.ToString("s") } } if ($Value -eq "") { $Value = [string]$ObjectProp } Add-XMLAttribute -Node $ObjectNode -Name $Property -Value $Value } } } Export-ModuleMember New-XML, Export-XML, Add-XMLAttribute, Add-XMLNode, Test-PathValidation, Export-ADObjectXML, Export-ObjectXML, Add-ComplexXML, Add-ObjectProperties, Add-ObjectCIMProperties |