PowerShell实现统计函数嵌套深度

所属分类: 脚本专栏 / PowerShell 阅读数: 1073
收藏 0 赞 0 分享

当你调用某个函数时,PowerShell会增加一次嵌套层次。当一个函数调用了另一个函数,或着脚本,也会增加嵌套层次。今天分享一个函数,它能告诉你的脚本嵌套的层次:

function Test-NestLevel
{
$i = 1
$ok = $true
do
{
try
{
$test = Get-Variable -Name Host -Scope $i
}
catch
{
$ok = $false
}
$i++
} While ($ok)
 
$i
}

当你调用的函数具有递归调用时,上面的函数非常有用,来看一个调用的示例:

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
 


当你运行Test-Diving时,函数会调用自己10次。函数使用一个参数来控制嵌套层次,而Test-NestLevel负责返回确切的深度数。

注意这里有个区别:Test-NestLevel返回绝对的嵌套层次,参数会纪录这个函数调用自己多少次。如果Test-Diving被嵌入到另外一个函数中,绝对深度和相对深度会不同。

 

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总会返回从当前代码的作用域到全局作用域的嵌套深度。

更多精彩内容其他人还在看

使用HTTP api简单的远程执行PowerShell脚本

为了你能非常简单的远程执行PoweShell脚本,使用REST API是一个很好的选择,因为现在许多流行的编程语言都可以简单的执行HTTP的GET操作。
收藏 0 赞 0 分享

Powershell小技巧之使用WS-Man来调用PowerShell命令

大多Windows系统的管理员应当已经意识到在Windows系统上进行脚本开发和命令行管理,PowerShell首当其冲。微软许多产品和一些第三方产品都提供了Windows PowerShell的管理接口。但是目前PowerShell只能运行在Windows系统上,如何才能在非W
收藏 0 赞 0 分享

Windows Powershell For 循环

这篇文章主要介绍了Windows Powershell For 循环的定义、用法以及示例,非常简单实用,有需要的朋友可以参考下
收藏 0 赞 0 分享

Windows Powershell Switch 循环

这篇文章主要介绍了Windows Powershell Switch 循环以及PowerShell中数组可以与switch语句结合,产生意想不到的效果。
收藏 0 赞 0 分享

Windows Powershell 定义函数

这篇文章主要介绍了Windows Powershell 定义函数,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell实现导入安装证书功能脚本分享

这篇文章主要介绍了Powershell实现导入安装证书功能脚本分享,本文用编程方法实现把pfx证书文件导入到指定的库中,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell改变脚本执行优先权的代码分享

这篇文章主要介绍了Powershell改变脚本执行优先权的代码分享,本文通过控制进程的方式调整程序的执行顺序,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell读取PFX证书并输入密码的脚本分享

这篇文章主要介绍了Powershell读取PFX证书并输入密码的脚本分享,本文实现直接用脚本输入密码,避免了手动输入密码的麻烦,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell访问SQL Server数据库代码实例

这篇文章主要介绍了Powershell访问SQL Server数据库代码实例,本文直接给出代码,使用时只需要替换数据库配置参数即可,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell打印文本文档例子

这篇文章主要介绍了Powershell打印文本文档例子,本文相对简单,默认给出的是打印系统日志文件,使用时要替换成自己想打印的文件,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多