2015-03-25 30 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

相關問題