Author Posts

October 19, 2017 at 7:22 pm

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

October 19, 2017 at 7:31 pm

Are you asking us to teach you how to use Powershell? 😉 😀 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.

October 19, 2017 at 8:26 pm

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.

October 19, 2017 at 8:32 pm

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.

October 19, 2017 at 11:57 pm

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!

October 20, 2017 at 12:01 am

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

October 20, 2017 at 12:14 am

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

#Check the service port
Test-NetConnection 10.1.1.10:80

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

October 20, 2017 at 12:25 am

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

October 20, 2017 at 12:28 am

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

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

nothing

October 20, 2017 at 12:42 am

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

October 20, 2017 at 3:05 pm

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?

October 20, 2017 at 3:07 pm

I don't want to have to remember to edit files. 🙂
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