Solved

powershell get server name

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

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 39

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 69

Accepted Solution

by:
Qlemo earned 500 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Set OWA language and time zone in Exchange for individuals, all users or per database.
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 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