Solved

Powershell script only shows one computer name

Posted on 2013-05-29
9
490 Views
Last Modified: 2013-05-29
Hi guys, i have written the following script below but it only displays one line, there are around 20 in the text file + it doent show the computer name. Any ideas how i can improve this to show all.

thanks

$Computers = Get-Content "C:\IT\exchangeserverservices.txt"
Get-Service -name MSExchangeMailboxReplication -ComputerName $Computers | Select name,status,displayname |sort name |format-table -autosize
0
Comment
Question by:cwstad2
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 35

Assisted Solution

by:Joseph Daly
Joseph Daly earned 100 total points
ID: 39205799
My guess would be that when you are performing the get-content it is taking the whole list in at one time.

I would suggest saving the list as a CSV file with a column heading of servername. Then you can run a command similar to below.

import-csv "C:\IT\exchangeserverservices.csv" | foreach {Get-Service -name MSExchangeMailboxReplication -ComputerName $servername | Select name,status,displayname |sort name |format-table -autosize }
0
 
LVL 35

Assisted Solution

by:Joseph Daly
Joseph Daly earned 100 total points
ID: 39205812
Also if you want to keep it as a text file you may do the following.

$computers = get-content "C:\file.txt"
foreach ($computer in $computers)
{Get-Service -name MSExchangeMailboxReplication -ComputerName $computer | Select name,status,displayname |sort name |format-table -autosize } 

Open in new window

0
 
LVL 40

Accepted Solution

by:
Subsun earned 400 total points
ID: 39205815
You can try
Get-Content "C:\IT\exchangeserverservices.txt" | % {
Get-Service -name MSExchangeMailboxReplication -ComputerName $_ | Select MachineName,name,status,displayname |sort name |format-table -autosize
}

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 15

Author Closing Comment

by:cwstad2
ID: 39205888
Cheers guys Subsun's solution worked a treat. Much appreciated
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39205952
Seems to be a bug in Get-Service. You can provide multipe services and/or computers, and all are checked - but only the results for the first computer given will be pushed into the pipe. You can test that with
Get-Service wuauserv, w32time -ComputerName pc1, pc2 | select MachineName, Name, ServiceName, Status

Open in new window

This is confirmed in http://powershell.com/cs/forums/t/5025.aspx . Hence all examples shown will use a loop as shown above.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39205990
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39206044
That posts claims my example would work - it does not. Pipe or non-piped, only a single machine appears in the output.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39206069
I have tested it.. It works for me in PS3.. in PS2 I get only the first object..
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39206090
Ok, so I'm right. Thanks for confirming.
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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
A brief introduction to what I consider to be the best editor for PowerShell.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

816 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

10 Experts available now in Live!

Get 1:1 Help Now