2014-07-16 81 views
0

這是我第一次嘗試創建XML文件。我正在嘗試編寫一個查詢我們的GPO列表的腳本,並生成一個具有GPO名稱和應用程序服務器名稱的XML文件。Powershell和XML文件創建

我遇到了兩個問題:

  • 的XML文件與第一個GPO創建的,但我得到的所有服務器的「節點名」標籤,而不是許多。

< MasterList>
<標籤>
<標籤名> WSUS-ALPHA < /標籤名>
<節點名> SERVER1 SERVER2 SERVER3 SERVER4 < /節點名>
< /標籤>
</MasterList>

  • 第一GPO寫入到XML文件後,我收到以下錯誤爲休息:

異常調用「WriteStartElement」與「1」的說法(S):「作者是關閉。」
在行:3焦炭:5
+ $ xmlWriter.WriteStartElement( '標籤')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~
+ CategoryInfo:NotSpecified:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:出現InvalidOperationException

異常調用 「WriteElementString」 與 「2」 的參數(一個或多個):「令牌起始元素在狀態錯誤將導致 無效的XML文檔。「 在線:4 char:5 + $ xmlWriter.WriteElementString('TagName',$ gpo.DisplayName) + ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId: InvalidOperationException

使用「2」參數調用「WriteElementString」的異常:「狀態錯誤中的Token StartElement將導致 無效的XML文檔。
在行:10字符:13
+ $ xmlWriter.WriteElementString( '節點名',(GET-ADGroupMember -Identit ...
+ ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~
+ CategoryInfo:NotSpecified:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:出現InvalidOperationException

異常調用 「對writeEndElement」 與 「0」 的參數(一個或多個) :「沒有XML開始標記打開。」
行:13 char:5
+ $ xmlWriter。對writeEndElement()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:NotSpecified:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:InvalidOperationException

使用「0」參數調用「WriteEndElement」的異常:「沒有打開XML開始標記」。
在行:14焦炭:5
+ $ xmlWriter.WriteEndElement()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:NotSpecified:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:出現InvalidOperationException

異常調用 「沖洗」 與 「0」 的參數(一個或多個): 「無法寫入到關閉的TextWriter」
在行:15焦炭:5
+ $ xmlWriter.Flush()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo:NotSpecified:(:) [ ],MethodInvocationException
+ FullyQualifiedErrorId:的ObjectDisposedException

這裏是代碼我使用:

$xmlFile = "c:\data-$randomnumber.xml" 
$xmlWriter = New-Object System.XMl.XmlTextWriter($xmlFile,$Null) 
$xmlWriter.Formatting = 'Indented' 
$xmlWriter.Indentation = 1 
$xmlWriter.IndentChar = "`t" 
$xmlWriter.WriteStartElement('MasterList') 

#Get the list of GPOs that start with "wsus". 
$wsusGPOs = Get-GPO -All | where {$_.DisplayName -like 'wsus*'} 

Foreach ($gpo in $wsusGPOs) { 
    #For each GPO that starts with "wsus", create a tag with the GPO's name. 
    $xmlWriter.WriteStartElement('Tag') 
    $xmlWriter.WriteElementString('TagName',$gpo.DisplayName) 

    #Get the list of AD groups that have the "Apply" permission, in each GPO. 
    $wsusPerms = $gpo | Get-GPPermission -All | where {$_.permission -eq 'GpoApply' -and $_.denied -eq $false} 
    Foreach ($permsList in $wsusPerms) { 
     If ($permsList.trustee.name -ne 'Authenticated Users') { 
      #For each AD group that can apply the GPO, get the list of servers in the AD group. Ignores the "Authenticated Users" group. 
      $xmlWriter.WriteElementString('NodeName',(Get-ADGroupMember -Identity $permsList.Trustee.name -Recursive).name) 
     } 
    } 

    $xmlWriter.WriteEndElement() 
    $xmlWriter.WriteEndElement() 
    $xmlWriter.Flush() 
    $xmlWriter.Close() 

    } 

我試圖移動$ xmlWriter.Flush()和$ xmlWriter.Close()命令了頂級的forea ch循環,但沒有幫助。

我覺得這兩個問題應該是相當直接的解決,我只是不知道如何。有關如何修改我的代碼的任何想法?

謝謝。

+0

不知道它是否會有所作爲,但'XmlTextWriter'構造文檔說'與.NET Framework 2.0開始,我們建議您使用的XmlWriter創建的XmlWriter實例。創建方法和XmlWriterSettings類以利用新功能 – Eris

回答

0

我結束了重新排序的命令位。這裏是工作代碼:

$randomnumber = Get-Random -minimum 100 -maximum 10000 
$xmlFile = "c:\data-$randomnumber.xml" 
$xmlWriter = New-Object System.XMl.XmlTextWriter($xmlFile,$Null) 
$xmlWriter.Formatting = 'Indented' 
$xmlWriter.Indentation = 1 
$xmlWriter.IndentChar = "`t" 
$xmlWriter.WriteStartElement('MasterList') 

#Get the list of GPOs that start with "wsus". 
$wsusGPOs = Get-GPO -All | where {$_.DisplayName -like 'wsus*'} 

Foreach ($gpo in $wsusGPOs) { 
#Get the list of AD groups that have the "Apply" permission, in each GPO. 
$wsusPerms = $gpo | Get-GPPermission -All | where {$_.permission -eq 'GpoApply' -and $_.denied -eq $false} 

    Foreach ($securityGroup in $wsusPerms) { 
     If ($securityGroup.trustee.name -ne 'Authenticated Users') { 
      #For each AD group that can apply the GPO, get the list of servers in the AD group. Ignores the "Authenticated Users" group. 
      Foreach ($computer in (Get-ADGroupMember -Identity $securityGroup.Trustee.name -Recursive).name) { 
       $xmlWriter.WriteStartElement('Tag') 
       $xmlWriter.WriteElementString('TagName',$gpo.DisplayName) 
       $xmlWriter.WriteElementString('NodeName',$computer) 
       $xmlWriter.WriteEndElement() #Closes the "Tag" element 
      } 
     } 
    } 
} 
$xmlWriter.WriteEndElement() #Closes the "MasterList" top-level element 
$xmlWriter.Flush() 
$xmlWriter.Close() 
$xmlContent = [IO.File]::ReadAllText($xmlFile) 
0

嘗試在循環增加對writeEndElement()的調用:

... 
Foreach ($permsList in $wsusPerms) { 
    If ($permsList.trustee.name -ne 'Authenticated Users') { 
     #For each AD group that can apply the GPO, get the list of servers in the AD group. Ignores the "Authenticated Users" group. 
     $xmlWriter.WriteElementString('NodeName',(Get-ADGroupMember -Identity $permsList.Trustee.name -Recursive).name) 
     $xmlWriter.WriteEndElement() 
    } 
} 
...