This gist contains all the code examples from the blog post "Adding Timeouts to Pester Tests Using PowerShell Runspaces".
Describe "Hanging Test Example" {
It "Should complete but never does" {
# Simulating a hanging operationThis gist contains all the code examples from the blog post "Adding Timeouts to Pester Tests Using PowerShell Runspaces".
Describe "Hanging Test Example" {
It "Should complete but never does" {
# Simulating a hanging operation| ## We can use the OS disk of the VM to see the VM's creation date since it's always created at the same time | |
| # Connect to Azure account (if not already connected) | |
| Connect-AzAccount | |
| # Get all VMs in the subscription | |
| $vms = Get-AzVM | |
| # Array to store results | |
| $results = @() |
| # Corrected Azure Site Recovery PowerShell Script with Azure CLI Commands | |
| # Set variables (modify these as needed) | |
| $ResourceGroup = "MyASRTestRG" | |
| $Location = "eastus" | |
| $RecoveryLocation = "westus" | |
| $VaultName = "MyASRTestVault" | |
| $PrimaryVNet = "PrimaryTestVNet" | |
| $RecoveryVNet = "RecoveryTestVNet" | |
| $StorageAccount = "myasrteststorage" |
| "Smith, John" -replace "(\w+),\s*(\w+)", '$2 $1' | |
| ## John Smith |
| function Write-CustomLog { | |
| param([string]$Message) | |
| $callerInfo = (Get-PSCallStack)[1] | |
| $scriptName = Split-Path -Leaf $callerInfo.ScriptName | |
| $lineNumber = $callerInfo.ScriptLineNumber | |
| $functionName = $callerInfo.FunctionName | |
| $logEntry = "{0:yyyy-MM-dd HH:mm:ss} - {1}:{2} - {3} - {4}" -f ` | |
| (Get-Date), $scriptName, $lineNumber, $functionName, $Message |
| $logEntries = @( | |
| "2024-06-09", | |
| "Error: Disk full", | |
| 404, | |
| "2024-06-08", | |
| "Error: Network unreachable", | |
| 500 | |
| ) | |
| foreach ($entry in $logEntries) { |
| function Remove-ItemSafely { | |
| [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] | |
| param([string]$path) | |
| if ($PSCmdlet.ShouldProcess($path, "Remove")) { | |
| Remove-Item -Path $path | |
| } | |
| } | |
| PS> Remove-ItemSafely -Path '\oh\god\wrong\file.txt' -WhatIf |
| function Get-ListOfFiles { | |
| [OutputType('System.IO.FileInfo')] | |
| [CmdletBinding()] | |
| param( | |
| [string]$Path | |
| ) | |
| Get-ChildItem -File -Path $Path | |
| } | |
| Get-ListOfFiles -Path "C:\Temp" | ForEach-Object { |
| function Get-DatabaseData { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory)] | |
| [string]$DatabaseType | |
| ) | |
| dynamicparam { | |
| $paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary | |
| $attributes = New-Object System.Collections.ObjectModel.Collection[System.Attribute] |
| function Set-LogLevel { | |
| param ( | |
| [ValidateSet("Debug", "Info", "Warning", "Error")] | |
| [string]$Level | |
| ) | |
| Write-Output "Log level set to $Level" | |
| } | |
| # Example usage: |