- /
- /
- /
PowerShell: Read variables from xml-file
Create an xml-file with the following content and save it as config.xml:
<?xml version="1.0" encoding="utf-8"?> <configuration> <General> <Domain>test.local</Domain> <Environment>development</Environment> </General> <Users> <User1>John Doe</User1> <User2>Jane Doe</User2> </Users> <Groups> <Group1>Developers</Group1> <Group2>Managers</Group2> </Groups> </configuration>
Create a powershellscript with the following content and save it in the same folder:
$ScriptPath = Split-Path -parent $PSCommandPath $ConfigFile = "$ScriptPath\config.xml" $ConfigFileExists = Test-Path $ConfigFile If ($ConfigFileExists -eq $False) { write-host -f red "Configfile $ConfigFile does not exist!" exit } [xml]$Config = (Get-Content $ConfigFile) $Domain = $Config.Configuration.General.Domain $Environment = $Config.Configuration.General.Environment $User1 = $Config.Configuration.Users.User1 $User2 = $Config.Configuration.Users.User2 $Group1 = $Config.Configuration.Groups.Group1 $Group2 = $Config.Configuration.Groups.Group2 $Domain $Environment $User1 $User2 $Group1 $Group2
If you run the powershellscript the output is
test.local
development
John Doe
Jane Doe
Developers
Managers