Creating computer objects in user supplied OU in AD

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 1 year, 2 months ago.

  • Author
  • #28273
    Profile photo of D Cohen
    D Cohen

    I am attaching a script so far that I have had a colleague basically help me write... Although he did substantially most of the work. I am very new to this but looking to finish up the end of this script. Any help would be greatly appreciated!

    I'm specifically looking for help with Region 4 and below, but any suggestions to a new scripter would be great!

    ##Mission: Create user supplied computer objects in referenced OU
    #region (1) Create static variables (Set Variables)
    # workstation ou
    # lab
    # server
    # mfg machines ou
    #region (2) Ask the user for input (Get Variables)
    #new computer name
    $newsystem= read-host "Please enter the system name."
    #Site (USA,USD,USJ)
    $site= Read-Host "Please enter the XXXX site code."
    #Region (USA,MY,UK,etc)
    $region=Read-Host "Please enter the region code."
    #Type (Lab,Server,Workstation,Mfg,etc)
    $systemtype=Read-Host "Please enter the system type."
    #region (3) Build OU path based on input from (2) Getting variables
    #Test type of PC and use appropriate base OU variable
    if ($systemtype -eq "lab")
    {$adlocation=("OU=" + $site + ",OU=" + $region + "," + $labou)}
    if ($systemtype -eq "server")
    {$adlocation=("OU=" + $site + ",OU=" + $region + "," + $serverou)}
    if ($systemtype -eq "workstation")
    {$adlocation=("OU=" + $site + ",OU=" + $region + "," + $workstationou)}
    if ($systemtype -eq "mfg")
    {$adlocation=("OU=" + $site + ",OU=" + $region + "," + $mfgou)}

    Write-Host $adlocation

    #create new string from multiple strings (concatenate the variables from step 2 and 1)
    #region (4) Verify computer name
    #Test if the computername is not like sitecode-*
    $learn=$site + "-*"
    if ($newsystem -like $learn)
    } else {

    #prepend site code to computername
    $newsystem=($site + "-" + $newsystem)}
    Write-Host $newsystem

    #Test if there are duplicates in AD
    $duplicate=Get-ADComputer -filter 'name -eq $newsystem'
    if ($duplicate)
    #Create computer name in proper OU
    #Use New-adcomputer
    #end script

  • #28275
    Profile photo of Rob Simmers
    Rob Simmers

    Hey D Cohen, welcome to and Powershell in general.

    First, you should understand that while the idea of read-host seems great, there will be a lot of code just making sure that the user entered what you expected. For instance, let's take the site code. You indicate in your notes that input should be USA, USD or USJ. I enter TGIF. You're just generating the OU and your code is going to fail. Look at doing a loop around each input, something like below so that you check it and re-prompt before going to the next question.

    do {
        $site= Read-Host "Please enter the XXXX site code (USA,USD,USJ)"
        #When you are doing comparisons, you typically want to force it to Upper or Lower
        #to validate.  This is a bad example because it's regex and case sensitvity is off
        #by default, but you typically want to compare USA to USA, not USA to Usa.
        switch -Regex ($Site.ToUpper()) {
            #Regex is Regular Expressions. It typically looks like the computer puked, but
            #below is a simple pattern saying USA OR USD OR USJ
            "USA|USD|USJ"{"Site code confirmed as {0}" -f $_}
                "{0} is not a valid site code." -f $_
                $site = $null
    while (!$site) #Loop until $Site is NOT null

    Basically, do this for each criteria. Try to break it. If you can get through with good data, THEN we can look at region 4. Also, another way to tackle the building of OU's:

    #Again we want to compare lowercase value to lowercase value, so we force ToLower()
    switch ($systemtype.ToLower()) {
        "lab"{$adlocation="OU={0},OU={1},ou=lab,ou=servers,dc=XXXX,dc=global" -f $site, $region}
        "server"{$adlocation="OU={0},OU={1},ou=regional,ou=servers,dc=XXXX,dc=global" -f $site, $region}
        "workstation"{$adlocation="OU={0},OU={1},ou=regional,ou=workstations,dc=XXXX,dc=global" -f $site, $region}
        "mfg"{$adlocation="OU={0},OU={1},ou=mfg,ou=servers,dc=XXXX,dc=global" -f $site, $region}
        default{"{0} is not a valid option" -f $_}

    The string is being built with a string format. The {0} and {1} are placeholders for the respective variables $site and $region. That reduces like 18 lines of code into the above. When you are validating a single variable (e.g. $systemtype) against multiple values, it's almost always better to use a switch statement. So, try to update your code to ensure users can't put wrong information in and post back your progress. These things are almost always better using a GUI interface with dropdowns so that you do not have to check user input, but this is a good starter script for you.

You must be logged in to reply to this topic.