溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Powershell實(shí)用命令(2)

發(fā)布時(shí)間:2020-07-18 09:44:16 來(lái)源:網(wǎng)絡(luò) 閱讀:419 作者:rikchen 欄目:系統(tǒng)運(yùn)維

我又來(lái)了,今天給大家分享的實(shí)用命令是Measure-Object,看這個(gè)命令的文字意思就知道是用于統(tǒng)計(jì)評(píng)估對(duì)象的了,那我們來(lái)說(shuō)說(shuō)他的具體用法

首先我們可以看下這個(gè)命令的語(yǔ)法,看看他能支持哪些參數(shù)

PS C:\Windows\system32> Get-Command measure-Object -Syntax

Measure-Object [[-Property] <string[]>] [-InputObject <psobject>] [-Sum] [-Average] [-Maximum] [-Minimum] [<CommonParameters>]

Measure-Object [[-Property] <string[]>] [-InputObject <psobject>] [-Line] [-Word] [-Character] [-IgnoreWhiteSpace] [<CommonParameters>]

從上面我們可以看到Measure-Object能夠支持求和,求平均值,取最大值,取最小值,統(tǒng)計(jì)行數(shù),單詞數(shù),字符數(shù),IgnoreWhiteSpace參數(shù)是為了讓你在統(tǒng)計(jì)的時(shí)候忽略空白字符(空格,回車)的計(jì)數(shù)。
下面我們用代碼來(lái)分別檢驗(yàn)一下這些功能

PS C:\Windows\system32> Get-Process | measure -Property vm -Maximum

Count    : 210
Average  : 
Sum      : 
Maximum  : 2238980472832
Minimum  : 
Property : VM

PS C:\Windows\system32> Get-Process | measure -Property vm -Minimum

Count    : 210
Average  : 
Sum      : 
Maximum  : 
Minimum  : 4096
Property : VM

PS C:\Windows\system32> Get-Process | measure -Property vm -Average

Count    : 214
Average  : 1596722333006.95
Sum      : 
Maximum  : 
Minimum  : 
Property : VM

PS C:\Windows\system32> Get-Process | measure -Property vm -sum

Count    : 212
Average  : 
Sum      : 337291811336192
Maximum  : 
Minimum  : 
Property : VM

PS C:\Windows\system32> Get-Content D:\File1.txt | measure -Line -Word -Character

Lines Words Characters Property
----- ----- ---------- --------
    4    20        113         

PS C:\Windows\system32> Get-Content D:\File1.txt | measure -Line -Word -Character -IgnoreWhiteSpace

Lines Words Characters Property
----- ----- ---------- --------
    4    20         97         

PS C:\Windows\system32> Get-Content D:\File1.txt
Learning Powershell Technology

I like Powershell Script Language

Do you like it as me

Fine , let's learn it togather

上面的代碼秀出了Measure-Object的功能,就問(wèn)你,強(qiáng)不強(qiáng)大,厲不厲害,不需要一會(huì)調(diào)用max,一會(huì)調(diào)用min,直接一個(gè)命令搞定所有

再介紹另外一個(gè)比較實(shí)用的Measure命令,Measure-Command,這個(gè)命令經(jīng)常用來(lái)檢測(cè)代碼的運(yùn)行時(shí)間,通過(guò)這個(gè)時(shí)間,我們可以評(píng)估出哪種代碼更加優(yōu)秀,所需時(shí)間更短,提升我們代碼的執(zhí)行效率,降低運(yùn)行代碼主機(jī)的性能損耗

$Script1={For($i=0;$i -le 100;$i++){Add-Content -Path D:\File1.txt -Value "I want add some word in this file"}}

$Script2={$NW=New-Object System.IO.StreamWriter "d:\file1.txt"; For($i=0;$i -lt 100;$i++){$NW.WriteLine("I Write it again !")};$nw.Close()}

PS C:\Windows\system32> Measure-Command -Expression $Script1 | select TotalMilliseconds

TotalMilliseconds
-----------------
         117.5692

PS C:\Windows\system32> Measure-Command -Expression $Script2 | select TotalMilliseconds

TotalMilliseconds
-----------------
           2.5188

通過(guò)Measure-Command 可以評(píng)估出同樣的結(jié)果,但是代碼不同,所花費(fèi)時(shí)間是完全不一樣的,這樣能使我們明白自己的代碼是否優(yōu)秀。

好了,今天就介紹到這,瓜子花生啤酒,客官來(lái)一份?

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI