find, loop through all windows servers??

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of tommls tommls 3 years, 10 months ago.

  • Author
    Posts
  • #7381
    Profile photo of tommls
    tommls
    Participant

    Finally!!! After loading this site into several browsers I find one that allows me to TYPE my post!!! 🙂 🙂 🙂

    I don't know where to find/get the old forums since I think I may have asked this already somewhere.

    I want to get and loop through all the servers that PowerShell can find and know about — aka powered-on, check if it's a Windows server like so:

    This is PS 2.0 code, so:

    $servers = $servers | foreach ($_name)

    If Get-ADComputer – filter {operatingsystem -Like "Windows Server*"}

    copy \\$server\c$\*changelog.txt \\foo\backup

    end if

    next end for (I can find whatever it is that ends a for loop)

    That's the general code. Where I'm stuck is:

    1) constructing the for-each loop to get the servers

    2) testing the server

    Do I have these parts correct??

    Thank you, Tom

  • #7385
    Profile photo of Don Jones
    Don Jones
    Keymaster

    The old forums content was pulled over, so it's here and searchable.

    PowerShell uses C-like syntax. So you're thinking...

    $computers = Get-ADComputer -filter (whatever)
    foreach ($computer in $computers) {
      if ($this -eq $that) {
        # do whatever
      }
    }

    In terms of testing the server, it depends a bit. If your network is set up to allow a ping, you can run if (test-host $computer -quiet) { #do whatever } to ping the computer.

  • #7386
    Profile photo of tommls
    tommls
    Participant

    Thank you!! I found the archives once, I can find them again...
    That general code snippet should get me started etc.
    Thank you, Tom

You must be logged in to reply to this topic.