why does test-connection not work right?? :) :)

<pre class="lang:ps decode:true ">cls
Enter-PSSession -ComputerName dc1 -Credential FOO\administrator
Set-Location -Path C:
Import-Module ActiveDirectory

$servers = Get-ADComputer -Filter {operatingsystem -like “Windows Server*”}

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.

Thank you, Tom

 

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.

If I use $test = Test-Connection $server.domain.local -quiet will it work properly???

Thank you, Tom

No, what you want is the server name
e.g.
Test-Connection $Server.Name -Quiet

OIC – thank you!!
I will test tomorrow when I’m at work…
Thank you, Tom

I tried $server.name as follows:

cls
Enter-PSSession -ComputerName dc1 -Credential essexarc\administrator
Set-Location -Path C:
Import-Module ActiveDirectory

$servers = Get-ADComputer -Filter {operatingsystem -like “Windows Server*”}
#$path = “\mls-storage2\backup\changelogs”

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

  • $test = Test-Connection <<<< $server.name -quiet
    • CategoryInfo : InvalidData: (:slight_smile: [Test-Connection], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand
      `

Make sure that you actually have some ADComputer objects in the $servers variable. I think there’s a problem with your call to Get-ADComputer.

You cannot use Enter-PSSession in a script. The subsequent commands in the script will not run there, they will run in the local session.

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.