Hello,
I have a scenario where I need to check if necessary services are up and running after the servers reboot. They are not just one or two servers that I can check with Get-Service cmdlet and go through the service lists but they are hundreds. If you are a GUI admin then this is not being possible to check them within (let’s say) half an hour. So I went through a few options to collect the services that need checking. My options were to keep those services in xml, JSON or csv file. After running the following scripts, I compare the output file size.
Get-services -cn $Computer | Where{($_.Status -eq "Running") -and ($_.StartType -eq "Automatic")} |
Sort Name | select Name,Status,StartType | Export-Csv c:\temp\Services\$Computer.csv -notype
Get-services -cn $Computer | Where{($_.Status -eq "Running") -and ($_.StartType -eq "Automatic")} |
Sort Name | select Name,Status,StartType | ConvertTo-JSON | Out-File c:\temp\Services\$Computer.JSON
Get-services -cn $Computer | Where{($_.Status -eq "Running") -and ($_.StartType -eq "Automatic")} |
Sort Name | select Name,Status,StartType | export-clixml c:\temp\Services\$Computer.xml
.csv files are the smallest. So I choose csv format and collect all those service details when the machines are running in a stable state. I then wrote the Pester script below. When writing the script I have a few questions come up in my mind.
- Is Pester the right too for such testing (I could just write the PowerShell script which would do the same thing). It does work but I am not that convinced
- Is there any way to replace this part - If (!($PingResult)) {Continue}, maybe in a Pester way?
Could someone enlighten me?
$CsvPath= "C:\temp\Services"
Describe 'Service status check'{
Context 'Csv Files Check'{
$Files = (Get-ChildItem $CsvPath\*.csv)
Foreach($file in $Files){
It "$file Size should be greater than 0KB"{
$file.length | Should BeGreaterThan 0
}
}
}
Context 'Server Check'{
Foreach($Computer in $Files.basename){
$PingResult = (Test-Connection $Computer -Count 1 -Quiet)
It "$Computer should be up and runing"{
$PingResult | Should Be $true
#Test-Connection $Computer -Count 2 -Quiet | Should Be $true
}
If (!($PingResult)) {Continue}
Context "$Computer Service Check"{
$Services = (Import-Csv $CsvPath\$Computer.csv)
Foreach($Service in $Services){
It "$Computer $Service should be running"{
(Get-Service -ComputerName $Computer -Name $Service.Name).Status | Should Be 'Running'
}
}
}
}
}
}
#Invoke-Pester .\ServicesCheck.tests.ps1
This is my first post so go easy on me ![]()
Thanks