Use XML to populate variable in Powershell

I have to say that iI'm programming in Powershell is for a quiet short time and there are some basic ones that I am not doing very well at the moment.

So what I'm trying to do is to pre-enter data in an XML file to use that data and fill in the $ var in powershell and then use those variables to change the Active Directory attribute. For the AD change attribute I'm fine, but the automation process by calling an XML file to populate my var just doesn't work at all. I think I am not using the got method and why I am asking for help.

Here is my XML file (where I only have two sites 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 var $ Destination name for "-match" location.site.city. If $ destination column match then fill var

$Newcity = location.site.city
$NewStreet = location.site.street
$NewState = location.site.state

      

etc.

Here is a part of the script, but I cannot get the result 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 these vars to change my AD attribute with another 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 attempt faild because I think my Foreach statement followed by my If statement is not appropriate for the process I want to do. Any advice?

Thanks, John

+3


source to share


4 answers


Try the following:



$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
       } 
}

      

+2


source


It also uses the Select-XML and Where-Object options to filter the Site and the Splatting technique using a hash table with keys matching the names of the cmdlet parameters.

The advantage of this and the SelectNodes method is that XML is case sensitive and you may not get what you want if you supply "alma" and the wrapping of the value in the file is different.



Visit this page for more information on 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

      

+3


source


It seems you only had a typo in your foreach loop (you used $City in...

instead $Site in ...

). An alternative to cleaning up your answer would be to get only the relevant site.

Example:

$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

      

Remember that xpath is case sensitive, so $ destination must be identical to the value in XML.

+1


source


solution CB. worked like a charm.

0


source







All Articles