英文:
Script to pull base64string from AD objects
问题
我正在编写一个PowerShell脚本,该脚本将:
- 查询现有的组织单位(OU)。
 - 选择OU中所有用户的名字、姓氏、samaccountname和objectguid。
 - 获取每个用户的objectguid并将其转换为base64string(immutableid)。
 - 以表格格式输出结果,包括用户的名字、姓氏、samaccountname、objectguid和immutableid,并按用户的名字按字母顺序排序。
 
以下脚本如果我想一次获取一个用户的base64string,就能够正常工作:
Import-module ActiveDirectory
$UserSamAccount = Read-Host "提供用户的SamAccountName"
$User = Get-ADuser $UserSamAccount -Properties * | select ObjectGUID
$ImmutableID = [convert]::ToBase64String(([GUID]($User.ObjectGUID)).tobytearray())
Write-Host "用户 $UserSamAccount 的ImmutableID是:" -ForegroundColor Cyan
$ImmutableID
任何对此的帮助将不胜感激。先行感谢你!
英文:
I am working on a powershell script together which will
- query an existing OU
 - select the first and last name, samaccountname, and objectguid, of all users in the OU
 - Take the objectguid of each user and convert it to a base64string (immutableid)
 - output the results in a table format with users' first and last name, samaccountname, objectguid, and immutableid, sorted in alphabetical order by users' firstname.
 
The below script works just fine if I wanted to pull the base64string for one user at a time:
Import-module ActiveDirectory
$UserSamAccount = Read-Host "Provide SamAccountName of a user"
  
$User = Get-ADuser $UserSamAccount -Properties * | select ObjectGUID
  
$ImmutableID = [convert]::ToBase64String(([GUID]($User.ObjectGUID)).tobytearray())
  
Write-Host "ImmutableID for user $UserSamAccount is:" -ForegroundColor Cyan
$ImmutableID
Any help with this will be most appreciated. Thank you in advance!
答案1
得分: 0
<!-- language-all: sh -->
如果我理解正确您的需求,以下代码应该能满足要求。它使用 [`[pscustomobject]`](https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-pscustomobject?view=powershell-7.3) 构建您期望的输出,并使用 [`ForEach-Object`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.2) 处理来自管道的每个对象:
Get-ADUser -Filter * -SearchBase "OU=myOU,DC=myDomain,DC=xyz" -SearchScope OneLevel |
Sort-Object GivenName |
ForEach-Object {
[pscustomobject]@{
GivenName      = $.GivenName
Surname        = $.Surname
SamAccountName = $.SamAccountName
ObjectGuid     = $.ObjectGuid
ImmutableId    = [convert]::ToBase64String($_.ObjectGuid.ToByteArray())
}
} # | Export-Csv path\to\myExport.Csv -NoTypeInformation <= 可以稍后导出到这个位置
您还可以使用 [`Select-Object`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-7.2) 与[计算属性](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_calculated_properties?view=powershell-7.3)(可能更简单但更难阅读):
Get-ADUser -Filter * -SearchBase "OU=myOU,DC=myDomain,DC=xyz" -SearchScope OneLevel |
Sort-Object GivenName |
Select-Object GivenName, Surname, SamAccountName, ObjectGuid, @{ N='ImmutableId'; E={ [convert]::ToBase64String($_.ObjectGuid.ToByteArray()) }}
英文:
<!-- language-all: sh -->
If I understand correctly your need the following should do the trick. It uses [pscustomobject] to construct your desired output and a ForEach-Object to process each object from the pipeline:
Get-ADUser -Filter * -SearchBase "OU=myOU,DC=myDomain,DC=xyz" -SearchScope OneLevel |
    Sort-Object GivenName |
    ForEach-Object {
        [pscustomobject]@{
            GivenName      = $_.GivenName
            Surname        = $_.Surname
            SamAccountName = $_.SamAccountName
            ObjectGuid     = $_.ObjectGuid
            ImmutableId    = [convert]::ToBase64String($_.ObjectGuid.ToByteArray())
        }
    } # | Export-Csv path\to\myExport.Csv -NoTypeInformation <= Can pipe this to export later :)
You could also use Select-Object with a calculated property (might be simpler but harder to read):
Get-ADUser -Filter * -SearchBase "OU=myOU,DC=myDomain,DC=xyz" -SearchScope OneLevel |
    Sort-Object GivenName |
    Select-Object GivenName, Surname, SamAccountName, ObjectGuid, @{ N='ImmutableId'; E={ [convert]::ToBase64String($_.ObjectGuid.ToByteArray()) }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论