Solved

powershell get server name

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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