PS: Get service state on all servers

This script crawls all server operating systems in the currend ADDS environment and tries to find out whether the W3C Publishing server is installaed and then reads the service state.


Requirements

  • Active Directory module

Usage

You have to change the variable and maybe the output path for the csv file.


Script code:

	
$ServiceName = "WinDefend"
$CSVFileName = "C:\temp\$ServiceName-Status.csv"

Write-Host -ForegroundColor red "Getting $ServiceName installation and execution status for all server operating systems in the currend ADDS domain"
Write-Host -ForegroundColor green "Loading ADDS module for PowerShell..."
Import-Module ActiveDirectory
Write-Host -ForegroundColor green "Creating List of systems wtih server operating systems..."
$Serverlist = Get-ADComputer -Filter * -Properties OperatingSystem, OperatingSystemServicePack, OperatingSystemVersion | Where-Object {$_.OperatingSystem -like "Windows Server *"} | Select-object DNSHostName, OperatingSystem, ServiceStatus, SystemStatus
Write-Host -ForegroundColor green "Creating List of systems wtih server operating systems..."
$Serverlist | foreach {
    Write-Host -ForegroundColor yellow -NoNewline "Testing system: "
    Write-Host $_.DNSHostName
    if (Test-Connection -ComputerName $_.DNSHostName -Quiet -TimeToLive 2 ) {
        $ServiceState = Get-Service -ComputerName $_.DNSHostName | Where-Object {$_.Name -eq $ServiceName} | Select-object -ExpandProperty Status
        $_.SystemStatus = "Reachable"
        if ($ServiceState -ne $ null) {
            $_.ServiceStatus = ("Installed and "+$ServiceState)
        }
        else {
            $_.ServiceStatus = ("Not installed")
        }
    }
    else {
        $_.SystemStatus = "Not reachable"
        $_.ServiceStatus = ("No status")
    }
}
Write-Host -ForegroundColor green "Exporting CSV file to: $CSVFileName"
$Serverlist | Export-Csv -Encoding utf8 -NoTypeInformation -Delimiter ";" -path $CSVFileName
	

>> syntax highlighting powered by highlight.js