Bil*_*art 83
([Security.Principal.WindowsPrincipal] `
[Security.Principal.WindowsIdentity]::GetCurrent() `
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
Run Code Online (Sandbox Code Playgroud)
这将检索当前的 Windows 身份并返回$true当前身份是否具有管理员角色(即,正在运行提升)。
edd*_*ves 58
在 Powershell 4.0 中,您可以在脚本顶部使用requires:
#Requires -RunAsAdministrator
Run Code Online (Sandbox Code Playgroud)
输出:
脚本“MyScript.ps1”无法运行,因为它包含以管理员身份运行的“#requires”语句。当前 Windows PowerShell 会话未以管理员身份运行。使用以管理员身份运行选项启动 Windows PowerShell,然后再次尝试运行脚本。
小智 54
[bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")
Run Code Online (Sandbox Code Playgroud)
分解它的作用:
[bool]- 将最终结果转换为bool.[System.Security.Principal.WindowsIdentity]::GetCurrent()- 检索WindowsIdentity当前正在运行的用户的 。(...).groups- 访问groups身份的属性以找出身份所属的用户组。-match "S-1-5-32-544"检查是否groups包含管理员组的众所周知的 SID,只有在使用“以管理员身份运行”时,身份才会包含它。| 归档时间: |
|
| 查看次数: |
39996 次 |
| 最近记录: |