How can I get the ip address computer from $computers?

Welcome Forums General PowerShell Q&A How can I get the ip address computer from $computers?

This topic contains 5 replies, has 4 voices, and was last updated by

 
Participant
4 months ago.

  • Author
    Posts
  • #102863

    Participant
    Points: 0
    Rank: Member

    cls
    $DomainName = 'LDAP://CN=Computers,DC=asia,DC=com'
    $Root = New-Object DirectoryServices.DirectoryEntry $DomainName
    $objSearcher = New-Object DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $Root
    $objSearcher.SearchScope = "SubTree"

    $ADResults = @()

    $colResults = $objSearcher.FindAll()
    foreach ($objResult in $colResults)
    {

    $computer = $objResult.GetDirectoryEntry()
    #$computer.nTSecurityDescriptor

    $computer | Select-Object -Property @{Name='Name';Expression={$_.CN}},@{Name='Owner';Expression={$_.ObjectSecurity.Owner}},@{Name='Created';Expression={$_.whencreated}}
    }

  • #102872
    Jon

    Participant
    Points: 2
    Rank: Member

    I would update your code to use get-adcomputer and look at the Ipv4address property.

  • #102880

    Participant
    Points: 28
    PublishedHelping Hand
    Rank: Member

    You can get virtually all of this in a one liner.

    # Get all AD computer information
    Get-ADComputer -Filter * -Properties *
    

    Just add a Select-Object to get the properties you want.

    # Get selected AD computer information
    Get-ADComputer -Filter * -Properties Name,SamAccountName,,DisplayName,
    CanonicalName,DistinguishedName,DNSHostName,Description,Created,
    EnabledIPv4Address,IPv6Address | Select-Object -First 1
    

    No calculated property effort required, well, unless you just want to rename stuff, just because.

    If you don't have the RSAT tools enabled, you can just use PSRemoting to any DC or other machine with the RSAT tools installed and proxy those cmdlets for use on your machine, or continue to use the .Net namespace you ae using.

    The cmdlets are far more succinct / easier to use.

    • #102883

      Participant
      Points: 0
      Rank: Member

      Thnaks so much for the information, but How Can I add this on the line $computer | Select-Object -Property @{Name='Name';Expression={$_.CN}},@{Name='Owner';Expression={$_.ObjectSecurity.Owner}},@{Name='Created';Expression={$_.whencreated}} ?

  • #102886

    Participant
    Points: 0
    Rank: Member

    Unless you really want to upset your net admin never use Get-ADComputer -Filter * -Properties * in a production environment

  • #102890

    Participant
    Points: 28
    PublishedHelping Hand
    Rank: Member

    8^}
    @SimonB
    8^}

    That was not what my suggestion was, but just highlighting that fact you can get all this without all the calculated property stuff. However, yes, in a large enterprise, with thousands of machines, multiple domains, etc. Sure, very, very bad thing. No different than using Server Manager and adding every server in the domain to the 'All Servers' node. You only add what you need of course, thus you only select what you need for any script / automation effort.

    And @BGomes is already doing...

    '$colResults = $objSearcher.FindAll()'

    ...in the code, so, ostensibly the same thing as...

    Get-ADComputer -Filter *

    … well, without the -Properties * thing. But you only need to do that for one computer to get the properties you want for your final select thingy.

    @BGomes, you don't add all that, you just add what you need. Again all the calculated stuff is a bit overkill IMHO, but as you did with those, you do the same thing for the IPA.

    $computer | Select-Object -Property @{Name='Name';Expression={$_.CN}},@{Name='Owner';Expression={$_.ObjectSecurity.Owner}},@{Name='Created';Expression={$_.whencreated}},@{Name='IPv4Address';Expression={$_.IPv4Address}}
    

    Yet, here's the thing. If your '$computer' object does not have as part of the properties, you can't get it from there. So, you have to ask for it another way. Say, like...

    @{Name='IPv4Address';Expression={(Test-Connection -ComputerName $_.Name -Count 1).IPV4Address.IPAddressToString}}
    

    Thus, that means you are making two network calls vs one. One to the DC for all the ADDS stuff and a second to ping a box to get the IPA. So, this...

    cls
    $DomainName = 'LDAP://CN=Computers,DC=contoso,DC=com'
    $Root = New-Object DirectoryServices.DirectoryEntry $DomainName
    $objSearcher = New-Object DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $Root
    $objSearcher.SearchScope = "SubTree"
    
    $ADResults = @() 
    
    $colResults = $objSearcher.FindAll()
    foreach ($objResult in $colResults)
    {
    
    $computer = $objResult.GetDirectoryEntry()
    #$computer.nTSecurityDescriptor
    
    $computer | Select-Object -Property @{Name='Name';Expression={$_.CN}},
    @{Name='Owner';Expression={$_.ObjectSecurity.Owner}},
    @{Name='Created';Expression={$_.whencreated}},
    @{Name='IPv4Address';Expression={(Test-Connection -ComputerName $_.Name -Count 1).IPV4Address.IPAddressToString}}
    }
    
    # Results
    Name          Owner                    Created              IPv4Address     
    ----          -----                    -------              -----------     
    SVR01         contoso\Domain Admins   4/2/2017 6:42:01 AM   192.168.1.1 
    ... 
    

    Sure, it works, but why take the extra step, then there is this effort also being looked on as bad by your network security admins as potential ping sweep, thus firing off warnings to them, if they are monitoring this sort of activity. So, there's that.

    Again, Get-ADComputer is just far simpler and less work, easier to maintain and follow, long term and no ping sweeps needed.

The topic ‘How can I get the ip address computer from $computers?’ is closed to new replies.