我寫一個PowerShell腳本,其詢問基於XML配置文件的Active Directory,看起來是這樣的:PowerShell的XML - 遍歷,父節點
<domains>
<domain name="DOMAIN.INTERNAL" exclude="false">
<orgunit name="OU1" exclude="false"/>
<orgunit name="OU2" exclude="false">
<orgunit name="OU3" exclude="false"/>
<orgunit name="OU4" exclude="false"/>
<orgunit name="OU5" exclude="true"/>
</orgunit>
<host name="HOST1" exclude="false"/>
<host name="HOST2" exclude="true" />
<host name="HOST3" exclude="true" />
</domain>
<domain name="SUB.DOMAIN.INTERNAL" exclude="false">
<orgunit name="OU6" exclude="false">
<orgunit name="OU7" exclude="false">
<orgunit name="OU8" exclude="false">
<host name="HOST4" exclude="false" />
</orgunit>
</orgunit>
</orgunit>
<host name="HOST5" exclude="false"/>
<orgunit name="OU7" exclude="true" />
</domain>
</domains>
我加載XML文件,設置XPath和然後選擇不生孩子的組織單元節點:
$currentPath=Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path
[xml]$configFile = Get-Content "$currentPath\WindowsUpdateOMatic.xml"
foreach ($domain in $configFile.domains.domain) {
$xpath = ("/domains/domain[@name=`"") + ($domain.name) + ("`"]//orgunit[@exclude=`"false`"][not (*)]/@name")
foreach($node in Select-Xml -Xpath $xpath $configFile){
# Find parent nodes
$node.Node.ParentNode
}
}
我的計劃是通過與上級單位部門的節點迭代,這樣我可以創建一個OU,例如完全可分辨名稱「OU =巴,OU = FOO,DC =子,DC =域,DC =內部」。
然後我可以查詢活動目錄中的OU並檢索其中的主機。
問題是,$ node.Node.ParentNode的值不會返回任何內容。嘗試各種各樣的東西,我得到它返回「InputStream」,但我不能現在再現。
我對Powershell相當陌生,我很欣賞它,看起來好像我可能在走路前試圖跑步。
感謝。這與我迄今爲止所取得的結果沒有什麼不同 - 除了我遍歷父元素併爲每個組織單元構建一個「FDQN」,然後將其轉換爲函數將FDQN轉換爲DN。你的方式看起來更乾淨。 – biscuitNinja 2014-10-17 13:39:02