[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

Powershell script only shows one computer name

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
cwstad2
Asked:
cwstad2
  • 3
  • 3
  • 2
  • +1
3 Solutions
 
Joseph DalyCommented:
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
 
Joseph DalyCommented:
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
 
SubsunCommented:
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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
cwstad2Author Commented:
Cheers guys Subsun's solution worked a treat. Much appreciated
0
 
QlemoDeveloperCommented:
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
 
QlemoDeveloperCommented:
That posts claims my example would work - it does not. Pipe or non-piped, only a single machine appears in the output.
0
 
SubsunCommented:
I have tested it.. It works for me in PS3.. in PS2 I get only the first object..
0
 
QlemoDeveloperCommented:
Ok, so I'm right. Thanks for confirming.
0

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now