如何将Get-ADGroupMember查询结果直接导入ADO Recordset?
Get AD Group Members into an ADO Recordset Directly
Got it, let's figure out how to pipe your AD group member results straight into an ADO Recordset instead of exporting to CSV first. Here's a practical approach that builds on your existing PowerShell code:
Step-by-Step Solution Code
# Define your target AD group $group = "myADgroupName" # Initialize an ADO Recordset COM object $recordSet = New-Object -ComObject ADODB.Recordset # Set up fields for the Recordset (match properties you want to capture) # 200 = adVarChar data type, 255 = maximum field length $recordSet.Fields.Append("Name", 200, 255) $recordSet.Open() # Fetch AD group members and populate the Recordset Get-ADGroupMember $group | Select-Object Name | ForEach-Object { $recordSet.AddNew() # Create a new empty record $recordSet.Fields.Item("Name").Value = $_.Name # Assign the member's name $recordSet.Update() # Save the record to the Recordset } # Example: Use the populated Recordset (loop through records) $recordSet.MoveFirst() while (-not $recordSet.EOF) { Write-Host "Member: $($recordSet.Fields.Item("Name").Value)" $recordSet.MoveNext() } # Clean up COM resources to avoid memory leaks $recordSet.Close() [System.Runtime.Interopservices.Marshal]::ReleaseComObject($recordSet) | Out-Null Remove-Variable recordSet
Key Details to Note
- Recordset Field Setup: When adding fields, use the correct ADO data type constants. For strings,
200isadVarChar—adjust the length (255 in the example) if you need longer values. If you want to include other properties (likeSamAccountName), just add anotherFields.Append()line for each. - COM Object Cleanup: Always release COM objects properly after use to prevent memory leaks in PowerShell. The lines at the end handle that.
- Compatibility: This works with the same ActiveDirectory module you're already using, so no extra installations are needed.
内容的提问来源于stack exchange,提问作者Bob Lochen
相关产品推荐
相关产品推荐

