3

我需要告诉我,我在 Powershell 中编程的时间很短,而且有一些基本的东西我暂时不能很好地处理。

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

这是我的 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>

我想使用 var 名称 $Destination 来“匹配”location.site.city。如果 $destination 匹配城市,则填充 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
       } 
}

然后我会使用这些 var 用另一个 Powershell 命令更改我的 AD 属性

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

但是我所有的尝试都失败了,因为我认为我的 Foreach 语句后面跟着我的 If 语句对于我想做的过程来说是不够的。有什么建议吗?

谢谢约翰

4

3 回答 3

3

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

与该SelectNodes()方法相比,此方法的优点是 XML 区分大小写,如果您提供 'alma' 并且文件中值的大小写不同,您可能无法获得想要的结果。

访问此页面以获取有关喷溅的更多信息。

[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
于 2013-01-22T14:22:17.240 回答
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
       } 
}
于 2013-01-22T13:28:06.310 回答
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 区分大小写,因此 $destination 必须与 XML 中的值相同。

于 2013-01-22T13:40:33.867 回答