2015-03-25 29 views
1

我能得到这样的当前打开窗口的列表:如果窗口名称存在,如何退出powershell脚本?

Get-Process | where {$_.mainWindowTitle} | format-table 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 

回答

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

基本介绍到如何ForEach-Object作品可以TechNet

+0

TKS,你能在'#iterates通过过程扩大这里。每一个都可以被$ _'逻辑引用? – Level1Coder 2015-03-25 01:26:56

+1

请研究“PowerShell foreach循环”以了解更多信息。 现在,只需运行: if(ps notepad -ErrorAction SilentlyContinue){'Notepad is open'} – 2015-03-25 01:45:20

1

尝试找到使用样,而不是包含。它为我工作。

下面是例如:

PS C:\ Windows \ System32下>获取进程|其中{$ _ mainWindowTitle样 “记事本”。}

把手NPM(K)PM(K)WS(K)VM(M)CPU(S)标识ProcessName ------- - ----- ----- ----- ----- -------- ----------- 71 7 1516 6760 95 0.23 6328记事本 71 7 1516 6676 95 0.11 7212记事本 68 7 1472 2808 94 8.14 7364记事本 73 7 1540 1568 95 0.48 8640记事本 74 8 1820 1672 160 9.41 8884记事本

+0

其“*记事本*” – user3788641 2015-03-25 04:44:58

+0

在记事本前和记事本后使用通配符*。 – user3788641 2015-03-25 04:46:19

相关问题