Solved

Verify if service is running on a list of servers

Posted on 2016-09-28
2
26 Views
Last Modified: 2016-09-28
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

0
Comment
Question by:MilesLogan
2 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 41820248
I have added a IF condition to check if server is pinging or not..
Try...
$Computers = Get-Content C:\PS\servers.txt

$output = @()

foreach ($c in $Computers)
{
If (Test-Connection $c -Quiet -Count 2){
	$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
		}
	}
 }Else{
		$output += "" | Select @{N="MachineName";E={$c}},Name,@{N="Status";E={"Server not up"}}
 }
}
$output | Out-GridView

Open in new window

0
 
LVL 2

Author Closing Comment

by:MilesLogan
ID: 41820292
Thank you Subsun !
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now