Windows Powershell分析和比較管道結果_PowerShell

來源:互聯網
上載者:User

使用Measure-Object和Compare-Object可以統計和對比管道結果。Measure-Object允許指定待統計對象的屬性。Compare-Object可以對比對象前後的快照。

統計和計算

使用Measure-Object可以對對象的屬性求最小值、最大值、平均值、和。例如要查看目前的目錄檔案佔用空間的情況。

PS C:Powershell> ls | measure lengthCount  : 19Average :Sum   :Maximum :Minimum :Property : lengthPS C:Powershell> ls | measure length -Average -Sum -Maximum -MinimumCount  : 19Average : 53768.8421052632Sum   : 1021608Maximum : 735892Minimum : 0Property : length

使用Measure-Object還可以統計文字檔中的字元數,單詞數,行數
例如我們可以把下面的文本儲存到:word.txt 。

Retirement Anxiety Spreads Among the One PercentReport: Green Monday a Boon for Online Shopping5 Lesser-Known Ways to Boost Your Credit ScorePS C:Powershell> Get-Content .word.txt | measure -Line -Word -CharacterLines Words Characters Property----- ----- ---------- --------  3  23    141

比較對象

有時需要比較前後兩個時間段開啟了那些進程,服務狀態有什麼變化。類似這樣的工作可以交給Compare-Object。

比較不同的時間段
可以先將所有開啟的進程資訊快照儲存到一個變數中,過一段時間,再儲存一份新的進程快照,然後就可以通過Compare-Object進行對比了。

PS C:Powershell> $before=Get-ProcessPS C:Powershell> $after=get-processPS C:Powershell> Compare-Object $before $afterInputObject               SideIndicator-----------               -------------System.Diagnostics.Process (notepad)  =>System.Diagnostics.Process (notepad)  =>System.Diagnostics.Process (AcroRd32)

$before 是一個數組儲存了當前所有的Process對象,Compare-Object的結果有兩個列:InputObject為前後不一致的對象,SideIndicator為不一致狀態,=>表示新增的對象,結合上面的例子分析:在before和after的時間段有3個進程(AcroRd32,AcroRd32,prevhost)關閉了,有2個進程開啟了(notepad,notepad)。

檢查對象的變化

Compare-Object並不僅僅能比較對象組中的是否新增和減少了對象,它還可以比較每個對象的屬性變化,因為它有一個參數-property 。

PS C:PowerShell> Get-Service wsearchStatus  Name        DisplayName------  ----        -----------Running wsearch      Windows SearchPS C:PowerShell> $svc1=Get-Service wsearchPS C:PowerShell> $svc1.stop()PS C:PowerShell> $svc2=Get-Service wsearchPS C:PowerShell> Compare-Object $svc1 $svc2 -Property Status,Name          Status Name            SideIndicator          ------ ----            -------------       StartPending wsearch          =>          Running wsearch

比較檔案的內容

對於文字檔可以通過Get-Content進行讀取,並且將檔案以行為單位儲存為一個數組,這時依然可以通過Compare-Object進行比較。下面的例子建立兩個不同的文字檔,然後通過Compare-Object比較兩個檔案的Get-Content結果。

PS C:PowerShell> "Hellow>> Power>> Shell" >a.txt>>PS C:PowerShell> "Hollow>> Shell>> Linux" >b.txt>>PS C:PowerShell> Compare-Object (Get-Content .a.txt) (Get-Content .b.txt)InputObject SideIndicator----------- -------------Hollow   =>Linux     =>Hellow

儲存快照以便後期使用

上面的例子都是把對象儲存在變數中,變數有一個缺點就是一旦Powershell退出或者電腦關閉變數都會消失。所以最好的方法就是把對象儲存到磁碟檔案中。怎樣把對象序列化成一個檔案,Powershell提供了一條命令:Export-Clixml,可以完成此工作,還有一條還原序列化的命令Import-Clixml。這樣可以使Compare-object的命令更方便。例如一個月前儲存一個$before對象,一個月後比較都可以。

PS C:PowerShell> Get-Process | Export-Clixml before.xmlPS C:PowerShell> $before=Import-Clixml .before.xmlPS C:PowerShell> $after=Get-ProcessPS C:PowerShell> Compare-Object -ReferenceObject $before -DifferenceObject $after

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.