Solved

Powershell script only shows one computer name

Posted on 2013-05-29
9
495 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Author Closing Comment

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

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 70

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 70

Expert Comment

by:Qlemo
ID: 39206090
Ok, so I'm right. Thanks for confirming.
0

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

717 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