Link to home
Start Free TrialLog in
Avatar of MilesLogan
MilesLoganFlag for United States of America

asked on

Verify if service is running on a list of servers

Hi EE

Does anyone have a script to share or can help me modify the one below?
I need the script to check a txt file and get me the status of a service on the list of servers.
The script below works but I need it to also output if a server on the list is not online .

$Computers = Get-Content C:\PS\servers.txt

$output = @()

foreach ($c in $Computers)
{
	$services = Get-Service *ServiceName* -ComputerName $c | Select Name, Status
	if ($services -eq $null)
	{
		$cinfo = New-Object PSObject
		$cinfo | Add-Member -MemberType NoteProperty -Name  "MachineName" -Value $c
		$cinfo | Add-Member -MemberType NoteProperty -Name  "Name" -Value $null
		$cinfo | Add-Member -MemberType NoteProperty -Name  "Status" -Value "No ServiceName present"
		$output += $cinfo
	}
	else
	{
		foreach ($s in $services)
		{
			$cinfo = New-Object PSObject
			$cinfo | Add-Member -MemberType NoteProperty -Name  "MachineName" -Value $c
			$cinfo | Add-Member -MemberType NoteProperty -Name  "Name" -Value $s.Name
			$cinfo | Add-Member -MemberType NoteProperty -Name  "Status" -Value $s.Status
			$output += $cinfo
		}
	}
}
$output | Out-GridView

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MilesLogan

ASKER

Thank you Subsun !