[玩转系统] 使用 $MyInspiration 做更多事情
作者:精品下载站 日期:2024-12-14 08:03:47 浏览:15 分类:玩电脑
使用 $MyInspiration 做更多事情
不久前,有人在 Twitter 上对我发表了关于我分享的与 PowerShell 相关的内容的评论。他想了解有关 $MyInvocau 变量的更多信息。这是没有详细记录的内容,但在 PowerShell 脚本编写中非常有用。让我们更详细地看一下它。
当您运行 PowerShell 脚本或函数时,会自动创建 $MyInspiration 变量。从技术上讲,它是一个 System.Management.Automation.InitationInfo 对象。您可以在此处找到 Microsoft 的文档。但让我们从实际的角度来探讨一下这个问题。
这是一个简单的演示函数。
Function Get-Foo {
[cmdletbinding()]
Param(
[Parameter(Position = 0, Mandatory)]
[string]$Name,
[datetime]$Since = (Get-Date).AddHours(-24)
)
$MyInvocation
Write-Host "Getting $name since $since" -fore yellow
}
为了充分利用 $MyInitation,我发现最好将脚本文件点源化或将模块导入到 PowerShell 会话中。该函数除了显示 $MyInspiration 之外不执行任何操作。
尽管这是一个丰富的对象,但您可能只会发现少数属性在 PowerShell 脚本编写中有用。我一直使用 MyCommand 属性。
我的调用.我的命令
这是演示函数的另一个版本。
Function Get-Foo {
[cmdletbinding()]
Param(
[Parameter(Position = 0, Mandatory)]
[string]$Name, [
datetime]$Since = (Get-Date).AddHours(-24)
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Processing $Name"
#create a global copy for testing purposes
$global:mi = $myinvocation
Write-Host "Getting $name since $since" -fore yellow
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
}
这是我几乎所有 PowerShell 函数的格式。 Begin 和 End 脚本块具有反映命令名称的详细语句。我喜欢这种方法,因为如果我更改函数名称,我不必修改任何其他代码。
当我有函数调用其他函数时,这才真正有用。这些详细的语句使跟踪流程变得更加容易。
Function Get-Foo {
[cmdletbinding()]
Param(
[Parameter(Position = 0, Mandatory)]
[string]$Name, [
datetime]$Since = (Get-Date).AddHours(-24)
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Processing $Name"
#create a global copy for testing purposes
$global:mi = $myinvocation
Write-Host "Getting $name since $since" -fore yellow
If (Test-Foo $name) {
$name
}
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
}
Function Test-Foo {
[cmdletbinding()]
Param([string]$Name)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Testing $name"
$True
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
}
现在,详细输出显示我何时使用一个命令、启动另一个命令并返回到原始命令。多年来我发现这项技术非常宝贵。
脚本
您还可以将 $MyInspiration 与脚本一起使用。
#requires -version 5.1
Param(
[Parameter(Position = 0, Mandatory)]
[string]$Name,
[datetime]$Since = (Get-Date).AddHours(-24)
)
write-host "Starting $(Resolve-Path $myinvocation.InvocationName)" -ForegroundColor green
write-host "PSScriptroot is $PSScriptRoot" -ForegroundColor green
#create a global copy for testing purposes
$global:mi = $MyInvocation
Write-Host "Getting $name since $since" -fore yellow
write-host "Ending $($myinvocation.mycommand)" -ForegroundColor green
throw "I failed"
我可以从 $MyInspiration 捕获脚本名称。我更进一步,使用 Resolve-Path 将任何 PSDrive 或相对路径转换为完整文件系统路径。 $PSScriptRoot 反映当前命令的位置。但这就是事情变得有趣的地方。
调用信息
我的演示脚本故意抛出异常。我们来看一下。
这是同一类型的对象,但属性的填充方式不同。在这里我可以看到错误发生的位置,即第 16 行。事实上,我什至看到了该行文本。这有可能。
调用调试
使用函数可以得到相同的结果。事实上,当您使用函数来获取 ps1 文件时,效果会更好。这是来自异常对象的 InloggingInfo。
这并没有告诉我运行了什么函数,但它确实显示了我尝试在失败的函数内运行的命令、行号和源文件!
有了这些信息,我可以将代码添加到脚本中来编辑源文件并直接跳转到有问题的行。如果您仍在使用 PowerShell ISE,则可以使用 $PSISE 对象模型将其自动化。但是,PowerShell ISE 是一个编辑器,您无法让它自动运行脚本。相反,我有一个辅助函数来创建一个包含所需信息的临时文件。
Function New-ISETemp {
[cmdletbinding()]
[Outputtype("System.IO.FileInfo")]
Param([string]$FilePath, [int]$LineNumber)
$content = @"
#Press F5 to Run This Script
`$f = `$psise.CurrentPowerShellTab.files.add("$FilePath")
`$psise.CurrentFile.Editor.Focus()
`$LineNumber = $LineNumber
`$f.Editor.SetCaretPosition(`$LineNumber,1)
`$f.editor.SelectCaretLine()
"@
$tmp = [system.io.path]::GetTempFileName() -replace "\.tmp$", ".ps1"
$content | Out-File -FilePath $tmp
#write the temp file as the function output
$tmp
}
在我的函数中,我有代码在对我的函数使用 -Debug 时在 PowerShell ISE 中打开临时文件。
Function Get-Foo2 {
[cmdletbinding()]
Param(
[Parameter(Position = 0, Mandatory)]
[string]$Name, [
datetime]$Since = (Get-Date).AddHours(-24)
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Processing $Name"
#create a global copy of myinvocation for testing purposes
$global:mi = $myinvocation
Write-Host "Getting $name since $since" -fore yellow
Try {
Get-Service $name -ErrorAction Stop
}
Catch {
Write-Warning $_.exception.message
write-Warning $DebugPreference
#export for testing purposes
$_.invocationInfo | Export-Clixml d:\temp\demo-error.xml
if ($DebugPreference -match 'continue|inquire') {
$src = $_.invocationInfo.PSCommandPath
$line = $_.invocationInfo.ScriptLineNumber
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Debugging line $line"
$f = New-ISETemp -filepath $src -linenumber $line
powershell_ise $f
}
}
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
}
PowerShell ISE 启动并加载了我的临时文件。
我需要做的就是按 F5,加载源文件并选择有问题的行。
使用 VS Code 调用调试
如果您使用 VS Code 作为编辑器,这会更容易。这是我的函数的 VSCode 版本。
Function Get-Foo2 {
[cmdletbinding()]
Param(
[Parameter(Position = 0, Mandatory)]
[string]$Name, [
datetime]$Since = (Get-Date).AddHours(-24)
)
Begin {
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Processing $Name"
#create a global copy of myinvocation for testing purposes
$global:mi = $myinvocation
Write-Host "Getting $name since $since" -fore yellow
Try {
Get-Service $name -ErrorAction Stop
}
Catch {
Write-Warning $_.exception.message
write-Warning $DebugPreference
#export for testing purposes
$_.invocationInfo | Export-Clixml d:\temp\demo-error.xml
if ($DebugPreference -match 'continue|inquire') {
$src = $_.invocationInfo.PSCommandPath
$line = $_.invocationInfo.ScriptLineNumber
$goto = "{0}:{1}" -f $src, $line
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Debugging $goto"
#launch VS Code and jump to the line
code.cmd --goto $goto
}
} #catch
} #process
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
} #end
}
现在,当我使用 -Debug 运行该函数时,VS Code 会加载该文件并直接跳转到该行。
概括
您可能不需要在代码中合并这种递归调试。但我认为它演示了如何利用 InspirationInfo 对象的不同属性。如果你想出了一个有用的方法,我很想听听。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag