Find what Computers are in Which Site

This topic contains 3 replies, has 4 voices, and was last updated by  Dan Potter 1 year, 3 months ago.

  • Author
    Posts
  • #38315

    Robert King
    Participant

    I've been working the below code and can't seem to get the Sites to populate into the csv...any help would really be appreciated.

    #Import Module
    Import-Module ActiveDirectory
    
    #Set Variables
    $Domain = (Get-ADDomain).distinguishedname
    $OU = "CN=Computers,$Domain"
    $Logfile = "C:\Logs\ComputerSiteAssignments.csv"
    
    # Retrieve Site
    $GetSite = Get-ADComputer -Filter * -SearchBase $OU -Property * | foreach {
        nltest /server:$_.Name /dsgetsite
        }
    
    #Get Computer information and format
    $Computer = @{}
    $Computer = Get-ADComputer -Filter * -SearchBase $OU -Property Name,OperatingSystem,Description,IPv4Address | foreach {
        $HostName = $_.Name
        $OS = $_.OperatingSystem
        $Description = $_.Description
        $IP = $_.IPv4Address
        $Sites = $GetSite
        
       [PSCustomObject]@{
        OU = $OU
        Name = $HostName
        OS = $OS
        Description = $Description
        IP = $IP
        Site = $Sites
        }
    }
    # Export Logfile to CSV for review
    $Computer | Export-Csv $logfile -NoTypeInformation
    
  • #38316

    Anthony Stringer
    Participant

    this may not be the only issue, but try using this command instead

    nltest /server:$($_.Name) /dsgetsite
    
  • #38339

    Rob Simmers
    Participant

    First, I would recommend making your nltest command a function because it's not a native Powershell command the results might not look like you expect and probably need to be parsed. Luckily, for you, someone had already created a function (link in the function). Also, rather than looping through the computers and manually generating an object, try using calculated properties to get the site information. This is not pseudo-code, not tested, but should be pretty close to what you are looking for:

    function Get-ComputerSite($ComputerName){
       #http://www.powershellmagazine.com/2013/04/23/pstip-get-the-ad-site-name-of-a-computer/
    
       $site = nltest /server:$ComputerName /dsgetsite 2>$null
       if($LASTEXITCODE -eq 0){ $site[0] }
    }
    
    
    #Import Module
    Import-Module ActiveDirectory
    
    #Set Variables
    $Domain = (Get-ADDomain).distinguishedname
    $OU = "CN=Computers,$Domain"
    $Logfile = "C:\Logs\ComputerSiteAssignments.csv"
    
    $Computer = Get-ADComputer -Filter * -SearchBase $OU -Property Name,OperatingSystem,Description,IPv4Address | 
    Select Name, 
           OperatingSystem, 
           Description, 
           IPv4Address, 
           @{Name="ADSite";Expression={Get-ComputerSite -ComputerName $_.Name}}
    
    $Computer | Export-Csv $logfile -NoTypeInformation
    
  • #38342

    Dan Potter
    Participant

    It's also in here.

    HKLM:\SYSTEM\CurrentControlSet\services\Netlogon\Parameters

You must be logged in to reply to this topic.