Solved

powershell get server name

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. 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

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 …
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

756 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