how to loop through all server names??

by tommls at 2012-08-23 08:17:41

I found this code to get all the server names in our domain:

"Here’s an alternate way to poll AD for all servers using Microsoft’s AD module: Get-ADComputer -Filter {operatingsystem -like “server”}"

What I want to do is loop through the servers and copy certain files from each server to a different name onto a backup location.

foreach servername
copy servername\C$\file.txt to \foo\backup\configs\ + servername + "_file.txt"

basically backup each file.txt within each server to a new file prefixed by the servername

I’m stumped on how to loop through the server names, first I want to get that working, then figure out the copy file.txt to a new filename.txt on another storage…

Thank you, Tom
by JeffH at 2012-08-23 10:45:30
Have you tried Get-ADComputer yet? What you get back is an object like this:

DistinguishedName : CN=CHI-DB01,CN=Computers,DC=GLOBOMANTICS,DC=local
Enabled : True
Name : CHI-DB01
ObjectClass : computer
ObjectGUID : f276a122-4732-42d9-b5e0-14d1ab84cccb
SamAccountName : CHI-DB01$
SID : S-1-5-21-2552845031-2197025230-307725880-1138
UserPrincipalName :

So you’ll want to use the Name property. I would get all the computers and save them to a variable.

$servers=Get-ADComputer -Filter {operatingsystem -like “server”}

Now, I can do this
foreach ($server in $servers) {
Write-Host “Copying files from $($” -foreground green
#insert your copy commands here
by tommls at 2012-08-23 10:57:55
the variable part is what I was looking for, I already had the Get-ADComputer part in my original post.

however I need to check if the server is up and running…
I found this:
If (Test-Connection remotehost -quiet) {
Write ‘Reachable!’
where I’d change remotehost to $server??
and where it says ‘reachable’ I’d do the copy stuff
if Test-Connection is false don’t do anything…
Now I must test all this…

I found this code in your Morning Script (I am NOT a Mornng Person.):
(Test-Connection -ComputerName $computername -quiet -Count 2)
I’ll try this too…thank you for writing Morning Script…

Thank you for your help!!