【问题标题】:Running an IF when variable begins with PR当变量以 PR 开头时运行 IF
【发布时间】:2020-04-23 09:09:53
【问题描述】:

我正在迈出 Powershell 的第一步,我想进一步了解它。

我正在编写一个脚本,允许用户通过从 ListBox 中选择网络打印机将其连接到他们的 PC。

我有两种类型的打印机。 PR 代表打印机,SC 代表扫描仪。

在选择PR设备时,它显示特定窗口。不幸的是,脚本不理解 PR* 是指所有以这两个字母开头的打印机。

这是我的代码:

$x = $listBox.SelectedItem
if ($x -eq "PR*"){
    $windowDR.ShowDialog()}

感谢您的每一种帮助!

【问题讨论】:

    标签: powershell user-interface search listbox network-printers


    【解决方案1】:

    您需要使用-like 操作符来做您所寻求的。

    $x = $listBox.SelectedItem
    if ($x -like "PR*"){
        $windowDR.ShowDialog()}
    

    匹配运算符

    like 运算符(-like 和 -notlike)使用通配符查找匹配或不匹配指定模式的元素 表达式。

    语法是:

    <string[]> -like <wildcard-expression>
    <string[]> -notlike <wildcard-expression>
    

    ...

    -喜欢

    说明:使用通配符 (*) 进行匹配。

    参考:MSDocs - About Comparison operators

    【讨论】:

      猜你喜欢
      • 2019-09-05
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 2017-02-03
      • 2020-03-04
      • 2019-09-17
      • 2022-11-16
      • 2022-08-11
      相关资源
      最近更新 更多