We help IT Professionals succeed at work.
Get Started

Verify if service is running on a list of servers

MilesLogan asked
1 Endorsement
Last Modified: 2016-09-28

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
		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

Watch Question
IT Infrastructure Architect
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE