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

如何不使用CSVDE获取Active Directory中CSVDE格式的objectSID

解决PowerShell导出AD用户objectSID为CSVDE格式的问题

格式差异说明

  • CSVDE输出的X'010500000000000515000000f3d2d24f8dce0c921ba52c8f97040000'是SID的原始二进制字节十六进制表示,直接对应Active Directory存储的原始数据格式。
  • PowerShell默认输出的150000052100024321021079141206121462716544143151400是SID的十进制可读字符串形式,由SID各部分的十进制数值拼接而成。

单用户转换命令

执行以下PowerShell代码,可将指定用户的objectSID转换为CSVDE格式:

# 替换为目标用户名或用户ID
$targetUser = "testuser"
$user = Get-ADUser -Identity $targetUser -Properties objectSID
$sidBytes = $user.objectSID.Value
$hexString = [System.BitConverter]::ToString($sidBytes).Replace('-', '')
$formattedSid = "X'$hexString'"

# 输出转换后的结果
$formattedSid

批量导出转换

如果需要批量导出AD用户并包含该格式的objectSID,使用以下命令:

# 导出路径可自行修改
$exportPath = "C:\AD_Users_With_Formatted_SID.csv"

Get-ADUser -Filter * -Properties objectSID | Select-Object Name, SamAccountName, @{
    Name = "objectSID"
    Expression = {
        $sidBytes = $_.objectSID.Value
        $hexString = [System.BitConverter]::ToString($sidBytes).Replace('-', '')
        "X'$hexString'"
    }
} | Export-Csv -Path $exportPath -NoTypeInformation

关键逻辑说明

  • 必须通过-Properties objectSID参数获取用户的objectSID属性,因为Get-ADUser默认不返回该属性。
  • $user.objectSID.Value直接提取SID的原始二进制字节数组,这是转换的核心——CSVDE的输出本质就是该字节数组的十六进制格式化结果。
  • 使用BitConverter::ToString将字节数组转为带连字符的十六进制字符串,再通过Replace('-', '')移除连字符,最后拼接成X'...'格式。

内容的提问来源于stack exchange,提问作者SureThing

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 04:49:51