none
Function to return distinguished name

    Question

  • Hello, 

    I've written a function to get the distinguished name of an OU which works fine on it's own, just printing the DN:

    function Get-DN{ 
        Param(
        [Parameter(Mandatory=$True,Position=1)]
        [string]$OrganisationalUnit
        )
        $DN = Get-ADOrganizationalUnit -Filter 'Name -like $OrganisationalUnit'
        Write-Host -ForegroundColor DarkGreen -BackgroundColor Gray "$($DN.DistinguishedName)"
        }

    However when I try to use it inside my script, it doesn't populate the variable:

    Import-CSV $newpath | ForEach-Object { 
        $location = (Get-DN $_.Department).DistinguishedName   
        Write-host $location
        }
    

    I've tried setting $location =  $DN.DistinguishedName but I'm guessing the function doesn't expose this?

    I've got the script working by using Get-ADOrganizationalUnit directly but I'm just curious as to how to use a function in this way in the future, any advice appreciated

    Thanks




    Wednesday, October 16, 2013 11:38 AM

Answers

  • Your Get-DN function doesn't return any output it just writes something to the screen. If you change it to return the object it should work as expected:

    function Get-DN{ 
        Param(
        [Parameter(Mandatory=$True,Position=1)]
        [string]$OrganisationalUnit
        )
        $DN = Get-ADOrganizationalUnit -Filter 'Name -like $OrganisationalUnit'
        Write-Host -ForegroundColor DarkGreen -BackgroundColor Gray "$($DN.DistinguishedName)"
        $DN
    }
    
    Import-CSV $newpath | ForEach-Object { 
        $location = (Get-DN $_.Department).DistinguishedName   
        Write-host $location
        }

    Wednesday, October 16, 2013 12:04 PM

All replies

  • Your Get-DN function doesn't return any output it just writes something to the screen. If you change it to return the object it should work as expected:

    function Get-DN{ 
        Param(
        [Parameter(Mandatory=$True,Position=1)]
        [string]$OrganisationalUnit
        )
        $DN = Get-ADOrganizationalUnit -Filter 'Name -like $OrganisationalUnit'
        Write-Host -ForegroundColor DarkGreen -BackgroundColor Gray "$($DN.DistinguishedName)"
        $DN
    }
    
    Import-CSV $newpath | ForEach-Object { 
        $location = (Get-DN $_.Department).DistinguishedName   
        Write-host $location
        }

    Wednesday, October 16, 2013 12:04 PM
  • Hello Benny,

    shouldn't the first positional parameter be "Position=0" for the first parameter?

    Calling the function you could specify the parameter to bind the input to with -OrganisationalUnit. The position parameter should make that not strictly necessary, but it's mroe for being lazy when typing in the Shell. In a Script, adding it won't be too much effort and make the command slightly easier to read (and avoid some accidental errors and timeloss caused by fixing those, if you make a practice of it).

    Consider adding [CmdletBinding()]
    As a line before the Param( declaration.

    When writing an explicit return $DN as the final line in a function, you can guarantee the function will return the parameter. It's not strictly necessary, but a good practice I think, to cut down on some errors and make it easier to read what will be returned, when you have to return to your code some months later.

    These may or may not solve your problem, consider dumping $error into a logfile, in case an error occurs.

    On another note: the OrganizationalUnit Class exposes the DN, never doubt that. Your PowerShell or .NET is stumbling over something while running as script, cleaning up the code and making sure you're up to date should go a long way top troubleshooting this.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Wednesday, October 16, 2013 12:18 PM