使用XML在Powershell中填充变量
我需要告诉大家,我在Powershell中编程的时间很短,但有一些基本的问题我目前还不能很好地处理 所以我要做的是将数据预输入到XML文件中以使用这些数据,并将$var填充到powershell中,然后使用这些var来更改Active Directory属性。对于changead属性我很好,但是通过调用XML文件来填充var的自动化过程根本不起作用。我想我没有使用GET方法,这就是为什么我要寻求帮助 这是我的XML文件(我只声明了两个站点) 等等等等 这是我做的脚本的一部分,但我不能得到我想要的结果使用XML在Powershell中填充变量,xml,powershell,Xml,Powershell,我需要告诉大家,我在Powershell中编程的时间很短,但有一些基本的问题我目前还不能很好地处理 所以我要做的是将数据预输入到XML文件中以使用这些数据,并将$var填充到powershell中,然后使用这些var来更改Active Directory属性。对于changead属性我很好,但是通过调用XML文件来填充var的自动化过程根本不起作用。我想我没有使用GET方法,这就是为什么我要寻求帮助 这是我的XML文件(我只声明了两个站点) 等等等等 这是我做的脚本的一部分,但我不能得到我想要的
$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语句都不适合我想要做的过程。有什么建议吗
谢谢
约翰试试这个:
$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
}
}
似乎您的foreach循环中只有一个输入错误(您在…中使用了
$City,而不是…
中的$Site)。另一种清理答案的方法是首先只获取匹配的站点
例:
请注意xpath是区分大小写的,因此$destination必须与XML中的值相同。这里有另一个选项,使用Select XML和Where对象筛选站点,并使用散点技术,使用哈希表和与cmdlet参数名对应的键
与SelectNodes方法相比,此方法的优点是XML区分大小写,如果提供“alma”,并且文件中的值大小写不同,则可能无法获得所需的内容
访问以了解有关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
是的,最好是这样。我会说Thumbs up@C.B.谢谢,我不远,但因为我不习惯使用XML文件,所以更容易出错。
$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
}
}
$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
[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