首页 > 解决方案 > Powershell 脚本获取 AD 组报告

问题描述

我正在编写一个脚本,但遇到了一个错误。我想看看是否有人能指出我正确的方向。基本上我希望用户搜索 AD 并生成特定组中用户的 .CSV 文件的报告。我将在下面显示脚本和错误。任何建议都将受到欢迎。我知道它可以做得更好和清理,但现在,它有效。

<# 
.NOTES
===========================================================================
Created with: PowerShell
Created on:   6/8/2020 3:51 PM
Created by:   William Christner
Organization: BCBSA
Filename:     AD Group Report
===========================================================================
.DESCRIPTION
Search AD groups, create a .CSV report of users in the group.
#>

####################################################################
import-module ActiveDirectory
####################################################################


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

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Select AD Group'
$form.Size = New-Object System.Drawing.Size(350, 140)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(125, 50)
$okButton.Size = New-Object System.Drawing.Size(75, 23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$okButton.add_Click({
######################################################################################################
$GroupName = "$textBox"
$Date = Get-Date -UFormat "%m.%d.%y_%H.%M"
$Members = Get-ADGroupMember -Identity $GroupName -Recursive | Select -ExpandProperty SAMAccountName

Foreach ($Name in $Members) {
Get-ADUser $Name -properties * | Select EmailAddress,SAMAccountName,DisplayName,OfficePhone,CanonicalName,Enabled |
Export-Csv -append C:\Reports\$($GroupName)_Membership_Detailed_$($Date).csv -NoTypeInformation -Encoding UTF8 }
})
#####################################################################################################################

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(225, 50)
$cancelButton.Size = New-Object System.Drawing.Size(75, 23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10, 20)
$label.Size = New-Object System.Drawing.Size(130, 20)
$label.Text = 'Please select AD Group:'
$form.Controls.Add($label)


$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(150, 15)
$textBox.Size = New-Object System.Drawing.Size(150, 40)
$form.Controls.Add($textBox)
$textbox.AutoCompleteSource = 'CustomSource'
$textbox.AutoCompleteMode = 'SuggestAppend'
$textbox.AutoCompleteCustomSource = $autocomplete
"awsarchitect",
"BHI",
"cc_support",
"Citrix",
"CyberArk",
"CyberArk_Admins",
"Datastage",
"DB2DBA",
"Distribution Groups",
"DS_Core_Security",
"DS_FEPSIU",
"ems-test",
"FAS_COLDFUSION_DEV",
"FEPBlue",
"FEPOC_MousePointer",
"FogOperations",
"GG_AcceletestAdmins",
"GG_AcceletestUsers",
"GG_Allow LinkedIn",
"GG_Allow_AWS",
"GG_Allow_Firewall_ERwin",
"GG_Allow_Firewall_Hyper-V",
"GG_Allow_Firewall_IBM_Websphere",
"GG_Allow_Firewall_Loadrunner",
"GG_Allow_Firewall_Mainframe",
"GG_Allow_Firewall_NEBA_FTP",
"GG_Allow_Firewall_nodejs",
"GG_Allow_Firewall_Observer_Platform",
"GG_Allow_Firewall_R_Selenium",
"GG_Allow_Firewall_Safeend_DPA",
"GG_Allow_Firewall_SoapUI",
"GG_Allow_Lockdown",
"GG_Allow_MS_Collab_Sites",
"GG_Allow_Skype",
"GG_Allow_Website_Menshealth",
"GG_Allow_Windows_GINA",
"GG_Allow_Wireless",
"GG_Ansible_Development",
"GG_ApplicationSupport_Users",
"gg_bcbsa_sp_audit",
"GG_BindAccounts",
"GG_Blackberry_SSP",
"GG_Chrome_NoPolicies",
"GG_Clearwell_Admin",
"GG_Container_Admins_Nonprod",
"GG_ContentDistributor",
"GG_Contingent",
"GG_Core_GPO",
"GG_Core_GPO_POC",
"GG_Core_Mid",
"GG_CredSSP_Vuln",
"GG_CTX_Allow_Shadow",
"GG_DEP_Disabled",
"GG_DevFep",
"GG_DriveMappings_Test_Group",
"GG_Dropbox ",
"GG_DUOAlerts",
"GG_eDiscovery_Collections",
"GG_EMS_Files",
"GG_EV_Client11_Pilot",
"GG_EV_Phase2_Pilot",
"GG_Exchange_CalendarAdmins",
"GG_Exchange_ISAdmin",
"GG_Exchange_JrAdmin",
"GG_Exchange_MidAdmin",
"GG_Exchange_PowerShell",
"GG_Exchange_SrAdmin",
"GG_Exchange_Training",
"GG_Extend_ScreenSaver",
"gg_fep_contingents",
"GG_FEP_PS_Admins",
"GG_FEP_SIU",
"GG_FEP_SIU_Legal",
"gg_fep_sr_staff",
"gg_fep_staff",
"GG_Firewall_Testing",
"GG_Firewall_Triage",
"GG_Foglight_Read",
"GG_Git-Admins",
"GG_Git-Users",
"GG_Google_Man",
"GG_Google_SU",
"GG_GPO-Arch",
"GG_IBC_SandBox",
"GG_IDN_Pilot",
"GG_Infosys",
"GG_iSight-SRM",
"GG_JumpServers",
"GG_L4CERT_Administrators",
"GG_Legal_Hold",
"GG_Legal_Hold_Pilot",
"GG_LocalProfileRedirect_CHG",
"GG_LocalProfileRedirect_MDC",
"GG_Lost_Devices",
"GG_LTDMailbox",
"GG_Microsoft_Patch_Alerts",
"GG_Mobile_Users_CHG",
"GG_Mobile_Users_Execs",
"GG_Mobile_Users_MDC",
"GG_MSP_Infosys",
"GG_MSP_TechMahindra",
"GG_MSP_UST",
"GG_No_WPAD",
"GG_NRS_Extracts_Access",
"GG_O365_Migration",
"GG_OneDrive_Pilot",
"GG_PeopleSoft_Testers",
"GG_Proxy_WPAD2",
"GG_RDM_ADMIN_NP",
"GG_RDM_Admin_p",
"GG_RDM_ALL",
"GG_RDM_APP_NP",
"GG_RDM_BDC",
"GG_RDM_BLUE2",
"GG_RDM_IPDS",
"GG_RDM_ITS",
"GG_RDM_LDLA",
"GG_RDM_PDSU",
"GG_RDM_TREATMENT_CATEGORY",
"GG_RDM_USER_NP",
"GG_RDM_User_p",
"GG_RDM_WEB_SERVICES",
"GG_RDP_Users ",
"GG_RecordPoint365_Admins",
"GG_Remove_ScreenSaver",
"GG_RMS_SuperUsers",
"GG_Screensaver_Blank",
"GG_Screensaver_Presentations",
"GG_Screensaver_Windows10_Lockscreen",
"GG_SIEM_Log_Captures",
"GG_SIEM_Log_Captures_CHG",
"GG_SIEM_Log_Captures_MDC",
"GG_SIEM_Log_Captures_NGS",
"GG_SIEM_Log_Captures_Rollback",
"GG_StaffNow",
"GG_StateAffairs",
"GG_Tableau_Admin ",
"GG_UC_VMware_Admin",
"GG_VDI_Application_Permissions",
"GG_VDI_PEOPLESOFT_REPORTING-FEPprd",
"GG_VDIDesktopSupport",
"GG_Web_Servers",
"GG_WebEx_Users",
"GG_Windows10_GPO_Testing",
"GG_Windows10_Users",
"GG_Wireless_Profile_Testers",
"GG_Wireless_Testers",
"HPE_IRS",
"Hyperion-Users",
"HyperV_Servers",
"Informatica",
"JohnsAwesomeGPOTestingGroup",
"mongo_nw_pe",
"mongo_rw_dev",
"mongo_rw_pv",
"NetApp",
"NoOutlookNoIE",
"NOW_Upgrade_Temp12",
"O365",
"OKTA",
"Projects",
"QualityStage",
"sco",
"SCOM",
"SCVMM",
"SNOW_Upgrade_Temp_test",
"SNOW_Upgrade_Temp11",
"SNOW_Upgrade_Temp12",
"SNOW_Upgrade_Temp123",
"SNOW_Upgrade_Temp1256",
"SNOW_Upgrade_Temp132",
"SNOW_Upgrade_Temp145",
"SNOW_Upgrade_Temp2",
"SNOW_Upgrade_Temp3",
"SNOW_Upgrade_Temp33",
"SNOW_Upgrade_Temp444",
"SNOW_Upgrade_Temp555",
"SNOW_Upgrade_Temp888",
"Specopshe",
"SUG_FlashPlayerPilot",
"SystemCenter",
"Tower",
"UCSD",
"VCO-Admin",
"VMware"| % { $textbox.AutoCompleteCustomSource.AddRange($_) }

#[void]$listBox.Items.Add('atl-dc-001')


$form.Controls.Add($listBox)

$form.Topmost = $true

$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $listBox.SelectedItem
$x
}

我得到的错误如下:

看起来它没有在域中看到组但是当我运行相同的脚本时,它会自行工作并创建报告。我认为文本框输入和脚本的其余部分缺少某些内容。

解压脚本后出错 再次感谢您的浏览。

干杯

标签: powershellactive-directory

解决方案


您需要从其Text属性中获取文本框值:

$GroupName = $textBox.Text

推荐阅读