这是我第一次尝试创建XML文件。我正在尝试编写一个查询GPO列表的脚本,并生成一个XML文件,其中包含GPO名称和应用的服务器名称。
我遇到两个问题:
< MasterList>
<标签>
< TagName> WSUS-ALPHA< / TagName>
< NodeName> SERVER1 SERVER2 SERVER3 SERVER4< / NodeName>
< / Tag>
< / MasterList>
异常调用" WriteStartElement"用" 1"参数:"作家关闭。"
在行:3 char:5
+ $ xmlWriter.WriteStartElement(' Tag')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:InvalidOperationException异常调用" WriteElementString"用" 2"参数:"状态错误中的令牌StartElement会导致 无效的XML文档。" 在行:4 char:5 + $ xmlWriter.WriteElementString(' TagName',$ gpo.DisplayName) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:InvalidOperationException
异常调用" WriteElementString"用" 2"参数:"状态错误中的令牌StartElement会导致 无效的XML文档。"
在行:10 char:13
+ $ xmlWriter.WriteElementString(' NodeName',(Get-ADGroupMember -Identit ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:NotSpecified:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:InvalidOperationException异常调用" WriteEndElement"用" 0"参数:"没有打开XML开始标记。"
在行:13 char:5
+ $ xmlWriter.WriteEndElement()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:InvalidOperationException异常调用" WriteEndElement"用" 0"参数:"没有打开XML开始标记。"
在行:14 char:5
+ $ xmlWriter.WriteEndElement()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:InvalidOperationException异常通话" Flush"用" 0"参数:"无法写入已关闭的TextWriter。"
在行:15 char: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()命令移出顶级foreach循环,但这没有帮助。
我觉得这两个问题应该是相当直接的解决,我只是不确定如何。关于如何修改我的代码的任何想法?
感谢。
答案 0 :(得分: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()
}
}
...
答案 1 :(得分: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)