运行 变量以 PR 开头时的 IF

Running an IF when variable begins with PR

我刚刚开始使用 Powershell,我想深入了解它。

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

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

选择 PR 设备时,它会显示特定的 window。不幸的是,脚本不理解 PR* 我的意思是所有以这两个字母开头的打印机。

这是我的代码:

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

感谢各种帮助!

您需要使用 -like 运算符来执行您想要的操作。

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

Matching Operators

The like operators (-like and -notlike) find elements that match or do not match a specified pattern using wildcard expressions.

The syntax is:

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

...

-like

Description: Match using the wildcard character (*).

参考:MSDocs - About Comparison operators