powershell - powerShell:获取 “Select-Object -Property” bool 状态以在条件中使用

标签 powershell shell conditional-statements

我使用一些Powershell命令来获取对象的属性是true还是false:

Get-ComputerInfo | Select-Object -Property OsPortableOperatingSystem

OsPortableOperatingSystem
-------------------------
                    False

然后,我想检查一下并据此做出决定:
if (OsPortableOperatingSystem)
     {dosomething}

如何获得 bool(boolean) 结果?

最佳答案

您应该将Get-ComputerInfo cmdlet的结果分配给一个变量,以便以后访问它:

$os = Get-ComputerInfo

if ($os.OsPortableOperatingSystem)
{
    # Do something...
}

如果以后不需要访问更多信息,则可以编写如下的if语句:
if ((Get-ComputerInfo).OsPortableOperatingSystem)
{
    # Do something...
}

关于powershell - powerShell:获取 “Select-Object -Property” bool 状态以在条件中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61303987/

相关文章:

powershell - 在 powershell 的 new-aduser -path 选项中提供变量

Powershell 命令在每个文件之前添加文本

Bash shell 十进制到二进制 base 2 转换

linux - 将进程的 STDOUT 发送到屏幕和第二个进程,但等待两者完成

perl - Perl 打开中的语法错误

javascript - 在 React JSX 中有条件地渲染组件部分

powershell - vagrant powershell 翻译缺失 : en. vagrant_winrm.errors.winrm_not_ready

powershell - 如何隐藏 Azure PowerShell cmdlet 的警告

linux - Shell GNU-Screen -X Stuff 问题

javascript - 空字符串应为 'false'