【发布时间】:2014-04-23 02:57:16
【问题描述】:
下午好 Stack Overflow。
我正在尝试使用 Powershell 自动化一些基于纸张的流程。我们已经有一个 Powershell 解决方案,其输出是 Excel 电子表格,但为了规划未来,我们认为 CSV 输出比 Excel 更易于使用。
我们所做的非常基础,我们正在与 AD 中的群组所有者联系,并在文本文件中指定。对于列出的每个组,我们循环并添加项目到表中。这是问题所在,我对 Powershell 仍然很陌生,并且在干净地循环遍历“for each”语句时遇到问题。
我遇到以下错误:
Exception calling "Add" with "1" argument(s): "This row already belongs to this table."
At line:77 char:17
+ $table2.Rows.Add <<<< ($row2)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
我觉得分享我正在使用的代码是合适的:
import-module activedirectory
$strGroupList = get-content "C:\Users\MYUSERNAME\Documents\Group Audit\audit.txt"
$strDate = Get-Date
foreach ($strGroup in $strGroupList)
{
$strGroupMember = Get-aduser -Identity $Group -property description
$tabName = "GroupAuditTable"
#Create the Table Object
$table = New-Object system.Data.DataTable "$tabName"
$table2 = New-Object system.Data.DataTable "$tabName2"
#define columns for row 1
$col1 = New-Object system.Data.DataColumn $strGroup,([string])
$col2 = New-Object system.Data.DataColumn $strDate,([string])
#define columns for row 2
$col3 = New-Object system.Data.DataColumn Name,([string])
$col4 = New-Object system.Data.DataColumn Description, ([string])
$col5 = New-Object system.Data.DataColumn NetworkID, ([string])
$col6 = New-Object system.Data.DataColumn Nested, ([string])
#Add the columns to row 1
$table.columns.add($col1)
$table.columns.add($col2)
#Add the columns to row 2
$table2.columns.add($col3)
$table2.columns.add($col4)
$table2.columns.add($col5)
$table2.columns.add($col6)
#create a row
$row = $table.NewRow()
$row2 = $table2.NewRow()
#create an additional row for the titles of columns
$rowTitles = $table.NewRow()
$rowTitles2 = $table2.NewRow()
$strGroupDetails = Get-ADGroupMember -identity $strGroup
foreach ($Group in $strGroupDetails)
{
########################################################################## Check for nested groups
if($Group.ObjectClass -eq "group")
{
$strGroupDetails = Get-ADGroupMember -identity $Group #-Recursive
$strNestedGroupName = $Group.name
########################################################################## List members of nested groups
foreach($strNestedGroup in $strGroupDetails)
{
#$strNestedGroupName = $Group.name
$strGroupMember = Get-aduser -Identity $StrNestedGroup -property description
$row2.Name = $strGroupMember.name
$row2.Description = $strGroupMember.description
$row2.NetworkID = $strGroupMember.SamAccountName
$row2.Nested = "(From NESTED GROUP: " + $strNestedGroupName
$table2.Rows.Add($row2)
}
}
else
{
#enter data into row 2
$row2.Name = $strGroupMember.name
$row2.Description = $strGroupMember.description
$row2.NetworkID = $strGroupMember.SamAccountName
$row2.Nested = "Not Nested"
#Add the row to the table
$table.Rows.Add($row)
#Add the row to the second table
$table2.Rows.Add($row2)
#Display the table
$table | format-table -AutoSize
$table2 | format-table -AutoSize
}
}
}
$tabCsv = $table | export-csv C:\Users\MYUSERNAME\Desktop\Audit\GroupAudit.csv -noType
$tabCsv2 = $table2 | export-csv C:\Users\MYUSERNAME\Desktop\Audit\GroupAudit_2.csv -noType
就是这样,非常基本的东西。
错误在第 77 行:
$table2.Rows.Add($row2)
这在我将“嵌套组” For Each 循环放在那里之前有效,我只是对为什么会破坏它感到困惑,除非我在这里遗漏了一些明显的东西。当我删除它时:
foreach ($Group in $strGroupDetails)
{
########################################################################## Check for nested groups
if($Group.ObjectClass -eq "group")
{
$strGroupDetails = Get-ADGroupMember -identity $Group #-Recursive
$strNestedGroupName = $Group.name
########################################################################## List members of nested groups
foreach($strNestedGroup in $strGroupDetails)
{
#$strNestedGroupName = $Group.name
$strGroupMember = Get-aduser -Identity $StrNestedGroup -property description
$row2.Name = $strGroupMember.name
$row2.Description = $strGroupMember.description
$row2.NetworkID = $strGroupMember.SamAccountName
$row2.Nested = "(From NESTED GROUP: " + $strNestedGroupName
$table2.Rows.Add($row2)
}
}
else
{
#enter data into row 2
一切正常,我错过了什么?
【问题讨论】:
标签: windows excel powershell