Search code examples
powershellpester

PowerShell/Pester function invoke missing logs and interactivity


This is my setup (simplified) where I create a function and pass it to Run-Test and then Run-Test invokes the function. But what is happening is I only see the warning messages. I am wondering is there a way to see exactly its doing at each step and line interactively?

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
} 

Solution

  • Using the PowerShell ISE, you can debug your script to see exactly what happens at each individual line.

    Open your script in the ISE. This is the default when you choose "Edit" in the context menu on a .ps1 file. Or open ISE and then open the script from there.
    Now set a breakpoint where you want to start debugging: Place the cursor in the line of your choice and press F9.
    Now run the script. It should stop at that line. You can now step through the script line by line and check out what's up.

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