?
Solved

powershell get server name

Posted on 2014-04-04
5
Medium Priority
?
1,596 Views
Last Modified: 2014-04-07
I have this powershell command which returns information but need to also get the server name. Can anyone show me how to get the server name along with teh other info returned.

gc servers.txt|%{Get-Service -ComputerName $_| Where-Object { $_.status -match "running" -and $_.DisplayName -match "(SNMP Service)" }

currently displayed

Status  name    displayname


What I need is

servername  status   name   displayname


thanks

Running V2 and also quest powershell commandlets.
0
Comment
Question by:techdrive
[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
5 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39979156
Try this (untested):
gc servers.txt|%{Get-Service -ComputerName $_| Where-Object { $_.status -match "running" -and $_.DisplayName -match "(SNMP Service)" | Select-Object *, @{name="servername"; expression="$($_.servername)"}}

Open in new window

HTH,
Dan
0
 

Author Comment

by:techdrive
ID: 39979177
Select-Object : Key "expression" cannot have an empty string value.
At line:1 char:164
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39979188
Then it's not called $_.servername. It's probably just $_

gc servers.txt|%{Get-Service -ComputerName $_| Where-Object { $_.status -match "running" -and $_.DisplayName -match "(SNMP Service)" | Select-Object *, @{name="servername"; expression="$_"}}

Open in new window

0
 
LVL 40

Expert Comment

by:footech
ID: 39979204
Being further down the pipeline at the point of the Select-Object command, we need to grab the information further upstream.  Something like below will work:
gc servers.txt|%{$comp = $_; Get-Service -ComputerName $_| Where-Object { $_.status -match "running" -and $_.DisplayName -match "(SNMP Service)"} | Select-Object *, @{name="servername"; expression="$($comp)"}}

Open in new window

0
 
LVL 70

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 39979984
The computer name is there, it just isn't displayed by default. So simply use:
gc servers.txt|% {
  Get-Service -ComputerName $_ -DisplayName "SNMP Service" } |
  Where-Object { $_.status -eq "Running" } |
  select @{n="ServerName"; e={$_.MachineName}}, Status, Name, DisplayName

Open in new window

You should try to filter the service names as soon as possible to reduce the amount of exchanged data when using remote targets.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

765 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