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

This topic contains 9 replies, has 3 voices, and was last updated by  Rob Campbell 4 years, 5 months ago.

  • Author
    Posts
  • #7397

    tommls
    Participant
    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

     

  • #7398

    Rob Campbell
    Participant

    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.

  • #7399

    tommls
    Participant

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

    Thank you, Tom

  • #7400

    Rob Campbell
    Participant

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

  • #7401

    tommls
    Participant

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

  • #7404

    tommls
    Participant

    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
     `
  • #7415

    Jeffrey Smith
    Participant

    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.

  • #7416

    Rob Campbell
    Participant

    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.

  • #7417

    tommls
    Participant

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

  • #7423

    Rob Campbell
    Participant

    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.

You must be logged in to reply to this topic.