Solved

Powershell script only shows one computer name

Posted on 2013-05-29
9
489 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
 
LVL 15

Author Closing Comment

by:cwstad2
ID: 39205888
Cheers guys Subsun's solution worked a treat. Much appreciated
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 68

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 68

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 68

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

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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 …

863 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

26 Experts available now in Live!

Get 1:1 Help Now