obtain all running server names and put into array??

This topic contains 11 replies, has 5 voices, and was last updated by  tommls 1 month ago.

  • Author
    Posts
  • #82535

    tommls
    Participant

    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

  • #82538

    Olaf Soyk
    Participant

    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.

  • #82556

    Jeremy Murrah
    Participant

    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.

  • #82558

    tommls
    Participant

    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.

  • #82582

    Naw Awn
    Participant

    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!

    • #82585

      tommls
      Participant

      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

    • #82588

      Naw Awn
      Participant

      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
      
    • #82591

      tommls
      Participant

      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
      
    • #82589

      Naw Awn
      Participant

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

    • #82595

      tommls
      Participant

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

  • #82636

    PowershellNoob
    Participant

    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?

  • #82637

    tommls
    Participant

    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

You must be logged in to reply to this topic.