foreach ($server in $servers) {
$test = Test-Connection $server -quiet
if (-not ($test)) {
Write-Host “Copying files from $($server.name)” -foreground green #copy-item \$server\c$*configuration.txt -Destination “\BAR\backup\changelogs”
}
}
Why does this code return ALL servers instead of only the ones that are really “up” (pingable)?? – it’s returning ALL servers regardless of up or down status (powered on).
I am using the not because it’s the only way I could avoid getting False back on all the servers.
Goal is to have files copied from within C$ of pingable (not powered off) servers to backup media.
Your test-connection isn’t working right (always returing $False) because your feeding it AD distinguished names (the default output of a Microsoft.ActiveDirectory.Managent.ADComputer object) that you got from Get-ADComputer. It needs a Server/DNS host name or IP address.
foreach ($server in $servers) {
$test = Test-Connection $server.name -quiet
if ($test) {
Write-Host “Copying files from $($server.name)” -foreground green #copy-item \$server\c$*configuration.txt -Destination “\mls-storage2\backup\changelogs”
}
}
`
I get this error:
Test-Connection : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument t
hat is not null or empty and then try the command again.
At D:\2013_PowerShell_Scripts\copy_changelogs_2.ps1:10 char:24
I know the Get-ADComputer thing works – I had it working yesterday.
However for other reasons I’m rebuilding my computer today and will install RSAT and AD Gateway and that should hopefully make remoting easier etc.
Thank you…
If you have remoting enabled on that DC and the AD gateway installed there, then I think it should work if you use Invoke-Command instead of Enter-PSSession.
You use Enter-PSSession to use a remote session interactively. For scripting, use Invoke-Command to run the commands in the remote session and return the results back to your script.