I am attaching a command breakpoint for every "Get*" in my powershell session. This command breakpoint involves calling an Action which invokes Write-Host "We are trapped in this code". As you can see below
Eg.
PS /Users/test> Set-PSBreakpoint -Command "Get*" -Action {
>> Write-Host "We are trapped in this code"
>> }
ID Script Line Command Variable Action
-- ------ ---- ------- -------- ------
1 Get* …
PS /Users/test> Get-Runspace
We are trapped in this code
Id Name ComputerName Type State Availability
-- ---- ------------ ---- ----- ------------
1 Runspace1 localhost Local Opened Busy
PS /Users/test>
Now instead of printing
We are trapped in this code
we want to print
We are trapped in this code: Get-Runspace
In this specifically we want to print the command ie. Get-Runspace on which breakpoint was applied. Is there any way to get the hold of the Executing Command within the ScriptBlock.
PS: We have tried $MyInvocation.MyCommand.Name within the script block and various other methods but all of them return null.
Assuming that you're interested in the command line that triggered the breakpoint, use $MyInvocation.Line
:
Set-PSBreakpoint -Command "Get-conten*" -Action {
Write-Host "We are trapped in this code: $($MyInvocation.Line)"
}
If you want to inspect the specific values of the automatic $MyInvocation
variable (which is an instance of type System.Management.Automation.InvocationInfo
), pipe it to the Out-Host
cmdlet:
Set-PSBreakpoint -Command "Get-conten*" -Action {
$MyInvocation | Out-Host
}