Solved

Powershell script only shows one computer name

Posted on 2013-05-29
9
488 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Cheers guys Subsun's solution worked a treat. Much appreciated
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
0
 
LVL 68

Expert Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

763 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

12 Experts available now in Live!

Get 1:1 Help Now