当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 检查 PowerShell 是否以管理员身份运行 [3 种方法]

作者:精品下载站 日期:2024-12-14 05:29:20 浏览:15 分类:玩电脑

检查 PowerShell 是否以管理员身份运行 [3 种方法]


[玩转系统] 检查 PowerShell 是否以管理员身份运行 [3 种方法]

使用 WindowsPrincipalWindowsIdentity

使用 WindowsPrincipalWindowsIdentity 类检查 PowerShell 是否以管理员身份运行。

使用 WindowsPrincipal 和 WindowsIdentity 类:

$currentPrincipal = New-Object Security.Principal.WindowsPrincipal(
                    [Security.Principal.WindowsIdentity]::GetCurrent()
                    )
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if ($isAdmin) {
    Write-Host "PowerShell is running as admin."
} else {
    Write-Host "PowerShell is not running as admin."
}

输出 :

PowerShell is running as admin.

如果 PowerShell 以管理员身份运行,上面的代码将返回 PowerShell is running as admin;否则,它返回PowerShell is not running as admin

让我们分解代码来理解它。

首先,我们使用 New-Object cmdlet 基于当前 Windows 身份创建一个新的 WindowsPrincipal 对象,代表运行 PowerShell 会话的用户;我们将该对象的引用存储在 $currentPrincipal 变量中。

然后,我们调用 $currentPrincipal 对象(在上一步中创建)上的 IsInRole() 方法,并传递 WindowsBuiltInRole.Administrator 枚举值作为参数来确定当前用户是否属于管理员组。

如果 IsInRole() 方法返回 True,则当前用户以管理员身份运行 PowerShell。另一方面,如果它返回 False,则当前用户没有以管理员身份运行 PowerShell。

我们在上面的代码片段中使用了 System.Security.Principal 命名空间中的 WindowsPrincipal 和 WindowsIdentity 类来检查当前用户是否具有管理员权限基于用户 Windows 身份的组成员身份。

我们还可以使用 System.Security 命名空间的 WindowsIdentity 类和 -match 运算符来检查 PowerShell 是否以管理员权限运行(也称为“更高的权利”)。

使用 WindowsIdentity 类:

$isAdmin = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")
if ($isAdmin) {
    Write-Host "PowerShell is running as admin."
} else {
    Write-Host "PowerShell is not running as admin."
}

输出 :

PowerShell is running as admin.

在上面的代码示例中,[bool] 将结果转换为 bool 值。然后,WindowsIdentity 类的 GetCurrent() 方法检索当前正在运行的用户的 Windows 标识。

接下来,.groups 用于访问身份的 group 属性,以确定该身份所属的用户组。最后,-match "S-1-5-32-544" 评估是否具有管理员组的众所周知的 SID,只有在以下情况下,身份才会包含它:使用了“以管理员身份运行”选项。

使用net user命令

使用 net user 命令检查 PowerShell 是否以管理员身份运行。

使用网络用户命令:

net user mehvish

输出 :

User name                    Mehvish
Full Name
Comment
User's comment
Country/region code          000 (System Default)
...                          ...
...                          ...
...                          ...
Local Group Memberships      *Administrators
Global Group memberships     *None
The command was completed successfully.

我们使用net user命令来检查当前用户是否在管理组中。在 PowerShell 中使用 net user 命令时,不要忘记将 mehvish 替换为您的用户名。使用此命令,我们将获得许多详细信息,我们需要从中找到本地组成员身份部分。例如,如果它包含“Administrators”,则表示当前用户是管理员组的成员,并且 PowerShell 正在以管理员权限运行。

您还可以使用 Get-ExecutionPolicy 命令检查管理员权限。如果执行策略不是“Restricted”,则 PowerShell 将以管理员身份运行。请注意,“Restricted” 执行策略表示非管理上下文。

使用 Requires 语句

如果您使用的是 PowerShell 4.0 或更高版本,则可以使用脚本顶部的 Requires 语句来阻止脚本以普通用户身份运行。请参阅以下示例。

使用需要声明:

#Requires -RunAsAdministrator

现在,运行两个 PowerShell 进程,第一个具有管理员权限(提升),第二个不具有管理员权限(非提升)。在这两个进程中,移动到脚本文件所在的目录并运行它。

在我们的例子中,我们的 testPS.ps1 文件仅包含 #Requires -RunAsAdministrator 语句,它位于 E:\Test\Script Files

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯