PowerShell/Pester函数调用缺少的日志和交互性



这是我的设置(简化(,我创建一个函数并将其传递给Run-Test,然后Run-Test调用该函数。但现在的情况是,我只看到了警告信息。我想知道有没有一种方法可以交互地查看它在每一步和每一行的表现?

function Run-Test
{
param(
[Parameter(Mandatory = $true)]$test,
[Parameter(Mandatory = $true)]$name,
[Parameter(Mandatory = $true)]
[Alias('resourceGroupName')]
[String[]]$resourceGroupNames
)
Describe $name {
Context "BeforeEach/AfterAll" {
BeforeAll {
Write-Output "Cleaning up before running test: $name"
foreach ($rgName in $resourceGroupNames)
{
Unlock-ResourceGroup -Name $rgName
}
}

AfterAll {
Write-Output "Cleaning up after running test: $name"
foreach ($rgName in $resourceGroupNames)
{
Unlock-ResourceGroup -Name $rgName
}
}

It "Should succeed failover and failback" $test
}
}
}

Run-Test `
-Name "Testing ..." `
-ResourceGroupName $primaryResourceGroupName, $recoveryResourceGroupName -Test {
# Create primary and recovery resource groups
New-AzResourceGroup -name $recoveryResourceGroupName -location $location -force
New-AzResourceGroup -name $primaryResourceGroupName -Location $location -Force
$primaryResourceGroupId = (Get-AzResourceGroup -Name $primaryResourceGroupName).ResourceId
$recoveryResourceGroupId = (Get-AzResourceGroup -Name $recoveryResourceGroupName).ResourceId
# print these
$primaryResourceGroupId
$recoveryResourceGroupId
} 

使用PowerShell ISE,您可以调试脚本以查看每一行发生的确切情况。

在ISE中打开您的脚本。当您选择";编辑";在.ps1文件的上下文菜单中。或者打开ISE,然后从那里打开脚本
现在设置一个要开始调试的断点:将光标放在您选择的行中,然后按F9。
现在运行脚本。它应该停在那条线上。现在,您可以逐行浏览脚本并查看发生了什么。

来源:https://devblogs.microsoft.com/scripting/use-the-powershell-debugger/

最新更新