PowerShell實(shí)現(xiàn)統(tǒng)計(jì)函數(shù)嵌套深度
當(dāng)你調(diào)用某個(gè)函數(shù)時(shí),PowerShell會(huì)增加一次嵌套層次。當(dāng)一個(gè)函數(shù)調(diào)用了另一個(gè)函數(shù),或著腳本,也會(huì)增加嵌套層次。今天分享一個(gè)函數(shù),它能告訴你的腳本嵌套的層次:
function Test-NestLevel { $i = 1 $ok = $true do { try { $test = Get-Variable -Name Host -Scope $i } catch { $ok = $false } $i++ } While ($ok) $i }
當(dāng)你調(diào)用的函數(shù)具有遞歸調(diào)用時(shí),上面的函數(shù)非常有用,來(lái)看一個(gè)調(diào)用的示例:
function Test-Diving { param($Depth) if ($Depth -gt 10) { return } "Diving deeper to $Depth meters..." $currentDepth = Test-NestLevel "calculated depth: $currentDepth" Test-Diving -depth ($Depth+1) } Test-Diving -depth 1
當(dāng)你運(yùn)行Test-Diving時(shí),函數(shù)會(huì)調(diào)用自己10次。函數(shù)使用一個(gè)參數(shù)來(lái)控制嵌套層次,而Test-NestLevel負(fù)責(zé)返回確切的深度數(shù)。
注意這里有個(gè)區(qū)別:Test-NestLevel返回絕對(duì)的嵌套層次,參數(shù)會(huì)紀(jì)錄這個(gè)函數(shù)調(diào)用自己多少次。如果Test-Diving被嵌入到另外一個(gè)函數(shù)中,絕對(duì)深度和相對(duì)深度會(huì)不同。
PS C:\> Test-Diving -Depth 1 diving deeper to 1 meters... calculated depth: 1 diving deeper to 2 meters... calculated depth: 2 diving deeper to 3 meters... calculated depth: 3 diving deeper to 4 meters... calculated depth: 4 diving deeper to 5 meters... calculated depth: 5 diving deeper to 6 meters... calculated depth: 6 diving deeper to 7 meters... calculated depth: 7 diving deeper to 8 meters... calculated depth: 8 diving deeper to 9 meters... calculated depth: 9 diving deeper to 10 meters... calculated depth: 10 PS C:\> & { Test-Diving -Depth 1 } diving deeper to 1 meters... calculated depth: 2 diving deeper to 2 meters... calculated depth: 3 diving deeper to 3 meters... calculated depth: 4 diving deeper to 4 meters... calculated depth: 5 diving deeper to 5 meters... calculated depth: 6 diving deeper to 6 meters... calculated depth: 7 diving deeper to 7 meters... calculated depth: 8 diving deeper to 8 meters... calculated depth: 9 diving deeper to 9 meters... calculated depth: 10 diving deeper to 10 meters... calculated depth: 11 PS C:\>
Test-NestLevel總會(huì)返回從當(dāng)前代碼的作用域到全局作用域的嵌套深度。
相關(guān)文章
PowerShell腳本清理指定天數(shù)前的臨時(shí)文件夾實(shí)現(xiàn)代碼
這篇文章主要介紹了PowerShell腳本清理指定天數(shù)前的臨時(shí)文件夾實(shí)現(xiàn)代碼,指定天數(shù)可以任意修改數(shù)字實(shí)現(xiàn),需要的朋友可以參考下2014-08-08PowerShell 讀取性能計(jì)數(shù)器二進(jìn)制文件(.blg)記錄并匯總計(jì)算
由于監(jiān)控及報(bào)告需要,要統(tǒng)計(jì)性能計(jì)數(shù)器每天數(shù)值情況,確認(rèn)數(shù)據(jù)庫(kù)服務(wù)器的運(yùn)行狀況。若打開(kāi)計(jì)數(shù)器填寫,比較麻煩,現(xiàn)在統(tǒng)計(jì)用 powershell 來(lái)讀取計(jì)數(shù)器的值2016-11-11Powershell改變腳本執(zhí)行優(yōu)先權(quán)的代碼分享
這篇文章主要介紹了Powershell改變腳本執(zhí)行優(yōu)先權(quán)的代碼分享,本文通過(guò)控制進(jìn)程的方式調(diào)整程序的執(zhí)行順序,需要的朋友可以參考下2014-11-11PowerShell腳本中查看網(wǎng)卡的高級(jí)屬性
這篇文章主要介紹了PowerShell腳本中查看網(wǎng)卡的高級(jí)屬性,本文直接給出實(shí)現(xiàn)代碼和運(yùn)行結(jié)果,需要的朋友可以參考下2014-11-11PowerShell函數(shù)中使用必選參數(shù)實(shí)例
這篇文章主要介紹了PowerShell函數(shù)中使用必選參數(shù)實(shí)例,即把一個(gè)參數(shù)設(shè)置為必選參數(shù)的方法,需要的朋友可以參考下2014-07-07Powershell實(shí)現(xiàn)獲取電腦序列號(hào)功能腳本分享
這篇文章主要介紹了Powershell實(shí)現(xiàn)獲取電腦序列號(hào)功能腳本分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-03-03PowerShell腳本開(kāi)發(fā)之批量掃描IP和端口
最近在不斷嘗試之下,找到了對(duì)指定范圍的IP段進(jìn)行掃描和對(duì)端口進(jìn)行掃描的方法,本文將會(huì)介紹如何通過(guò)PowerShell批量掃描IP及其對(duì)應(yīng)的端口。2014-10-10