当然。您可以使用 Set-PSBreakpoint cmdlet 在 PowerShell 中创建条件断点。考虑以下代码。将其保存为脚本文件,然后执行。有在线 cmets 可以帮助您了解发生了什么。
请记住,有三种不同类型的断点:
命令断点
此代码示例使用命令断点类型,因为我告诉它只在Get-WmiObject 命令上设置断点。您可以交替指定特定的行号或变量断点类型。您使用-Action 参数来指定您希望设置断点的条件。您必须在 -Action ScriptBlock 中的某处使用 break 关键字,以指示调试器暂停执行 PowerShell 脚本。
# 1. Reset $Error to $null
$WmiError = $null;
# 2. Clean up any existing breakpoints
Get-PSBreakpoint | Remove-PSBreakpoint;
# 3. Set breakpoint, but only on Get-WmiObject commands, when the $WmiError variable is not $null
Set-PSBreakpoint -Command Get-WmiObject -Action { if ($WmiError) { break; } };
# 4. Failed Get-WmiObject command
Get-WmiObject -Class Win32_NonExistentClass -ErrorVariable WmiError;
# 5. Successful Get-WmiObject command
# PowerShell breaks here, because:
# - It's a Get-WmiObject command
# - The $WmiError variable is not null
Get-WmiObject -Class Win32_BIOS;
由于您提到使用Write-Error,您可以在出现Write-Error 的行上设置PSBreakpoint。以下是如何做到这一点的示例:
Set-PSBreakpoint -Command Write-Error -Action { break; };
很简单,对吧?
变量断点
此示例使用变量PSBreakpoint 类型,但仅当变量的内容被修改时。您可以使用-Mode参数来确定在什么条件下命中变量断点:
代码:
# 1. Clean up any existing breakpoints
Get-PSBreakpoint | Remove-PSBreakpoint;
# 2. Set a PSBreakpoint of type "variable" on a variable named "Data," but only when it has changed
Set-PSBreakpoint -Action { Write-Host -ForegroundColor Green -Object ('The $Data variable has changed! Value is: {0}' -f $Data); break; } -Variable Data -Mode Write;
# 3. No break on this line, because we are not changing the variable
Write-Host -Object $Data;
# 4. Execution is paused on this line, because we change the variable
$Data = 1;
行断点
现在我们已经查看了 variable 和 command PSBreakpoint 类型,最后要探索的断点类型是 line断点。如果您要复制/粘贴下面的代码,保存并执行它,您会看到代码在 Write-Host 行(恰好是第 9 行)上中断,但仅在 Name 属性时$Service 变量等于 WinRM。这就是-Action 参数的ScriptBlock 中的条件语句所定义的内容。
# 1. Clean up any existing breakpoints
Get-PSBreakpoint | Remove-PSBreakpoint;
# 2. Set a PSBreakpoint of type "line" on line #8, but only if the $Service variable's Name property equals 'winrm'
Set-PSBreakpoint -Action { if ($Service.Name -eq 'winrm') { break; } } -Line 9 -Script $MyInvocation.MyCommand.Path;
# 3. Get a list of Windows Services and iterate over them
foreach ($Service in (Get-WmiObject -Class Win32_Service)) {
Write-Host -Object ('Service name is: {0}' -f $Service.Name);
}