How to query a service from computers file

I have a CSV file with lots of servers: Server.csv and header "name". I want to read the file and query for a service and eventually produce a csv with computer name and name of the service I am looking for and a list of the computer where the nxlog service does not exist

$CORP_SERVERS = Import-CSV c:\scripts\CORP_Servers.csv
foreach($CorpServer in $CORP_SERVERS.name){ get-service nxlog -ComputerName $CorpServer.name } | Select $CorpServer.name

Open in new window


Thanks for your help
namergSystems AdministratorAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
Try this:
Import-CSV c:\scripts\CORP_Servers.csv | ForEach-Object {
	$ComputerName = $_.Name
	Write-Host "Processing $($ComputerName) ..."
	$(Try {
		Get-Service nxlog -ComputerName $ComputerName -ErrorAction Stop
	} Catch {
		$_.Exception.Message | Select-Object -Property @{n='Error'; e={$_}}
	}) | Select-Object -Property @{n='ComputerName'; e={$ComputerName}}, Name, DisplayName, Status, Error
} | Export-Csv -NoTypeInformation -Path C:\Temp\nxlog.csv

Open in new window

0
 
namergSystems AdministratorAuthor Commented:
Thank you sir
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.