2013-01-22 101 views
3

我需要告诉我,我是在短时间内安装Powershell的编程,并且有一些基本问题我暂时无法处理。使用XML填充Powershell中的变量

所以我想要做的是预先输入数据到一个XML文件来使用这些数据并将$ var填充到powershell中,然后使用这些var来更改Active Directory属性。对于更改AD属性我很好,但通过调用XML文件来填充我的var的自动化过程根本就不起作用。我想我没有使用得到的方法,这就是为什么我要求帮助。

这是我的XML文件(其中我只有两个网站声明)

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

我想用一个变量名称$目的地为“-match” location.site.city。如果$目的地配城,然后填写了var

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

等,等

这里的脚本我做的一部分,但我不能得到我想要的结果。

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

然后我会用这些变种改变我的AD属性与其他PowerShell命令

##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 

但我都尝试zhcon失败,原因我想接着我的我的foreach语句如果声明是不够的为我想要做的过程。有什么建议?

感谢 约翰

回答

2

试试这个:

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

是的,这样更好。我会说大拇指@ C.B。谢谢,我不是很远,但因为我不习惯使用XML文件,所以很容易犯错。 – lotirthos227

1

看来你只能在你的foreach循环有一个错字(你用$City in...代替$Site in ...)。清理答案的另一种方法是首先获取匹配网站。

例:

$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 

注意,XPath是区分大小写,因此$目的地必须是相同的在XML值。

3

下面是使用Select-XML和Where-Object过滤站点以及Splatting技术的另一个选项,它使用带有与cmdlet参数名称对应的键的散列表。

这与SelectNodes方法的优点是XML区分大小写,如果您提供'alma'并且文件中值的大小写不同,您可能无法获得所需内容。

访问this page获取更多关于溅镀的信息。

[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