Solved

powershell get server name

Posted on 2014-04-04
5
1,017 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
5 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
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
Comment Utility
Select-Object : Key "expression" cannot have an empty string value.
At line:1 char:164
0
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
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 39

Expert Comment

by:footech
Comment Utility
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 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

8 Experts available now in Live!

Get 1:1 Help Now