从AD对象中提取base64字符串的脚本

huangapple go评论59阅读模式
英文:

Script to pull base64string from AD objects

问题

我正在编写一个PowerShell脚本,该脚本将:

  1. 查询现有的组织单位(OU)。
  2. 选择OU中所有用户的名字、姓氏、samaccountname和objectguid。
  3. 获取每个用户的objectguid并将其转换为base64string(immutableid)。
  4. 以表格格式输出结果,包括用户的名字、姓氏、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

  1. query an existing OU
  2. select the first and last name, samaccountname, and objectguid, of all users in the OU
  3. Take the objectguid of each user and convert it to a base64string (immutableid)
  4. 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 &quot;OU=myOU,DC=myDomain,DC=xyz&quot; -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 &lt;= 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 &quot;OU=myOU,DC=myDomain,DC=xyz&quot; -SearchScope OneLevel |
    Sort-Object GivenName |
    Select-Object GivenName, Surname, SamAccountName, ObjectGuid, @{ N=&#39;ImmutableId&#39;; E={ [convert]::ToBase64String($_.ObjectGuid.ToByteArray()) }}

huangapple
  • 本文由 发表于 2023年2月10日 03:24:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定