微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当使用POWERSHELL将数据添加到表中时,“该行已经属于该表”

下午好堆栈溢出。

我正在尝试使用Powershell自动化一些基于纸张的stream程。 我们已经有了一个Powershell解决scheme,其中的输出一个Excel电子表格,但为了规划未来,我们觉得CSV输出比Excel更容易使用。

我们正在做的是非常基本的,我们正在接触到在AD的组的所有者,在一个文本文件中指定。 对于列出的每个组,我们循环并将项目添加到表中。 问题出在这里,我对Powershell还是很陌生,并且在循环遍历“for each”语句时遇到了问题。

我遇到以下错误

我怎样才能创build一个locking的Python环境?

使用Excel Interop打开Excel工作簿时出现问题

突出显示Excel文档中的行

Saveas excel不能在C#中工作

将图像名称与其大小一起保存到Windows中的文件

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:UsersMYUSERNAMEDocumentsGroup Auditaudit.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:UsersMYUSERNAMEDesktopAuditGroupAudit.csv -noType $tabCsv2 = $table2 | export-csv C:UsersMYUSERNAMEDesktopAuditGroupAudit_2.csv -noType

就是这样,很基本的东西。

错误在#77行:

$table2.Rows.Add($row2)

这工作之前,我坚持“嵌套组”每个循环在那里,我只是困惑,为什么会打破它,除非我在这里明显的东西丢失。 当我删除这个:

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

事情按预期工作,我错过了什么?

使用axlsx gem下载xlsx文件时,Coulmns处于隐藏状态

在不使用ZipArchive类的PHP中读取Excel xlsx文件或启用PHP扩展PHP_zip

我可以使用OleDbConnection Excel ACE驱动程序从不可查找的System.IO.Stream而不是文件读取Excel文件吗?

使用Apache POI打开.xlsx文件会导致出现NoClassDefFoundError InvalidFormatExceptionexception

无效的标题签名; 在Excel文档上使用Apache POI IOException

您正在运行一个ForEach,第一件事是您定义$ strGroupMember的If子句,您在Else子句中为相同的If / Then引用。 在这里,这是简化的:

IF($Group.ObjectClass -eq "group"){ <do stuff> $strGroupMember = get-aduser $strnestedgroup <more parameters> $row2.stuff = $strgroupmember.stuff } Else{ $row2.stuff = $strgroupmember.stuff }

但是你不要在其他脚本块中定义$ strgroupmember。 因此,它添加最后一个组的最后一个,它已经有了这个条目,所以它会抛出一个错误

用假数据….组是管理员,它包含1个组(“工资”,其中有1个成员,“约翰”)和2个用户(“吉姆”和“莎莉”)。

ForEach($Item in get-adgroupmember "Administrators"){ if($Item.ObjectClass -eq "group"){ #Payroll is a group,it gets processed here ForEach($User in $Item){ $Userdata = Get-ADUser $User #Returns Jim's AD info $Row2.name = $Userdata.name $Row2.description = $Userdata.description $Table2.add($Row2) #Adds John to the table } } Else{ #if not a group,process root users here,should process Jim and Sally $Row2.Name = $Userdata.name #$Userdata still contains John's info $Row2.Description = $Userdata.Description #$Userdata still contains John's info $Table2.add($Row2) #attempts to add John to the table again }

希望这一切都有道理。 或者至少可以看到这个问题。 修复? 将一行添加到您的Else scriptblock:

$strGroupMember = Get-aduser -Identity $Group -property description

编辑:我会做一个数组,创建用户对象,并将它们添加到数组,然后遍历嵌套组,然后将该组添加到它应用于每个用户对象,然后将该数据推入一个表,所以你会结束与类似的东西:

Name Description NetworkID nested John John Smith JSmith Payroll,HR Mark Mark Jones MJones Not nested Mary Mary Anderston MAnderson HR

但那就是我。

编辑 2 好的,第二轮! 我看到的变化,但仍然有一些问题是肯定的。 您遇到的问题是向表中添加行。 好吧,可以理解。 以下是我看到的一些问题:

foreach($strnestedGroup in $strGroupDetails) { #$strnestedGroupName = $Group.name $strGroupMember = Get-aduser -Identity $StrnestedGroup -property description $row2.nested = "(From nesTED GROUP: " + $strnestedGroupName $table2.Rows.Add($row2) }

好的,对于嵌套组查询AD中的每个项目,并将结果存储在$strGroupMember 。 然后将$row2.nested更新$row2.nested当前组的名称,并将$row2作为新行添加到表中。 所以,让我们说我们进入这个循环与$row2是这样的:

$row2.Name = "John Smith" $row2.Description = "Director of HR" $row2.NetworkID = "JSmith" $row2.nested = "Not nested"

好吧,它通过它为嵌套组“Power Users”拉入的条目循环。 我们可以很容易地假设这个嵌套组中没有其他组嵌套,这是另一个问题。

该清单上的项目1是marcia Winkle,她有“工资”的AD描述和MWinkle的用户名。 循环找到了,并将其所有信息分配给$strGroupMember 。

接下来,它更新$row2 ,并替换nested值,将“未嵌套”替换为“嵌套组:高级用户”(我们正在使用的组)。

然后它将$row2添加到$table2 ,创建一个行:

Name Description NetworkID nested John Smith Director of HR JSmith From nested group: Power Users

OK完成! 下一个成员是埃塞尔·琼斯。 她也有Payroll的说明,她的用户名是EJones。 循环从AD中拉出来,并用它们替换$strGroupMember任何现有值。 不是那么重要,我们似乎从来没有在最后一个循环中使用任何信息,但这不是脚本的问题,它只是通过!

接下来,我们再次更新$row2.nested ,将“从嵌套组:高级用户”更改为“从嵌套组:高级用户”…呵呵,和以前一样,但是好的,你是老板。 继续!

现在,它试图在表中添加另一行,但是这里是我们得到一个错误的地方:这和上一次数据是完全一样的,因为$ row2中的任何内容都没有真正改变,它仍然是:

Name Description NetworkID nested John Smith Director of HR JSmith From nested group: Power Users

你知道我要去哪里? 我看到的下一个问题是,你正在做一个If / Then / Else循环,但是你试图在Then部分添加行,而不是在Else部分。 在这里,采取这个思想,并相应地更新您的代码

If (Is a Group) Then For Each Member of the group,do this query AD to get user details Update the $row2 variable with the user's details and the current group Add $row2 to the table End For Each loop Else (if it is NOT a group) query AD to get the user's details Update the $row2 variable with the user's details and the current group Add $row2 to the table End Else section

这应该清除你目前的错误。 它不会递减多于一个级别的组,所以如果在一个组内有一个组,那么你将会错过人。 如:

Main Group --->nested Group --->Another nested Group ->Billy Mac

这将包括一个嵌套组和第二个嵌套组,但是除非他在其他地方代表,否则Billy Mac将不会将其列入您的列表中。

至于我的解决办法,昨天晚上我写了大部分,所以今天就写完了。 这应该做你想做的事情,我想。 它创建一个空数组,然后为每个递归找到的用户填充一个对象。 然后它遍历所有嵌套组,并将该组的名称添加到其中每个用户的对象的nested属性。 然后输出该数组作为每个处理组的CSV文件

import-module activedirectory #Define a function to add a group's name to its members nested field recursivly. Function GetnestedMembers{ Param($Group) ForEach($Item in (Get-ADGroupMember $Group)){ if($Item.ObjectClass -eq "group" -and $Global:SubGroups -inotcontains $Item.name){ $Global:SubGroups += $Item.name.tostring() GetnestedMembers $Item }else{ $AllMembers|?{$_.Name -match $Item.Name -and !($_.nested -match $group.name)}|%{$_.nested = "$($_.nested),$($Group.Name.tostring())"} } } } $GroupList = get-content "C:UsersMYUSERNAMEDocumentsGroup Auditaudit.txt" ForEach($Entry in $GroupList){ $SubGroups = @() #Create an empty array $AllMembers = @() #Populate it with all recursive members of the group ForEach($Person in (Get-ADGroupMember $Entry -Recursive)){ $User = Get-ADUser $Person -Property description $AllMembers += New-Object PSObject -Property @{ Name = $Person.Name Description = $User.Description NetworkID = $Person.SamAccountName nested = $Null } } $CurrentGroup = Get-ADGroupMember $Entry #Mark root members as direct group members in the nested field $AllMembers|?{($CurrentGroup | ?{$_.ObjectClass -ne "group"}).name -contains $_.Name}|%{$_.nested = "Direct Member"} #Iterate through all nested groups $CurrentGroup | ?{$_.ObjectClass -eq "group"} | %{GetnestedMembers $_} #If the output path doesn't exist,make it quietly. If(!(Test-Path "C:UsersMYUSERNAMEDocumentsGroup Auditgroups")){$null = New-Item "C:UsersMYUSERNAMEDocumentsGroup Auditgroups" -ItemType directory} #Output to CSV $AllMembers |%{if($_.nested){$_.nested = $_.nested.Trimstart(",")};$_} | Select Name,Description,NetworkID,nested | Export-csv "C:UsersMYUSERNAMEDocumentsGroup Auditgroups$Entry.csv" -NoType@R_647_4045@ion }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐