powershell - 如果窗口名称存在,如何退出powershell脚本?

标签 powershell foreach powershell-4.0

我可以获得当前打开的窗口的列表,如下所示:

获取进程|其中 {$_.mainWindowTitle} |格式表mainWindowTitle

如何编写正确的语法来循环并检查窗口名称是否存在然后退出?

以下是我想要执行的逻辑:

# BASIC-esque CONDITIONAL LOGIC
# FILENAME: CheckWindowName.ps1

Get-Process | where {$_.mainWindowTitle} | format-table mainWindowTitle

# LOOP START - Loop through $_.mainWindowTitle / mainWindowTitle

If $_.mainWindowTitle CONTAINS "*notepad*" Then
    Exit #break script
Else
    Echo "Hello World! There are no instances of notepad open"
End If

# LOOP END

最佳答案

Get-Process | where {$_.mainWindowTitle} | ForEach {
    #iterates through processes here. each one can be referenced by $_
}

有关 ForEach-Object 工作原理的基本介绍可以在 TechNet 上找到。

关于powershell - 如果窗口名称存在,如何退出powershell脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29245728/

相关文章:

powershell - PSCustomObject 到哈希表

PowerShell WebRequest POST

sql - 在没有 'foreach' 的情况下检索 Webmatrix 中的数据

regex - PowerShell在文件行为中查找并替换为正则表达式

arrays - 使用 powershell 的数组中的 "Counting"

powershell - 简单的Powershell : Output basic Text instead of formatted from Get-ADOrganizationalUnit

php - 连接两个以上的 mysql 表,然后将行操作为分组的多维数组

php - 未进入 foreach 循环

powershell - 如何在Powershell脚本中使用属性值

powershell - 检查用户是否存在于组中