none
Adding a Paramater IF Switch is True RRS feed

  • Question

  • Im working on creating a simple script to call out the Get-ADobject cmdlet and use the -Server parameter, only if a switch is used.

    For Example:

    Get-ADObject -Filter { ObjectClass -eq 'organizationalunit' } -Server $NewDomain | 
    Where-Object { $_.Name -like "*$OU*" } | 
    Select-Object -Property DistinguishedName,Name | 
    sort DistinguishedName
    

    Dont want to use the -Server Parameter at all times. Since my cached domain is my main domain, just sometimes would be nice to return the results from the New Domain too.

    I made it work by using an If statement, but that involves using this cmdlet twice in the script, and it seems inefficient that way (Currently, my script has two "IF" statements.). My goal is, if I use the switch $NewDomain, the results to return a single time, only for the new domain and not include the original domain $OU. 

    Sorry if Im making this more difficult that it really is.

    Tuesday, February 13, 2018 12:34 AM

Answers

  • I know what splatting is, its for a "collection" of parameters, I only have one (servers).  Or if I was using this command outside a function, then yes, I can see where that makes sense. 

    Actually, this worked for me. Simple.

     if ($NewDomain)
        {
          Get-ADOrganizationalUnit -Filter "Name -like '*$OU*'" -Server MyNewDomainDC | Select-Object -Property DistinguishedName,Name | sort DistinguishedName
        }
        
        else
        {
         Get-ADOrganizationalUnit -Filter "Name -like '*$OU*'" | Select-Object -Property DistinguishedName,Name | sort DistinguishedName   
        
        }

    • Proposed as answer by Ben Lavender Wednesday, February 14, 2018 3:19 PM
    • Marked as answer by prisoner107 Wednesday, February 14, 2018 7:52 PM
    Wednesday, February 14, 2018 12:58 PM

All replies

  • Why is it a problem.  Also you would do better by first learning PowerShell and AD.

    Get-ADOrganizationalUnit'-Filter 'Name like '*$OU*'" -Server$NewDomain


    \_(ツ)_/

    Tuesday, February 13, 2018 1:04 AM
    Moderator
  • Thanks for your suggestion to shorten the cmdlet, unfortunately it doesn't answer my question.  I'll post my script so you can see what Im trying to do, that might make more sense.  I will go back and fix the filter, but for now, im trying to figure out how to only use the -Server parameter when I need it and return single domain results when I call it out.

    function Get-OUNames
    <#
    .Synopsis
       Pulls only OU Names from Current AD
    .DESCRIPTION
       Long description
    .EXAMPLE
       Get-OUNames
    .EXAMPLE
       Another example of how to use this cmdlet
    #>
    {
        [CmdletBinding()]
        [Alias()]
        [OutputType([int])]
        Param
        (
            # Param1 help description
            [Parameter(Mandatory=$true,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
            $OU,
            [switch]$NewDomain 
        )
       
        if ($NewDomain)
        {
          Get-ADObject -Filter { ObjectClass -eq 'organizationalunit' } -Server NewDomainServer| Where-Object { $_.Name -like "*$OU*" } | Select-Object -Property DistinguishedName,Name | sort DistinguishedName
        }
        
        if ($OU)
        {
         Get-ADObject -Filter { ObjectClass -eq 'organizationalunit' }  | Where-Object { $_.Name -like "*$OU*" } | Select-Object -Property DistinguishedName,Name | sort DistinguishedName   
        
        }
      
    }
    

    Tuesday, February 13, 2018 1:15 AM
  • Use a splat.

    help about_splatting


    \_(ツ)_/

    Tuesday, February 13, 2018 1:33 AM
    Moderator
  • I know what splatting is, its for a "collection" of parameters, I only have one (servers).  Or if I was using this command outside a function, then yes, I can see where that makes sense. 

    Actually, this worked for me. Simple.

     if ($NewDomain)
        {
          Get-ADOrganizationalUnit -Filter "Name -like '*$OU*'" -Server MyNewDomainDC | Select-Object -Property DistinguishedName,Name | sort DistinguishedName
        }
        
        else
        {
         Get-ADOrganizationalUnit -Filter "Name -like '*$OU*'" | Select-Object -Property DistinguishedName,Name | sort DistinguishedName   
        
        }

    • Proposed as answer by Ben Lavender Wednesday, February 14, 2018 3:19 PM
    • Marked as answer by prisoner107 Wednesday, February 14, 2018 7:52 PM
    Wednesday, February 14, 2018 12:58 PM