Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Find Computer OU Cross Domain and export to csv file

$
0
0

Hello,

I have a list with server names (servers.txt)  with this list I am trying to find the related ou the servers are in.
In total the servers are possible located in 15 trusted domains. (i have a list with the trusted domains)

At the end I d like this all in a CVS file.

I have found several scripts but none of them seem to help me :(

of course it has to do with my basic knowledge about scripting and PS1.

I was trying to do it with the following:

 $computerlist = @(Get-Content C:\temp\servers.txt) 
$computerlist | C:\temp\GetComputerOU.ps1 | Export-CSV c:\temp\oulijst.csv

getcomputerou.ps1 refers to this script: (found on Scripting forum 

<#
.SYNOPSIS
This script will find the OU of the current computer or a specified computer or computers.

.DESCRIPTION
This script will use the System.DirectoryServices functionality to retrieve the OU for
the current computer or a list of computers. This will return a list of objects with
a Name and OU property. If the ValueOnly switch is used, then only the OU of the objects
is returned.

.PARAMETER ComputerName
The name(s) of the computer or computers to retrieve the OUs for. This can be specified as a string
or array. Value from the pipeline is accepted.

.PARAMETER ThisComputer
This parameter does not need to be specified. It is used to differentiate between parameter sets.

.PARAMETER ValueOnly
This switch parameter specifies that only the OU of the computer(s) should be returned.

.EXAMPLE
.\GetComputerOU.ps1 -ValueOnly

This will return the OU of the current computer.

.EXAMPLE
$ComputerList = @("Computer1", "Computer2", "Computer3")
$ComputerList | .\GetComputerOU.ps1

This will return an array of object (Name and OU) of the three computer names specified.
#>
[CmdletBinding()]
#requires -version 3
param(
    [parameter(ParameterSetName = "Computername", Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
    $ComputerName ="" 

  #  [parameter(ParameterSetName = "ThisComputer")]
   # [switch]$ThisComputer,
 #  [switch]$ValueOnly
)

begin
{
    $rootDse = New-Object System.DirectoryServices.DirectoryEntry("LDAP://RootDSE")
    $Domain = $rootDse.DefaultNamingContext
    $root = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$Domain")
}

process
{
    if ($PSCmdlet.ParameterSetName -ne "ComputerName")
    {
        $ComputerName = $env:COMPUTERNAME
    }

    $searcher = New-Object System.DirectoryServices.DirectorySearcher($root)
    $searcher.Filter = "(&(objectClass=computer)(name=$ComputerName))"
    [System.DirectoryServices.SearchResult]$result = $searcher.FindOne()
    if (!$?)
    {
        return
    }
    $dn = $result.Properties["distinguishedName"]
    $ouResult = $dn.Substring($ComputerName.Length + 4)
    if ($ValueOnly)
    {
        $ouResult
    } else {
        New-Object PSObject -Property @{"Name" = $ComputerName; "OU" = $ouResult}
    }
}

But When I run this, i am getting errors like this one

 

You cannot call a method on a null-valued expression.
At C:\temp\GetComputerOU.ps1:65 char:5
+     $ouResult = $dn.Substring($ComputerName.Length + 4)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

So I am stuck here and hope for some help...

Resume:

The script needs to find the ou for a Servername (in a txt file) in de AD and return its FQDN 
there are 15 trusted domains to search for
These results need to be exported to a Csv File.

thx in advance



Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>