You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将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, 200 is adVarChar—adjust the length (255 in the example) if you need longer values. If you want to include other properties (like SamAccountName), just add another Fields.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:40:10