在 PowerShell 中实现与 whoami 命令的不同选项类似功能,可以通过使用不同的命令和方法来获得用户信息、组信息、声明和权限等。下面列出对应的 PowerShell 实现方式:
在 PowerShell 中实现与 whoami
命令的不同选项类似功能,可以通过使用不同的命令和方法来获得用户信息、组信息、声明和权限等。下面列出对应的 PowerShell 实现方式:
1. WHOAMI
等效命令:
powershellCopy Code
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
2. WHOAMI /UPN
(User Principal Name)
获取当前用户的 UPN (通常为 username@domain.com
格式),可以使用:
powershellCopy Code
(Get-WmiObject -Class Win32_ComputerSystem).UserName
或者:
powershellCopy Code
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
3. WHOAMI /FQDN
(Fully Qualified Domain Name)
获取当前计算机的 FQDN (完整域名),可以使用:
powershellCopy Code
([System.Net.Dns]::GetHostEntry($env:COMPUTERNAME)).HostName
4. WHOAMI /LOGONID
获取当前用户的登录 ID,可以通过 Get-WmiObject
命令来获取:
powershellCopy Code
(Get-WmiObject -Class Win32_ComputerSystem).UserName
或者使用:
powershellCopy Code
$env:USERDOMAIN + "\" + $env:USERNAME
5. WHOAMI /USER
获取当前用户的 SID 和用户名:
powershellCopy Code
whoami /USER
在 PowerShell 中的等效命令:
powershellCopy Code
[System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
6. WHOAMI /USER /FO LIST
获取用户的 SID 和其他详细信息并以列表形式显示:
powershellCopy Code
[System.Security.Principal.WindowsIdentity]::GetCurrent() | Format-List
7. WHOAMI /USER /FO CSV
将用户 SID 和其他信息以 CSV 格式显示:
powershellCopy Code
[System.Security.Principal.WindowsIdentity]::GetCurrent() | Select-Object * | Export-Csv -Path "user_info.csv" -NoTypeInformation
8. WHOAMI /GROUPS
列出当前用户的所有组信息:
powershellCopy Code
[System.Security.Principal.WindowsIdentity]::GetCurrent().Groups
9. WHOAMI /GROUPS /FO CSV /NH
以 CSV 格式并且不包含表头,列出当前用户的所有组信息:
powershellCopy Code
[System.Security.Principal.WindowsIdentity]::GetCurrent().Groups | Select-Object -Property Value | Export-Csv -Path "user_groups.csv" -NoTypeInformation
10. WHOAMI /CLAIMS
列出用户的声明:
PowerShell 本身没有一个内建的命令来直接查看声明。可以使用 Get-ADUser
和 Get-ADUserCertificate
等 Active Directory 命令来获取类似的信息(需要 Active Directory 模块)。
11. WHOAMI /CLAIMS /FO LIST
列出用户的声明并以列表格式显示(使用 AD 模块):
powershellCopy Code
Get-ADUser -Identity $env:USERNAME -Properties * | Select-Object -Property DistinguishedName, ClaimTypes
12. WHOAMI /PRIV
列出当前用户的权限信息:
powershellCopy Code
whoami /priv
PowerShell 等效:
powershellCopy Code
$privileges = [System.Security.Principal.WindowsIdentity]::GetCurrent().Groups
$privileges | ForEach-Object { $_.Value }
13. WHOAMI /PRIV /FO TABLE
以表格格式列出当前用户的权限信息:
powershellCopy Code
$privileges = [System.Security.Principal.WindowsIdentity]::GetCurrent().Groups
$privileges | Format-Table -Property Value
14. WHOAMI /USER /GROUPS
列出当前用户的信息及其所属组:
powershellCopy Code
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
[System.Security.Principal.WindowsIdentity]::GetCurrent().Groups
15. WHOAMI /USER /GROUPS /CLAIMS /PRIV
列出当前用户的信息、组信息、声明和权限:
powershellCopy Code
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
[System.Security.Principal.WindowsIdentity]::GetCurrent().Groups
# 需要查询 Active Directory 声明及权限
16. WHOAMI /ALL
列出所有用户信息、组、声明、权限等:
powershellCopy Code
[System.Security.Principal.WindowsIdentity]::GetCurrent() | Format-List *
17. WHOAMI /ALL /FO LIST
以列表形式列出所有详细信息:
powershellCopy Code
[System.Security.Principal.WindowsIdentity]::GetCurrent() | Format-List *
18. WHOAMI /ALL /FO CSV /NH
以 CSV 格式列出所有详细信息,并且没有表头:
powershellCopy Code
[System.Security.Principal.WindowsIdentity]::GetCurrent() | Select-Object * | Export-Csv -Path "user_info.csv" -NoTypeInformation
以上是对应 whoami
命令不同选项在 PowerShell 中的实现方法。根据需要,你可以选择适合的命令来获取当前用户的信息、组信息、声明、权限等。