Use XML to fill variable in Powershell
Solution 1
Try this:
$var1 = ""
$Street = ""
$Destination = "Alma"
[xml]$SiteAttribute = Get-Content SitesAttributes.xml
foreach( $Site in $SiteAttribute.location.Site){ #this line in your code has issue
$var1 = $Site.city
If ($var1 -match $Destination){
$NewStreet = $Site.Street
$NewCity = $Site.city
$NewPoBox = $site.POBox
$NewState = $site.State
$Newzip = $Site.zip
$NewCountry = $Site.country
$NewPhone = $Site.OfficePhone
}
}
Solution 2
Here's another option using Select-Xml
and Where-Object
to filter the Site, and a splatting technique, using a hashtable with keys that correspond to the cmdlet parameter names.
The advantage of this vs the SelectNodes()
method is that XML is case-sensitive, and you might not get what you want if you supply 'alma' and the casing of the value in the file is different.
Visit this page for more info about splatting.
[xml]$xml = Get-Content SitesAttributes.xml
$site = $xml | Select-Xml -XPath "//Location/Site" | Where-Object {$_.Node.City -match 'alma'}
$params = @{
StreetAddress = $site.Node.Street
City = $site.Node.city
State = $site.Node.State
PostalCode = $site.Node.zip
Country = $site.Node.country
OfficePhone = $site.Node.OfficePhone
}
Import-Module ActiveDirectory
Set-ADUser $username -server $dc @params
Solution 3
It seems you only had a typo in your foreach loop (you used $City in...
instead of $Site in ...
). An alternative to clean up your answer would be to only get the matching site in the first place.
Ex:
$xml = [xml](Get-Content .\my.xml)
$destination = "Alma"
$sites = $xml.SelectNodes("/Location/Site[City='$destination']")
$sites | % {
$NewStreet = $_.Street
$NewCity = $_.city
$NewPoBox = $_.POBox
$NewState = $_.State
$Newzip = $_.zip
$NewCountry = $_.country
$NewPhone = $_.OfficePhone
}
#Printing variables to test
$NewStreet
$NewCity
$NewPoBox
$NewState
$Newzip
$NewCountry
$NewPhone
Be aware that xpath is case-sensitive so $destination has to be be identical to the value in the XML.
lotirthos227
I've start Powershell January 2013 so quiet new for me please be kind if I ask stupid question, in french we say: there is no stupid questions, only the answer can be. System Adminsitrator since 2005 Microsoft MCITP-Enterprise Admistrator 2008 Microsoft MCSA 2003 VMware Esx 4.1 Background
Updated on June 26, 2022Comments
-
lotirthos227 almost 2 years
I need to tell that iI'm programming in Powershell for quiet short time and there is some basic that I do not handle well for the moment.
So what I'm trying to do is to pre-enter data into a XML file to use those data and populate $var into powershell then use those var to change Active Directory Attribute. For the change AD attribute i'm good, but the automation process by calling XML file to fill my var is simply not working at all. I think i'm not using the got method and that's why i'm asking for help.
Here's my XML file (where I have only two Site declared)
<Location> <Site> <City>Alma</City> <Street>333 Pont Champlain Street</Street> <POBox></POBox> <State>Ontario</State> <Zip>G1Q 1Q9</Zip> <Country>CA</Country> <OfficePhone>+1 555-555-2211</OfficePhone> </Site> <Site> <City>Dolbeau</City> <Street>2525 Avenue du Pinpont</Street> <POBox></POBox> <State>Quebec</State> <Zip>G2Q 2Q9</Zip> <Country>CA</Country> <OfficePhone>+1 555-555-3000</OfficePhone> </Site> </Location>
I want to use a var name $Destination to "-match" location.site.city. If the $destination match city, then fill the var
$Newcity = location.site.city $NewStreet = location.site.street $NewState = location.site.state
etc, etc
Here's the part of script I did but I can't get the result that I want.
$var1 = "" $Street = "" $Destination = "Alma" [xml]$SiteAttribute = Get-Content SitesAttributes.xml foreach( $City in $SiteAttribute.location.Site){ $var1 = $site.city If ($var1 -match $Destination){ $NewStreet = $Site.Street $NewCity = $Site.city $NewPoBox = $site.POBox $NewState = $site.State $Newzip = $Site.zip $NewCountry = $Site.country $NewPhone = $Site.OfficePhone } }
Then I would use those var to change my AD attribute with an other Powershell command
##Normal AD module come with W2k8 Import-Module ActiveDirectory Set-ADUser $username -server $dc -StreetAddress $NewStreet -City $NewCity -State $NewState -PostalCode $NewZip -OfficePhone $NewPhone -Country $NewCountry
But all my try faild, cause I think my Foreach statement followed by my If statement is not adequate for the process I want to do. Any advice ?
Thanks John