Answers:
[bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")
分解这是做什么的:
[bool]
-将最终结果转换为bool
。[System.Security.Principal.WindowsIdentity]::GetCurrent()
-检索WindowsIdentity
当前正在运行的用户的。(...).groups
-访问groups
身份的属性,以查找该身份所属的用户组。-match "S-1-5-32-544"
检查以查看是否groups
包含管理员组的SID,该标识仅在使用“以管理员身份运行”时包含。-match
和强制转换:[Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains 'S-1-5-32-544'
([Security.Principal.WindowsPrincipal] `
[Security.Principal.WindowsIdentity]::GetCurrent() `
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
这将检索当前的Windows身份,如果当前身份具有管理员角色(即正在提升权限),则返回True。
在PowerShell中4.0你可以使用需要在你的脚本的顶部:
#Requires -RunAsAdministrator
输出:
无法运行脚本“ MyScript.ps1”,因为它包含一个“ #requires”语句以管理员身份运行。当前的Windows PowerShell会话未以管理员身份运行。使用“以管理员身份运行”选项启动Windows PowerShell,然后尝试再次运行脚本。
return
如果用户不是admin,则可以:)
#Requires -RunAsAdministrator
有用的原因:如果您没有提升权限,它将阻止整个脚本运行。