Solved

Verify if service is running on a list of servers

Posted on 2016-09-28
2
33 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Property cannot be found on a hashtable passed in from another script 8 38
Powershell 2.0/3.0 2 40
powershell script error 2 30
Powershell error using sql agent job 24 33
Utilizing an array to gracefully append to a list of EmailAddresses
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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