2013-01-22 2 views
3

저는 조용한 짧은 시간 동안 Powershell에서 프로그래밍하고 있습니다. 잠시 동안 잘 처리하지 못하는 몇 가지 기본 사항이 있습니다.XML을 사용하여 Powershell에서 변수를 채우십시오.

그래서 내가하려는 것은 데이터를 XML 파일에 미리 입력하여 해당 데이터를 사용하고 $ var를 powershell에 채운 다음 해당 var를 사용하여 Active Directory 특성을 변경하는 것입니다. 변경 광고 특성에 대해,하지만 XML 파일을 호출하여 자동화 프로세스가 내 var를 채우기 위해 단순히 전혀 작동하지 않습니다. 나는 내가 가지고있는 방법을 사용하지 않고 있다고 생각하며, 그래서 나는 도움을 요청하고있다. (나는 단지이 사이트를 선언 한 경우)

는 여기에 내가 var에 이름 $ 대상을 "-match"location.site.city를 사용하려면

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

내 XML 파일입니다. 은 $ 대상 경기 도시 경우, VAR

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

등 등

여기에 내가했던 스크립트의 일부입니다 채우기하지만 내가 원하는 결과를 얻을 수 없습니다. 문이 적절하지 않으면 내 foreach 문 내 뒤에 생각하는 원인

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

그럼, 타 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 

그러나 내 모든 시도의 faild 내 AD 속성을 변경하는 VAR를 사용합니다 내가 원하는 과정을 위해서. 어떤 충고 ? 존

답변

2

이 시도

감사합니다 : 그것은 당신 만 foreach 루프에 오타가 있었다 것

$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

그렇습니다. 나는 Thumbs up @ C.B를 말할 것이다. 고마워, 멀지 않았지만 XML 파일로 작업하는 데 익숙하지 않아 실수를하기 쉽습니다. – lotirthos227

1

(대신 $Site in ...$City 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를 사용하여 사이트를 필터링하는 또 다른 옵션으로, 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 
관련 문제