AdComputer

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of adi dumitras adi dumitras 2 years, 2 months ago.

  • Author
    Posts
  • #18721
    Profile photo of Itamar Tziger
    Itamar Tziger
    Participant

    $allcomputers = $AllServers = Get-ADComputer -Filter * -Property * -SearchBase 'OU=a,OU=b,OU=c,DC=domain,DC=local'

    $computers = $Allcomputers | select -Property Name

    $computer not equal to name is show me as arry

    this is what i get

    Name
    —-
    OHAD_B

    how can i get the name i want to pass the $computers to foreach loop

    foreach ($computer in $computers.Name) {

    if (test-Connection -Cn $computer -quiet) {

    }
    }

    Plaes Help

    TNX
    ITamar

  • #18957
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Use "Select -expandproperty name" instead of "Select -property name".

    $names = Get-ADComputer -filter * -searchbase "ou=a,dcdomain,dc=local" | select -expand name
    foreach ($name in $names) {

    }

  • #18958
    Profile photo of Itamar Tziger
    Itamar Tziger
    Participant

    Tnx,
    I see your powershell in lunch
    Why the different ?
    what the different between -expandproperty and -property ?

    sincerity
    Itamar

  • #18962
    Profile photo of adi dumitras
    adi dumitras
    Participant

    The difference is that with -ExpandProperty you get an array of strings that represent names whereas if you just use -Property you will get an array of custom objects that contain a name column.

    Example:

    $names = Get-ADComputer -filter * -searchbase "ou=a,dcdomain,dc=local" | select -expand name
    foreach ($name in $names) {
        Test-Connection $name
    }
    

    If you use just -Property name you would do:

    $names = Get-ADComputer -filter * -searchbase "ou=a,dcdomain,dc=local" | select -property name
    foreach ($name in $names) {
        Test-Connection $name.name
    }
    

You must be logged in to reply to this topic.