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

Powershell

Avatar of undefined
Last Comment
MilesLogan
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of MilesLogan
MilesLogan
Flag of United States of America image

ASKER

Thank you Subsun !
Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo