Skip to content

Instantly share code, notes, and snippets.

@markwragg
Last active May 1, 2022 20:12
Show Gist options
  • Select an option

  • Save markwragg/cb07238c1364bffa37c10917c7561625 to your computer and use it in GitHub Desktop.

Select an option

Save markwragg/cb07238c1364bffa37c10917c7561625 to your computer and use it in GitHub Desktop.
Powershell pester script with various examples of using pester for operational validation.
Describe 'My process checks' {
Context 'Checking essential Windows processes are running' {
It 'winlogon.exe is running' {
get-process -Name 'winlogon' | Should be $true
}
}
$CPUThreshold = 10
Context "Checking for processes using > $CPUThreshold% CPU" {
$CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
$Samples = (Get-Counter “\Process(*)\% Processor Time” -ErrorAction SilentlyContinue).CounterSamples
$Samples | Select InstanceName,@{Name=”CPU”;Expression={[Decimal]::Round(($_.CookedValue / $CpuCores), 2)}} `
| Sort-Object InstanceName `
| Where-Object {$_.InstanceName -ne "idle" -and $_.InstanceName -ne "_total"} `
| ForEach-Object {
It "Process $($_.InstanceName) is using $($_.CPU)% CPU" {
$_.CPU | Should Not BeGreaterThan 10
}
}
}
}
Describe 'Weird science checks' {
Context 'Checking for the impossible' {
It 'It should be opposite day' {
$true | Should be $false
}
}
}
Describe 'Day of the week check' {
$WeekDays = "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"
$WeekDays | ForEach-Object {
It "Today should be $_" {
(get-date).DayOfWeek | Should be $_
}
}
}
Describe 'Operating System check' {
It 'C:\Windows folder exists' {
'C:\Windows' | Should Exist
}
It 'C:\Linux folder does not exist' {
'C:\Linux' | Should Not Exist
}
}
Describe 'Windows service check' {
Get-Service -DisplayName "Windows*" | ForEach-Object {
It "The $($_.DisplayName) service should be running" {
$_.Status | Should be 'Running'
}
}
}
@mark05e

mark05e commented May 1, 2022

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment