none
Take users out of default users OU

    Question

  • This is my script so far
    #Move computers from the default "Computers" OU to an OU of your choice. 
    [cmdletbinding()]            

    param (
    [parameter(mandatory=$true)]
    #The OU you will put the computers in
    $TargetOU = "OU=test,DC=AD1,DC=BCU,DC=EDU"
    )            

    Import-Module ActiveDirectory
    #Domain you are searching
    $Domain = [ADSI]"ad1.bcu.edu"
    $DN=$domain.distinguishedName
    #The OU you are retrieving the computers
    $SourcePath = "CN=Computers,DC=AD1,DC=BCU,DC=EDU" 
    #A basic filter: If no computers are in the OU it will spit out the messag by the IF statement
    $Computers = Get-ADComputer -Filter * -SearchBase "CN=Computers,DC=AD1,DC=BCU,DC=EDU" 
    if(!$Computers) {
     write-host "No Computers are found in default container"
     return
    }
    #Command, for each computer, move it to xxx OU, repeat process. 
    foreach ($Computer in $Computers) {
     if(!(Move-ADObject $Computer -TargetPath $TargetOU)) {
      $Status = "SUCCESS"
     } else {
      $Status = "FAILED"
     }
     $OutputObj = New-Object -TypeName PSobject
     $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.Name.tostring()
     $OutputObj | Add-Member -MemberType NoteProperty -Name SourcePath -Value $SourcePath
     $OutputObj | Add-Member -MemberType NoteProperty -Name DestinationPath -Value $TargetOU
     $OutputObj | Add-Member -MemberType NoteProperty -Name Status -Value $Status
     $OutputObj
    }

    I would like to make a statement that says if it starts (or has) IT in the name (ex:ITSYS-745) it will automatically put it in the IT OU


    bradley Wyatt

    Monday, April 09, 2012 6:57 PM

Answers

  • Grant's suggestions, plus expand to two targets and code to check beginning to computer name (not tested):

    # Move computers from the default "Computers" OU to an OU of your choice.
    [cmdletbinding()]

    param (
        #The OU you will put the computers in
        $Target1 = "OU=test,DC=AD1,DC=BCU,DC=EDU",
        $Target2 = "ou=IT,dc=AD1,dc=BCU,dc=EDU"
    )

    Import-Module ActiveDirectory
    # Domain you are searching
    # $Domain = [ADSI]"ad1.bcu.edu"
    # $DN=$domain.distinguishedName
    # The OU you are retrieving the computers
    $SourcePath = "CN=Computers,DC=AD1,DC=BCU,DC=EDU"
    # A basic filter: If no computers are in the OU it will spit out the messag by the IF statement
    $Computers = Get-ADComputer -Filter * -SearchBase $SourcePath
    if(!$Computers) {
        write-host "No Computers are found in default container"
        return
    }
    # Command, for each computer, move it to xxx OU, repeat process.
    foreach ($Computer in $Computers) {
        If ($Computer.Name.ToUpper().Startswith("IT") -eq $True) {
            $TargetOU = $Target2
        }
        Else {
            $TargetOU = $Target1
        }
        if(!(Move-ADObject $Computer -TargetPath $TargetOU)) {
            $Status = "SUCCESS"
        }
        else {
            $Status = "FAILED"
        }
        $OutputObj = New-Object -TypeName PSobject
        $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.Name.tostring()
        $OutputObj | Add-Member -MemberType NoteProperty -Name SourcePath -Value $SourcePath
        $OutputObj | Add-Member -MemberType NoteProperty -Name DestinationPath -Value $TargetOU
        $OutputObj | Add-Member -MemberType NoteProperty -Name Status -Value $Status
        $OutputObj
    }

    -----



    Richard Mueller - MVP Directory Services


    Monday, April 09, 2012 10:52 PM
  • Heck, I don't know. None of my 3 PowerShell books describes it, and the Get-Help About_Parameters has no examples. I looked before I posted. I wondered why it prompted when I tested, but I just copied the OP's code. It was only by trial and error that I figured out the parameters needed to be comma delimited. Obviously I never use it.


    Richard Mueller - MVP Directory Services

    Tuesday, April 10, 2012 12:40 AM
  • Heck, I don't know. None of my 3 PowerShell books describes it, and the Get-Help About_Parameters has no examples. I looked before I posted. I wondered why it prompted when I tested, but I just copied the OP's code. It was only by trial and error that I figured out the parameters needed to be comma delimited. Obviously I never use it.


    Richard Mueller - MVP Directory Services

    It got me the first time, too.

    Setting required od mandatory then setting a default would override the mandatory.  Yu can have one or the other but not both.  The SDK does documetn this.


    ¯\_(ツ)_/¯

    Tuesday, April 10, 2012 1:13 AM

All replies

  • Design points:

    1. You are defining 2 variables that you never use: $Domain and $DN.

    2. You already have the Searchbase in the $Sourcepath variable.  It could be -Searchbase $Sourcepath.

    3. To do what you want, you would have to change the code that moves the computer objects.


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)


    • Edited by Bigteddy Monday, April 09, 2012 7:57 PM
    Monday, April 09, 2012 7:56 PM
  • Her eis a little hint on how to structure this.  YOU cre using 'Mandatory' and a default value.  These are mutually exclusive.

    It is also more efficient to use a property hash to generate objects.

    Allowing many parameters to be used in the Param statement with default values makes the function more flexible.  Uisng Write-Verbose allows you to take advantage of CMdLetBinding to control optional information output.


    # ==============================================================================================
    # 
    # Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2011
    # 
    # NAME: 
    #Move computers from the default "Computers" OU to an OU of your choice. 
    [cmdletbinding()]           
    param(
        [parameter(mandatory=$true)]
        $TargetOU,
        $SourcePath='CN=Computers,DC=AD1,DC=BCU,DC=EDU'
    )            
    Try{
        Import-Module ActiveDirectory -ea stop
    }
    Catch{
        throw
    }
    $computers = Get-ADComputer -Filter * -SearchBase $SourcePath
    if(!$computers){
        Write-Host "No Computers are found in default container"
         return
    }
    foreach($computer in $computers){
        if(!(Move-ADObject $computer -TargetPath $TargetOU)) {
            Write-Verbose "Move computer failed:$computer"
            $Status = "SUCCESS"
        }else{
            Write-Verbose "Move computer succeeded:$computer"
            $Status = "FAILED"
        }
        $hash=@{
            ComputerName=$($computer.Name)
            SourcePath=$SourcePath
            DestinationPath=$TargetOU
            Status=$Status
        }
        New-Object -TypeName PSobject -Property $hash
    }


    ¯\_(ツ)_/¯


    • Edited by jrv Monday, April 09, 2012 9:55 PM OOPS!
    Monday, April 09, 2012 9:51 PM
  • Grant's suggestions, plus expand to two targets and code to check beginning to computer name (not tested):

    # Move computers from the default "Computers" OU to an OU of your choice.
    [cmdletbinding()]

    param (
        #The OU you will put the computers in
        $Target1 = "OU=test,DC=AD1,DC=BCU,DC=EDU",
        $Target2 = "ou=IT,dc=AD1,dc=BCU,dc=EDU"
    )

    Import-Module ActiveDirectory
    # Domain you are searching
    # $Domain = [ADSI]"ad1.bcu.edu"
    # $DN=$domain.distinguishedName
    # The OU you are retrieving the computers
    $SourcePath = "CN=Computers,DC=AD1,DC=BCU,DC=EDU"
    # A basic filter: If no computers are in the OU it will spit out the messag by the IF statement
    $Computers = Get-ADComputer -Filter * -SearchBase $SourcePath
    if(!$Computers) {
        write-host "No Computers are found in default container"
        return
    }
    # Command, for each computer, move it to xxx OU, repeat process.
    foreach ($Computer in $Computers) {
        If ($Computer.Name.ToUpper().Startswith("IT") -eq $True) {
            $TargetOU = $Target2
        }
        Else {
            $TargetOU = $Target1
        }
        if(!(Move-ADObject $Computer -TargetPath $TargetOU)) {
            $Status = "SUCCESS"
        }
        else {
            $Status = "FAILED"
        }
        $OutputObj = New-Object -TypeName PSobject
        $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.Name.tostring()
        $OutputObj | Add-Member -MemberType NoteProperty -Name SourcePath -Value $SourcePath
        $OutputObj | Add-Member -MemberType NoteProperty -Name DestinationPath -Value $TargetOU
        $OutputObj | Add-Member -MemberType NoteProperty -Name Status -Value $Status
        $OutputObj
    }

    -----



    Richard Mueller - MVP Directory Services


    Monday, April 09, 2012 10:52 PM
  • Richard - YOu cannot have 'Mandatory' and a default value in the same parameter.  Think about it.


    ¯\_(ツ)_/¯

    Monday, April 09, 2012 11:36 PM
  • Heck, I don't know. None of my 3 PowerShell books describes it, and the Get-Help About_Parameters has no examples. I looked before I posted. I wondered why it prompted when I tested, but I just copied the OP's code. It was only by trial and error that I figured out the parameters needed to be comma delimited. Obviously I never use it.


    Richard Mueller - MVP Directory Services

    Tuesday, April 10, 2012 12:40 AM
  • Heck, I don't know. None of my 3 PowerShell books describes it, and the Get-Help About_Parameters has no examples. I looked before I posted. I wondered why it prompted when I tested, but I just copied the OP's code. It was only by trial and error that I figured out the parameters needed to be comma delimited. Obviously I never use it.


    Richard Mueller - MVP Directory Services

    It got me the first time, too.

    Setting required od mandatory then setting a default would override the mandatory.  Yu can have one or the other but not both.  The SDK does documetn this.


    ¯\_(ツ)_/¯

    Tuesday, April 10, 2012 1:13 AM