PowerShell中使用Filter來創(chuàng)建管道輸入函數(shù)
本文介紹在自定義PowerShell函數(shù)時,不使用Function而使用Filter。Filter可以快速的創(chuàng)建一個管道輸入函數(shù),F(xiàn)ilter就相當(dāng)于只有一個Process塊的Function。
Filter關(guān)鍵詞可以代替Function關(guān)鍵詞來創(chuàng)建函數(shù)。但Filter創(chuàng)建的函數(shù),只能是接收管道輸入?yún)?shù)的參數(shù),并且是接收塊結(jié)構(gòu)數(shù)據(jù)的輸入。所以在Filter定義的函數(shù)里面,你都看不到接收輸入?yún)?shù)。
在Filter定義的函數(shù)中,使用$_變量,來遍歷所有的輸入對象。
Filter Test-ApplicationProgram
{
if ($_.MainWindowTitle -ne '')
{
$_
}
}
執(zhí)行結(jié)果如下:
PS> Get-Process | Test-ApplicationProgram
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
787 53 110040 102008 319 7,91 7600 chrome
……
上面函數(shù)的作用是檢測窗口標(biāo)題不為的進(jìn)程,輸入?yún)?shù)必須是Process類型的變量。我們在演示的時候,使用了Get-Process來獲取所有的進(jìn)程,然后當(dāng)參數(shù)傳遞給Filter。
另外再說一句,F(xiàn)ilter創(chuàng)建的函數(shù)跟Function創(chuàng)建的函數(shù),在本質(zhì)上是一樣的。所以,上面的Filter函數(shù),與下面這段函數(shù)是一樣的。
Function Test-ApplicationProgram
{
process
{
if ($_.MainWindowTitle -ne ‘')
{
$_
}
}
}
或者說,在PowerShell系統(tǒng)內(nèi)部,F(xiàn)ilter創(chuàng)建的函數(shù)將被翻譯成上面這段Function存儲在內(nèi)存中。
關(guān)于PowerShell使用Filter來快速創(chuàng)建管道輸入函數(shù),本文就介紹這么多,希望對您有所幫助,謝謝!
- PowerShell實現(xiàn)按條件終止管道的方法
- PowerShell中終止管道的方法
- PowerShell入門教程之PowerShell管道介紹
- Windows Powershell導(dǎo)出管道結(jié)果
- Windows Powershell分析和比較管道結(jié)果
- Windows Powershell過濾管道結(jié)果
- Windows Powershell排序和分組管道結(jié)果
- Windows Powershell使用管道
- Windows Powershell 管道和重定向
- PowerShell函數(shù)中接收管道參數(shù)實例
- PowerShell管道入門必看篇(管道例子大全)
相關(guān)文章
PowerShell數(shù)組結(jié)合switch語句產(chǎn)生的奇特效果介紹
這篇文章主要介紹了PowerShell數(shù)組結(jié)合switch語句產(chǎn)生的奇特效果介紹,產(chǎn)生了類似枚舉的效果,需要的朋友可以參考下2014-08-08PowerShell正則表達(dá)式(Regex)從右往左進(jìn)行匹配方法代碼實例
這篇文章主要介紹了PowerShell正則表達(dá)式(Regex)從右往左進(jìn)行匹配方法代碼實例,最重要的就是一個RightToLeft參數(shù)的運(yùn)用,本文直接給出代碼實例,需要的朋友可以參考下2015-05-05PowerShell中使用GetType獲取變量數(shù)據(jù)類型
這篇文章主要介紹了PowerShell中使用GetType獲取變量數(shù)據(jù)類型,本文使用實例來說明GetType的使用方法,并對返回值作了一定的解釋,需要的朋友可以參考下2014-08-08原創(chuàng)powershell腳本小工具ctracert.ps1跟蹤路由(win8以上系統(tǒng))
這篇文章主要介紹了原創(chuàng)powershell腳本小工具ctracert.ps1跟蹤路由(win8以上系統(tǒng)),需要的朋友可以參考下2017-10-10PowerShell顯示隱藏文件和系統(tǒng)文件的方法
這篇文章主要介紹了PowerShell顯示隱藏文件和系統(tǒng)文件的方法,重點在于對文件、文件夾屬性的介紹,需要的朋友可以參考下2014-08-08