Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何在powershell中测试布尔值?_Windows_Powershell_Scripting - Fatal编程技术网

Windows 如何在powershell中测试布尔值?

Windows 如何在powershell中测试布尔值?,windows,powershell,scripting,Windows,Powershell,Scripting,我有一个powershell脚本,可以确保有人以管理员身份运行该脚本 变量$currentPrincipal返回true或false 代码如下: $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) $currentPrincipal.IsInRole([Security.Principal.WindowsBuil

我有一个powershell脚本,可以确保有人以管理员身份运行该脚本

变量$currentPrincipal返回true或false

代码如下:

$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

If ($currentPrincipal -eq $true) {

    Write-host "yay"
}
Else {

   Write-Host "boo"

}
但是当以管理员和currentPrincipal的身份运行时,它仍然会落在else上。。。以下是CLI,其中显示:

PS C:\WINDOWS\system32> C:\Users\dogzilla\Desktop\SetSQLServerToManual.ps1
True
boo

我的问题是,在powershell中计算布尔值的正确方法是什么?

这是测试布尔值的方法:

if ( ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) -eq $true )

这是测试它的方法:

if ( ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) -eq $true )

您当前正在调用,它将输出
Boolean
结果。此外,
$currentPrincipal
的类型为
System.Security.Principal.WindowsPrincipal
,它不是
布尔值。我会将您的代码转换为将
IsInRole()
的结果存储为变量,并进行检查

$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$checkRole = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

If ($checkRole -eq $true) {
    Write-host "yay"
}
Else {
    Write-Host "boo"
}

您当前正在调用,它将输出
Boolean
结果。此外,
$currentPrincipal
的类型为
System.Security.Principal.WindowsPrincipal
,它不是
布尔值。我会将您的代码转换为将
IsInRole()
的结果存储为变量,并进行检查

$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$checkRole = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

If ($checkRole -eq $true) {
    Write-host "yay"
}
Else {
    Write-Host "boo"
}

#Requires-RunAsAdministrator
放在脚本顶部是否有效?为什么$currentPrincipal是$true?@KoryGill。。。要求runasadministrator与powershell的所有版本不兼容。因此,本文中的解决方案更具可移植性。@js2010这是真的,因为我正在一个以“管理员身份运行”打开的终端中运行它。CLI的输出也显示为“true”——我直接从终端复制并粘贴了它。$true不是来自$currentPrincipal。它来自于运行方法$currentPrincipal.IsInRole(…)将
#Requires-RunAsAdministrator
放在脚本工作的顶部?为什么$currentPrincipal是$true?@KoryGill。。。要求runasadministrator与powershell的所有版本不兼容。因此,本文中的解决方案更具可移植性。@js2010这是真的,因为我正在一个以“管理员身份运行”打开的终端中运行它。CLI的输出也显示为“true”——我直接从终端复制并粘贴了它。$true不是来自$currentPrincipal。它来自于运行方法$currentPrincipal.IsInRole(…)