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.
我需要告诉我,我在Powershell编程安静的短时间,并且有一些我目前处理不好的基本功能。
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.
所以我要做的是将数据预先输入到XML文件中以使用这些数据并将$ var填充到powershell中,然后使用这些var来更改Active Directory属性。对于更改AD属性我很好,但通过调用XML文件来填充我的var的自动化过程根本就不起作用。我想我没有使用得到的方法,这就是我寻求帮助的原因。
Here's my XML file (where I have only two Site declared)
这是我的XML文件(我只声明了两个Site)
<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
我想使用var名称$ Destination来“-match”location.site.city。如果$ destination匹配city,则填充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
然后我将使用这些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
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 ?
但是我所有的尝试都会失败,因为我认为我的Foreach语句后跟我的If语句不适合我想要的过程。任何建议?
Thanks John
3 个解决方案
#1
2
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
}
}
#2
3
Here's another option using Select-XML and Where-Object to filter the Site, and a Splatting technic, using a hashtable with keys that correspond to the cmdlet parameter names.
这是使用Select-XML和Where-Object过滤站点的另一个选项,以及Splatting技术,使用带有与cmdlet参数名称对应的键的哈希表。
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.
这与SelectNodes方法的优点是XML区分大小写,如果提供“alma”并且文件中值的大小不同,则可能无法得到所需的XML。
Visit this page for more info about splatting.
有关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
1
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.
看起来你的foreach循环中只有一个拼写错误(你使用$ City而不是$ Site in ...)。清理答案的另一种方法是首先只获得匹配的网站。
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.
请注意,xpath区分大小写,因此$ destination必须与XML中的值相同。
#1
2
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
}
}
#2
3
Here's another option using Select-XML and Where-Object to filter the Site, and a Splatting technic, using a hashtable with keys that correspond to the cmdlet parameter names.
这是使用Select-XML和Where-Object过滤站点的另一个选项,以及Splatting技术,使用带有与cmdlet参数名称对应的键的哈希表。
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.
这与SelectNodes方法的优点是XML区分大小写,如果提供“alma”并且文件中值的大小不同,则可能无法得到所需的XML。
Visit this page for more info about splatting.
有关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
1
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.
看起来你的foreach循环中只有一个拼写错误(你使用$ City而不是$ Site in ...)。清理答案的另一种方法是首先只获得匹配的网站。
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.
请注意,xpath区分大小写,因此$ destination必须与XML中的值相同。