Author Posts

June 10, 2013 at 2:23 pm

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

 

June 10, 2013 at 3:00 pm

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.

June 10, 2013 at 3:30 pm

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

Thank you, Tom

June 10, 2013 at 3:34 pm

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

June 10, 2013 at 3:49 pm

OIC — thank you!!
I will test tomorrow when I'm at work...
Thank you, Tom

June 11, 2013 at 5:38 am

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: (:) [Test-Connection], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand
 `

June 11, 2013 at 6:40 am

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.

June 11, 2013 at 6:44 am

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.

June 11, 2013 at 6:47 am

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...

June 11, 2013 at 6:52 am

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.