【问题标题】:Export user's group memberships to a text file将用户的组成员身份导出到文本文件
【发布时间】:2022-03-16 17:37:46
【问题描述】:

我是 powershell 新手,在摆弄了大约 3 个小时之后......我在问专家......

我需要设置一个脚本,提示输入用户的广告 ID,然后仅将他/她所在组的规范名称导出到文本文件中。

【问题讨论】:

  • 嗨,欢迎来到 SO。这不是代码编写服务。尝试自己编写脚本并在遇到具体问题时在此处发布。另见this
  • 酷,我会想办法的......

标签: powershell active-directory


【解决方案1】:

好吧,至少要让你开始你想要查看 cmdlet

Get-ADPrincipalGroupMembership

如果您执行“Help Get-ADPrincipalGroupMembership”,您可以看到参数。它接受 -identity ,因此您可以给它一个广告用户的 samacount。

Get-ADPrincipalGroupMembership -Identity testuser

这将返回您真正想要的更多信息,因此最后您可以通过选择特定属性将其过滤到您真正需要的信息。

Get-ADPrincipalGroupMembership -Identity testuser | select distinguishedname

要提示某人输入信息,您可以执行“Read-Host”。除了这些研究变量之外,您还应该能够将所有这些组合在一起,从而提示用户输入 id 然后进行搜索。

【讨论】:

  • Noah,你今天赢得了互联网......我在 principalgroupmembership 事情上已经做到了,但在过滤输出时完全被难住了!
  • 不客气,很高兴能为您提供一点帮助。除了使用帮助之外,还有一些技巧是“Get-Command”,您可以使用它来探索您可能需要的 cmdlet,例如,如果您正在寻找 AD cmdlet,您可以使用 Get-Command *AD*。另一个技巧是研究“Get-Member”。你可以通过管道来探索对象类型和可用属性。
【解决方案2】:

不久前,我创建了这样一个带有 GUI 的脚本供我的同事使用。 您只需输入用户,它就会将组名、组类别(安全/分发)和组范围(全局/通用)导出到 csv 文件中 我认为最好将组导出为 csv 文件而不是 txt 文件。

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

Function ExportMailboxMembers{
$Input = $InputBox.Text
$ReportPath = “C:\temp\UserGroups.csv“

Get-ADPrincipalGroupMembership $Input | select name, groupcategory, groupscope | 
export-CSV “C:\temp\UserGroups.csv“

Add-OutputBoxLine -Message "The csv file has been succesfully exported to 
C:\temp\UserGroups.csv
                       You may now click the EXIT button."
}

Function Add-OutputBoxLine {
    Param ($Message)
    $OutputBox.AppendText("`r`n$Message")
    $OutputBox.Refresh()
    $OutputBox.ScrollToCaret()
}

#Creating the Form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Export User Groups to CSV ~ made by Victor G." 
$Form.size = New-Object System.Drawing.Size (800,600)

#Adding Label 1
$Label1 = New-Object System.Windows.Forms.Label

$Label1.Text = "Imput the user for which you wish to export the groups to a CSV 
file (ex. jsnow)"
$Label1.Location  = New-Object System.Drawing.Point(10,20)
$Label1.AutoSize = $true
$Form.Controls.Add($Label1)

#Adding Label 2
$Label2 = New-Object System.Windows.Forms.Label

$Label2.Text = "System response here:"
$Label2.Location  = New-Object System.Drawing.Point(10,120)
$Label2.AutoSize = $true
$Form.Controls.Add($Label2)

#Adding imput box
$InputBox = New-Object System.Windows.Forms.TextBox
$InputBox.Location = New-Object System.Drawing.Size (20,50)
$InputBox.Size = New-Object System.Drawing.Size (300,20)
$Form.Controls.Add($InputBox)

#Adding an outbox
$OutputBox = New-Object System.Windows.Forms.TextBox
$OutputBox.Location = New-Object System.Drawing.Size (10,150)
$OutputBox.Size = New-Object System.Drawing.Size(500,300)
$OutputBox.Font = New-Object System.Drawing.Font("Arial",12, 
[System.Drawing.FontStyle]::Regular)
$OutputBox.Multiline = $true
$OutputBox.ScrollBars = "Vertical"
$OutputBox.ReadOnly = $True
$Form.Controls.Add($OutputBox)

#Adding the button
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size (600,50)
$Button.Size = New-Object System.Drawing.Size (150,100)
$Button.Text = "Export groups"
$Button.Add_click({ExportMailboxMembers})
$Form.Controls.Add($Button)

$ButtonCloseSkript = New-Object System.Windows.Forms.Button 
$ButtonCloseSkript.Location = New-Object System.Drawing.Size(650,500) 
$ButtonCloseSkript.Size = New-Object System.Drawing.Size(92,30) 
$ButtonCloseSkript.Text = "EXIT" 
$ButtonCloseSkript.Add_Click({$Form.Close()})#On click do function
$ButtonCloseSkript.Cursor = [System.Windows.Forms.Cursors]::Hand 
$Form.Controls.Add($ButtonCloseSkript)

$Form.ShowDialog()

【讨论】:

    【解决方案3】:

    这是我用来获取用户通讯组的脚本。它还将为您提供在最后从所有组中删除用户的选项,我在转发已离职员工的电子邮件时使用该选项。这可以很容易地修改为导出到文本文件而不是打印到屏幕上。

    write-host ""
    $User = Get-Mailbox (Read-Host -Prompt "User Name") | Select-Object Name,DistinguishedName
    
    
    #script
    
    Write-Host ""
    $User.Name + " is a member of the following groups:"
    Write-Host ""
    
    
    $groups = (Get-DistributionGroup -ResultSize unlimited | Select-Object identity,displayname)
    $i = $groups.Count
    $j = 0
    
        $usersgroups = @(
            foreach ($group in $groups) {
                Write-Progress -Activity $group.DisplayName -Status "$i Groups Remaining.."  -PercentComplete (($j / $groups.count)*100)
                $i--
                $j++
    
                if ((Get-DistributionGroupMember $group.identity | Select-Object -ExpandProperty distinguishedname) -contains $user.DistinguishedName) {
                    $group.displayname
                    Write-Host $group.DisplayName
                }
            }
        )
    
    
    
    #option to remove user from groups
    
    Write-Host ""
    if ((Read-Host "Do you want to remove user from these groups? y/n").ToLower() -like 'y*') { 
        Write-Host ""
    
        foreach ($group in $usersgroups){
            Remove-DistributionGroupMember -Identity $group -Member $User.Name -confirm: $false -Verbose
        }
    Write-Host ""
    Write-Host "Done!"
    } ELSE {
    Write-Host ""
    Write-Host "Aborting Script!"
    }
    

    【讨论】:

    • 虽然这是一个很好的脚本,但它并不能回答操作问题。这是与 Exchange 一起使用的,我认为该操作正在寻找所有 AD 组。也请不要支持不好的问题。
    猜你喜欢
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多