检查开关参数的正确方法是什么

2024-04-12

检查开关值的正确方法是什么?

function testSwitch
{
    Param(
        [switch] $swth
    )

    Write-Host "Value of swth is $swth"

    if($swth.IsPresent){
        Write-host "Switch is present"
    }
    if($swth){
        Write-Host "Switch is present"
    }

}

testSwitch -swth

我知道两个 if 语句都可以正常工作,但是如何呢?


$swth.IsPresent and $swth can be used interchangeably, because in a Boolean context (such as an if conditional) $swth (an instance of type [switch] https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.switchparameter representing a switch parameter https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-6#switch-parameters) effectively returns the value of the .IsPresent property.[1]

In fact, $swth is probably preferable[2], not just for concision, but because the .IsPresent property is somewhat confusingly named:

.IsPresent并不表示presence开关本身,但它是否值为$true.

单独指定开关时 --swth- 暗示both,如果您明确将其设置为,则情况并非如此$false:通过-swth:$false makes .IsPresent return $false,即使开关很明显present.

Passing $false明确地并不常见,但有其用途,例如选择退出确认提示时-Confirm:$false, 什么时候以编程方式构建论据。

所以,如果你想区分用户没有通过交换机和用户传递它的值$false, .IsPresent不会帮助你 - 你必须使用$PSBoundParameters.ContainsKey('swth')


[1] On conversion to Boolean, which happens in method LanguagePrimitives.IsTrue() https://github.com/PowerShell/PowerShell/blob/bbd54c317e01d652081753a9cd2350e96aa8bd3e/src/System.Management.Automation/engine/LanguagePrimitives.cs#L1013, PowerShell calls a [switch] instance's .ToBool() method https://github.com/PowerShell/PowerShell/blob/bbd54c317e01d652081753a9cd2350e96aa8bd3e/src/System.Management.Automation/engine/MshCmdlet.cs#L108, which in turn returns the private backing variable behind the .IsPresent property.

[2] The only caveat is that you must be aware that $swth is not a Boolean ([bool]), but of type [switch], which may matter in other contexts.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

检查开关参数的正确方法是什么 的相关文章

  • 如何在 PowerShell 中使用参数执行外部程序?

    我读过这个答案堆栈溢出 https stackoverflow com questions 1673967 how to run exe in powershell with parameters with spaces and quote
  • Connect-AzAccount 无提示

    我正在尝试使用以下方式登录 Azure 门户 Connect AzAccount 此代码要求我输入我不想要的提示 我们可以使用一些简单的配置脚本自动登录吗 正如Joy所说 您可以通过凭据使用用户帐户登录 但不会出现任何提示 请确保您的帐户未
  • 通过 powershell 获取压缩的 TFS 2015 (vNext) 构建输出日志(就像构建后的下载链接一样)

    我想知道是否有人有一个 PowerShell 脚本 可以通过 TFS 2015 vNext 的 Rest API 下载此构建 ID 的所有当前构建日志 直到当前步骤 为每个记录的构建步骤创建单独的文本文件 并压缩所有文本文件 或者 如果已经
  • Powershell 错误:方法调用...不包含名为“replace”的方法

    我想使用 PowerShell 搜索并替换 xml 文件中的字符串 我试过这个 gc d test xml replace 1234 xxxx sc d test xml 这对于我的 test xml 文件效果很好 我的 test xml
  • Jenkins 执行 PowerShell 脚本

    我正在尝试从 Jenkins 运行 PowerShell 脚本 但它似乎完全忽略了执行策略 可以通过直接执行 powershell exe 或使用PowerShell插件 https wiki jenkins ci org display
  • PowerShell 找不到过载

    我正在尝试使用https sshnet codeplex com https sshnet codeplex com 允许 PowerShell 脚本将文件上传到 SFTP 服务器 一切似乎都有效 除了找不到该方法的重载UploadFile
  • 如何从 powershell 中设置 bash 的环境变量?

    使用powershell我该如何设置 https askubuntu com q 1205227 45156 the 环境 https stackoverflow com q 5327495 262852变量 这里 特别 https sta
  • 如何使用 R 或 PowerShell 从文本文件中提取数据?

    我有一个包含如下数据的文本文件 This is just text Username SOMETHI C Text Account DFAG Finish time 1 JAN 2011 00 31 58 91 Process ID 202
  • Azure 函数核心工具安装卡在 Visual Studio Code 中

    我创建了 Azure 函数应用程序来执行 powershell 脚本 想先在我的本地运行它来测试它 当我运行它时 无论有没有调试选项 都会出现一个弹出窗口 其中显示 您必须安装 Azure Functions 核心工具才能调试本地函数 当我
  • 一行 PowerShell 脚本

    我创建了这个基本的单行 PowerShell 脚本 如果我运行以下命令 该脚本就会执行adAD 的 cmdlet 然后是查询的其余部分 但是尝试将它们一起运行时 它似乎只加载了 cmdlet 而没有执行 cmd 的其余部分 powershe
  • 在远程桌面上使用 Powershell 获取 Windows DisplayLanguage

    我正在尝试通过 powershell 获取 Windows 显示语言设置远程计算机 我尝试了 Get WinUserLanguageList 但这会返回所有语言的列表 Get WinSystemLocale 和 Get Culture 也不
  • 从 PowerShell 运行 SSIS 包,无需部署项目

    This https learn microsoft com en us sql integration services ssis quickstart run powershell view sql server ver16 ssis
  • 如何使用 PowerShell 查找 CPU 和 RAM 使用情况?

    我试图让 PowerShell 提供 RAM 和 CPU 使用情况 但我无法弄清楚要使用哪个 WMI 类 我的计算机有两个处理器 因此拥有这两个处理器的信息会很有用 您还可以使用 Get Counter cmdlet PowerShell
  • Powershell - 奇怪的 WSL 输出字符串编码

    今天我只是想检查我的 Windows 工作站上是否安装了特定的子系统 因此 我使用 Windows Subsystem for Linux WSL 并安装可从 Microsoft Store 获取的 Ubuntu 现在我试图找到一种方法来检
  • Powershell:复制时自动更改文件属性

    我有一个只读文件的文件夹 当我将这些文件之一复制到测试位置时 我希望能够复制它们而不保留其原始属性 我可以做到这一点 但这很长 copy item srcfilefullname destfilefullname set itemprope
  • 将变量传递给 Start-Job

    我必须在这里遗漏一些非常简单的东西 这是一个非常基本的脚本来说明我正在尝试的内容 Computers comp1 comp2 ScriptBlock New Item C Temp C txt Force Foreach C in Comp
  • 导入模块 WebAdministration 不会从脚本加载,但会从命令行加载

    我正在进行一个使用的项目PowerShell编写构建脚本 该构建利用了WebAdministration模块来管理本地 IIS 实例 当我运行构建脚本时 尝试导入时会引发以下错误WebAdministration 错误 06 29 2016
  • Import-Csv - 成员已存在问题

    我必须将多个 CSV 文件合并为一个文件 每个 CSV 都有一个标题 其中一列标题是相同的 理想情况下 最终文件 all out csv 必须有一个标头 我运行 PowerShell 代码 Import Csv out 1 result c
  • Pester 没有捕获抛出的错误

    当我运行以下纠缠测试时 我希望它能够捕获预期的错误 但事实并非如此 但是 当我使用不同的函数和不同的 throw 语句运行测试时 它会起作用 纠缠测试 Describe Remove GenericCredential Function T
  • 在 Powershell 4 中压缩和解压缩文件

    我使用的是 Windows Server 2012 R2 64 位 我有 powershell 版本 4 可用 我正在尝试压缩和解压缩文件 当我尝试 Write Zip 命令时 它会抛出以下错误 Write Zip 术语 Write Zip

随机推荐