none
homefolder automation scripting RRS feed

  • General discussion

  • I and my team is working on an automation of UserHomeFolder creation which would be automated. I have attached a script but I am stuck at a point where UID matches the “Integer Range” folders. I have attached the script as an attachment.

     Scenario:

    1. A user joins company> HR requests ID & Access creation.
    2. Software is used to create AD user. There can only be 3 Types of users:
      1. Full-Time employee has 5 digit Employee ID which is UID of the ADUsers
      2. Vendor ID is Alpha-Numeric and always starts with XD (eg. XD919)
      3. There is a test user (Forget about it).
    3. Once the AD Object is created, every night our Scripting Server will run this script as a scheduled task and create a user’s home folder under following Folder Structure:
      1. Folder Name 1: 00001to01500
      2. Folder Name 2: 01501to03000
      3. Folder Name 3: 03001to04500
    4. The goal is that the script should be able to match the UID and create a UserFolder under above range i.e. a; b or c as mentioned above. Example: if the UID is say, 04002, that user’s folder should be created under folder name “03001to04500”.
    5. Once the folder is in place, when the user logs in, the group policy preference does the rest.

    Please help!

    ***************Script below***********************

    ### Global Variables
    [string]$alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}
    $When = ((Get-Date).AddDays(-3))
        # Rights
    $rights = [System.Security.AccessControl.FileSystemRights]"ExecuteFile, AppendData, CreateFiles, Delete, DeleteSubdirectoriesAndFiles, ReadAttributes, ReadData, ReadExtendedAttributes, ReadPermissions, WriteAttributes, WriteExtendedAttributes"
        # Inheritance
    $inheritanceFlag = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
        # Propagation
    $propagationFlag = [System.Security.AccessControl.PropagationFlags]::None
        # Type
    $type = [System.Security.AccessControl.AccessControlType]::Allow

    #### Gets all the users created in the last 3 days and stores just the name in $NewUsers####
    $NewUsers = Get-ADUser -Filter {whenCreated -ge $When} | select -Property Name


    #### Takes the Name Property of the users in $newusers and checks to see if the name contains any letters.
    #### If the name doesn't have letters, it converts it to an INT, to be compared against a number range
    #### that matches to the folder structure where the user folder needs to be created

    ForEach ($UID in $NewUsers)
    {
        If($UID.name -notcontains $alphabet) {
        [convert]::ToInt32($UID)

            Try {
            IF(($_ -ge 1) -and ($_ -le 1500)) {
                mkdir -path "\\pvansga303es001\PW-SNV-User\00001to01500\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\00001to01500\$($_)"
                    }
            else-if (($_ -ge 1501) -and ($_ -le 3000)) {
                mkdir -path "\\pvansga303es001\PW-SNV-User\01501to03000\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\01501to03000\$($_)"
                    }
            else-if (($_ -ge 3001) -and ($_ -le 4500)) {
                mkdir -path "\\pvansga303es001\PW-SNV-User\03001to04500\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\03001to04500\$($_)"
                    }
            else-if (($_ -ge 4501) -and ($_ -le 6000)) {
                mkdir -path "\\pvansga303es001\PW-SNV-User\04501to06000\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\04501to06000\$($_)"
                    }
            else-if (($_ -ge 6001) -and ($_ -le 7500)) {
                mkdir -path "\\pvansga303es001\PW-SNV-User\06001to07500\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\06001to07500\$($_)"
                    }
            else-if (($_ -ge 7501) -and ($_ -le 9000)) {
                mkdir -path "\\pvansga303es001\PW-SNV-User\07501to09000\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\07501to09000\$($_)"
                    }
            else-if (($_ -ge 9001) -and ($_ -le 15000)) {
                mkdir -path "\\pvansga303es001\PW-SNV-User\09001to15000\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\09001to15000\$($_)"
                    }
            else-if (($_ -ge 15001) -and ($_ -le 25000)) {
                mkdir -path "\\pvansga303es001\PW-SNV-User\15001to25000\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\15001to25000\$($_)"
                    }
            else-if (($_ -ge 25001) -and ($_ -le 35000)) {
                mkdir -path "\\pvansga303es001\PW-SNV-User\25001to35000\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\25001to35000\$($_)"
                    }
            else-if (($_ -ge 35001) -and ($_ -le 45001)) {
                mkdir -path "\\pvansga303es001\PW-SNV-User\35001to45000\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\35001to45000\$($_)"
                    }
            else-if (($_ -ge 45001) -and ($_ -le 55000)) {
                mkdir -path "\\pvansga303es001\PW-SNV-User\45001to55000\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\45001to55000\$($_)"
                    }
            else-if (($_ -ge 55001) -and ($_ -le 65000)) {
                mkdir -path "\\pvansga303es001\PW-SNV-User\55001to65000\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\55001to65000\$($_)"
                    }
            else-if (($_ -ge 65001) -and ($_ -le 75000)) {
                mkdir -path "\\pvansga303es001\PW-SNV-User\65001to75000\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\65001to75000\$($_)"
                    }
            else-if (($_ -ge 75001) -and ($_ -le 85000)) {
                mkdir -path "\\pvansga303es001\PW-SNV-User\75001to85000\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\75001to85000\$($_)"
                    }
            else-if (($_ -ge 85001) -and ($_ -le 100000)) {
                mkdir -path "\\pvansga303es001\PW-SNV-User\85001to100000\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\85001to100000\$($_)"
                    }
            }
            Catch {"User Name contain letters, Continuing"}
           }
        If($UID.name -contains $alphabet) {           
            Try  {
                IF ($_ -contains "X"){
                mkdir -path "\\pvansga303es001\PW-SNV-User\Contractors\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\Contractors\$($_)"
                    }
                IF ($_ -contains "test") {
                mkdir -path "\\pvansga303es001\PW-SNV-User\TestAccounts\$($_)"
                $path = "\\pvansga303es001\PW-SNV-User\TestAccounts\$($_)"
                    }
                  }
            Catch{"Name doesn't match formating of this script, please check username"| Out-File -FilePath C:\logs\usercreation.log -Append}

                }
        $Acl = Get-Acl -Path $Path
        $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule @($UID,$rights,$inheritanceFlag, $propagationFlag, $type)
        $Acl.SetAccessRule($Ar)

        Set-Acl -AclObject $Acl -Path $Path
    }

    Monday, June 10, 2019 8:41 PM

All replies

  • Home folder creation is already fully automated using Group Policy.  There is no need to do this with a script.


    \_(ツ)_/

    Monday, June 10, 2019 9:32 PM
  • Home folder creation is already fully automated using Group Policy.  There is no need to do this with a script.


    \_(ツ)_/


    Did you even read the scenario? Please go through the scenario mentioned in initial thread before answering.

    V-2ARCH3R Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights.

    Tuesday, June 11, 2019 12:30 PM
  • Home folder creation is already fully automated using Group Policy.  There is no need to do this with a script.


    \_(ツ)_/


    Did you even read the scenario? Please go through the scenario mentioned in initial thread before answering.

    V-2ARCH3R Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights.

    All of this can be done with GP more easily and will occur immediately when a user is created or will update the folders if they have not been created.


    \_(ツ)_/

    Tuesday, June 11, 2019 2:58 PM