XML

The next file type that you must know and need to learn to work with is Extensible Markup Language (XML). It can be used for configuration and data storage. We will start with an example of storing and loading configuration data:

#XMLConfigFile
$XMLContent = @'
<?xml version="1.0" standalone="yes"?>
<Config>
<TargetCollectionDefault>Windows10_1</TargetCollectionDefault>
<TargetCollection>
<Collection company_id = "A">Windows10_1_A</Collection>
<Collection company_id = "B">Windows10_12</Collection>
<Collection company_id = "C">Windows10_1_B</Collection>
</TargetCollection>
<ADLocations>
<Mandant Name="School" Nummer="1" UserName="Guest" OrgName="School" OSDVersion="Win7"
Domain="sl1.contoso.net" DomainDC="DC=sl1,DC=contoso,DC=net"></Mandant>
<Mandant Name="School" Nummer="3" UserName="Guest" OrgName="School" OSDVersion="Win10"
Domain="sl2.contoso.net" DomainDC="DC=sl2,DC=contoso,DC=net"></Mandant>
<Mandant Name="University" Nummer="45" UserName="Student" OrgName="N1" OSDVersion="Win7" Domain="un1.contoso.net" DomainDC="DC=un1,DC=contoso,DC=net"></Mandant>
<Mandant Name="University" Nummer="67" UserName="Student" OrgName="N1" OSDVersion="Win10" Domain="un2.contoso.net" DomainDC="DC=un2,DC=contoso,DC=net"></Mandant>
</ADLocations>
<PCType>
<Type Name = "Desktop PC" Value='D'></Type>
<Type Name = "Notebook" Value='N'></Type>
<Type Name = "Tablet PC" Value='T'></Type>
</PCType>
<!-- Logfile configuration, If this section is uncomented logfile is written in the same folder as the script file.-->
<Logdir>E:Logs</Logdir>
<Logfile>SCCMLogs.txt</Logfile>
</Config>
'@

XML is one of the basic file extensions, and consists of opening and closing tags. Parameters can be added both in a nested fashion and inline. By using the nested approach, you will also have a visual recognition of which objects are on the same level through indentation:

#Path where the config file is being saved
$configPath = 'c: empconfig.xml'

#Saving config file
$XMLContent | Set-Content $configPath

#Loading xml as config
[XML] $configXml = Get-Content -Path $configPath -ErrorAction 'Stop'

#region some examples for loading and filtering data
$configXml.Config.TargetCollectionDefault
$configXml.Config.TargetCollection.Collection
$configXml.Config.ADLocations.Mandant | Where-Object {$_.Name -eq 'School'} | Out-GridView

$NewGUIDforNotebook = ($configXml.Config.PCType.Type | Where-Object {$_.Name -like 'Notebook'}).Value + [guid]::NewGuid()

if ($configXml.Config.Logdir -ne $null)
{
$LogDir = $configXml.Config.Logdir
$LogFile = $configXml.Config.LogFile
}

$LogDir
$LogFile
#endregion

In the previous example, you saw how the XML file can be loaded easily with PowerShell and using the configuration data. The implementation is very straightforward, and involves casting the loaded content with [XML] into an XML file. This allows us to directly work with IntelliSense and find our configuration properties easily. In addition, it is possible to use filtering and searching to find configuration properties. Especially when you are working with very complex configuration files, this technique might come in handy.

In recent times, the use of REST methods has increased. REST can also return XML objects, and their use is very simple. In the following example, an RSS feed is being retrieved as an XML file:

#Retrieving the RSS feed from the MSDN PowerShell blogs
Invoke-RestMethod -Uri https://blogs.msdn.microsoft.com/powershell/feed/ | Select-Object Title, Link, PubDate | Out-GridView

There might be some nice use cases around here. Just think about continuously retrieving the RSS feed and sharing new articles, for example, via social media.

In the next example, we will take a more programmatic approach to finding the values of an XML file by using XPath filters. XPath filters allow us to find and retrieve objects in bigger, more complex XML files:

#Path for Type formattings
$Path = "$PshomeTypes.ps1xml"

#Show
psedit "$PshomeTypes.ps1xml"

#XPath filter for returning Node objects and its ReferencedMemberName
$XPath = "/Types/Type/Members/AliasProperty"

#Retrieving the data
Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node

#XPath filter for finding all types with dedicated formattings
$XPath = "/Types/Type/Name"

#Retrieving the data
Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node

#XPath filter for finding all types with dedicated formattings and its members
$XPath = "//Types"

#Retrieving the data
$ListOfTypes = Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node

#Displaying all Types with its Members
$ListOfTypes.Type

To make use of XPath filters, you can work with the Select-Xml cmdlet and add the XPath filter as string. We used a simple filter here to retrieve the objects for a specific structure with "/Types/Type/Name" , and to retrieve all values for all included types recursively with "//Types".

If you frequently work with complex XML files, it is recommended to learn the XPath syntax in detail.

Details on the XPath syntax can be found here: https://msdn.microsoft.com/en-us/library/ms256471

In the previous example, we worked with the XML file for type formatting. This formatting is automatically used if you work with one of the specified types. We will show a dedicated example to find these formattings in detail.

First, all the types are retrieved and stored in the $ListOfTypes variable:

#Path for Type formattings
$Path = "$PshomeTypes.ps1xml"

#XPath filter for finding all types with dedicated formattings
$XPath = "//Types/Type"

#Retrieving the data
$ListOfTypes = Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node

In the next lines, you further investigate the service and its format:

#Finding the type with Get-Member
Get-Service | Get-Member #TypeName: System.ServiceProcess.ServiceController

#Retrieve the dedicated formatting type for services
$Type_for_Win32_Service = $ListOfTypes | Where-Object {$_.Name -like 'System.ServiceProcess.ServiceController'}

#Taking a look at the default property names
$Type_for_Win32_Service.Members.MemberSet.Members.PropertySet.ReferencedProperties.Name

The TypeName for the services can be retrieved with Get-Member, as you learned in previous chapters. This type is used for filtering the types and retrieving the specific one you are currently investigating. The default property names return Status, Name, and Display, as we can also see in the XML file:


And the difference can be seen if you try to retrieve all the properties one service has:

#Retrieving the first service with standard formatting
(Get-Service)[0]

#Taking a look at all available properties
(Get-Service)[0] | Select-Object * | Format-Table

To work with the specific type data, there are also dedicated cmdlets available:

  • Get-TypeData
  • Update-TypeData
  • Remove-TypeData

These deliver new options to accomplish the same task. Another way to retrieve the type data for the services is as follows:

#Another way to retrieve the TypeData
Get-TypeData | Where-Object {$_.TypeName -like 'System.ServiceProcess.ServiceController'}
You can also create your own type formattings easily and add them to your session.

Further information can be found here: 
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_types.ps1xml

In the past, XML has been widely used as an option for storing application data. XML has some limitations though. It begins to struggle as your XML files increase in size (>1 MB), or if many people try to work with them simultaneously.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.217.134