obtain all running server names and put into array??

Usually I make scripts with an array containing server names.
Servers come and go, how is it possible in PS to do this??

  1. Find all running server names
  2. Put all these server names into an array, e.g. $servers = @(‘x5’,‘x6’,‘x7’,‘x8’)
  3. Do something with the array
    Thank you, Tom

Are you asking us to teach you how to use Powershell? :wink: :smiley: Well this forum is not really about writing scripts for you. It’s more about answering specific questions about Powershell scripting. Do you have a specific question about a particular issue you have with a Powershell script you wrote?
If you already have some code to show you can post it here and we’ll be pleased to try to help you out.

based on your statement “Usually I make scripts with an array containing server names”, it sounds like you already have the answer to number 2 and 3 on your list. How you go about number 1 depends on your environment. Think about how you create your static list of server names now. Do you just know them off the top of your head? Do you pull them from a master list somewhere? That question is really about how you organize data in your environment, and less about how to do that with powershell. Really the powershell is the easy part once you figure out how to organize your server list in your environment.

I don’t want to update all the scripts all the time to accommodate servers that come and go.

Also, for at least one script I use, if the server is not running, an error is thrown…but that server could be re-activated later…continually updating scripts is not optimal.

I want to dynamically obtain and work with the running servers.
That is all. Doing so should not be dependent on anything but:
is this server running?? if yes, add it to the array. when done looking at servers, process the array.

This is how I normally do it.

$Servers = (Get-ADComputer -Filter {OperatingSystem -like "*Server*"}).Name
foreach($Comp in $Servers){
    #If the server replies Ping request
    If(Test-Connection -ComputerName $Comp -Count 1 -Quiet){
        #Do-Something or create another servers list

Hope this helps!

Thank you, that helps a lot, gets me started.
I had trouble figuring out a good search term to get good/usable results.

Do you think it’s better or faster to test for RPC response rather than pinging??
This is a completely internal script, perhaps RPC response testing would be quicker.

How did you put the code into your post, please??

Thank you, Tom

Well, it depends.
Perhaps play around with a few other commands like below:

#Check the service port

#Check the StatusCode if it's 200
Invoke-WebRequest www.google.com

How did you put the code into your post, please??
Click on pre at the start and /pre at the end.

Depends on what?? :slight_smile:
I’m simply looking for suggestions of quickest way to test if server is up or down. :slight_smile:
Thank you, Tom

Thank you on the pre tags, I’ll try them again here:


That above ‘nothing’ was wrapped in a table tag, the pre thingamajig will not render HTML tags containing left/right brackets.

You could also have them in a CSV file and just edit the file whenever one of these servers come and go rather than modifying the script itself. Have you tried that?

I don’t want to have to remember to edit files. :slight_smile:
I’ve already found all I need to gather all the servers, test them, next is to learn how to build an array containing the desired servers, which should be reasonably easy.
Thank you, Tom