none
Add line to HomeDirectory RRS feed

  • Question

  • Hello,

    I tryed to add HomeDirectory variable to this script but it's not work normaly... I have low level in PowerShell and i try to understand...

    Everyone can help me ?

    Thanks

    # Add the Active Directory bits and not complain if they're already there
    Import-Module ActiveDirectory -ErrorAction SilentlyContinue
    
    # set default password
    # change pass@word1 to whatever you want the account passwords to be
    $defpassword = (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -force)
    
    # Get domain DNS suffix
    $dnsroot = '@' + (Get-ADDomain).dnsroot
    
    # Import the file with the users. You can change the filename to reflect your file
    $users = Import-Csv .\users.csv
    
    foreach ($user in $users) {
            if ($user.manager -eq "") # In case it's a service account or a boss
                {
                    try {
                        New-ADUser -SamAccountName $user.SamAccountName -Name ($user.FirstName + " " + $user.LastName) `
                        -DisplayName ($user.FirstName + " " + $user.LastName) -GivenName $user.FirstName -Surname $user.LastName `
                        -EmailAddress ($user.SamAccountName + $dnsroot) -UserPrincipalName ($user.SamAccountName + $dnsroot) `
                        -Title $user.title -Enabled $true -ChangePasswordAtLogon $true -PasswordNeverExpires  $false `
                        –HomeDrive ‘Z:’ –HomeDirectory \\CONTROLER\Base\$newuser.name `
                        -AccountPassword $defpassword -PassThru `
                        }
                    catch [System.Object]
                        {
                            Write-Output "Could not create user $($user.SamAccountName), $_"
                        }
                }
                else
                 {
                    try {
                        New-ADUser -SamAccountName $user.SamAccountName -Name ($user.FirstName + " " + $user.LastName) `
                        -DisplayName ($user.FirstName + " " + $user.LastName) -GivenName $user.FirstName -Surname $user.LastName `
                        -EmailAddress ($user.SamAccountName + $dnsroot) -UserPrincipalName ($user.SamAccountName + $dnsroot) `
                        -Title $user.title -manager $user.manager `
                        -Enabled $true -ChangePasswordAtLogon $true -PasswordNeverExpires  $false `
                        –HomeDrive ‘Z:’ –HomeDirectory \\CONTROLER\Base\$newuser.name `
                        -AccountPassword $defpassword -PassThru `
                        }
                    catch [System.Object]
                        {
                            Write-Output "Could not create user $($user.SamAccountName), $_"
                        }
                 }
    
            # Put picture part here.
            $filename = "$($user.SamAccountName).jpg"
            Write-Output $filename
    
            if (test-path -path $filename)
                {
                    Write-Output "Found picture for $($user.SamAccountName)"
    
                    $photo = [byte[]](Get-Content $filename -Encoding byte)
                    Set-ADUser $($user.SamAccountName) -Replace @{thumbnailPhoto=$photo} 
                }
       }
    

    • Moved by Bill_Stewart Friday, March 15, 2019 7:08 PM This is not "teach me system admin basics" forum
    Tuesday, February 5, 2019 7:27 PM

All replies

  • Sorry but we do not customize script found on the Internet. 

    Please contact a person in your organization to help you or contact a consultant.

    Please carefully review the following links to set your expectation for posting in  technical forums.

    This Forum is for Scripting   Questions Rather than script requests
     
      Please review the following links:

    Script Gallery.

    Forum for Script requests

          Microsoft Virtual   Academy - Getting Started with Microsoft PowerShell

          PowerShell Documentation

          PowerShell Style Guidelines

    How to ask   questions in a technical forum

    Rubber duck problem   solving

    How to write   a bad forum post

    Help Vampires: A   Spotter's Guide

    This forum is   for scripting questions rather than script requests


    \_(ツ)_/


    • Edited by jrv Tuesday, February 5, 2019 8:12 PM
    Tuesday, February 5, 2019 8:11 PM
  • First remove the extra line continuation character from this line:

     -PassThru ` <---

     -PassThru  <--- like this.




    \_(ツ)_/

    Tuesday, February 5, 2019 8:15 PM
  • Here is a better version of the code.

    $defpassword = (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -force) $dnsroot = '@' + (Get-ADDomain).dnsroot $users = Import-Csv .\users.csv foreach ($user in $users) {

    $fullname = $user.FirstName + ' ' + $user.LastName $splat = @{ SamAccountName = $user.SamAccountName Name = $fullname
    DisplayName = $fullname GivenName = $user.FirstName Surname = $user.LastName EmailAddress = $user.SamAccountName + $dnsroot UserPrincipalName = $user.SamAccountName + $dnsroot Title = $user.title Enabled = $true ChangePasswordAtLogon = $true PasswordNeverExpires = $false HomeDrive = 'Z:' HomeDirectory = \\CONTROLER\Base\$fullname AccountPassword = $defpassword } if ($user.manager){ $splat['Manager'] = $user.manager} try { $newuser = New-ADUser @splat -PassThru -ErrorAction Stop $filename = "$($newuser.SamAccountName).jpg" if (test-path -path $filename) { Write-Host "Found picture for $($user.SamAccountName)" [byte[]]$photo = Get-Content $filename -Encoding byte -ErrorAction Stop Set-ADUser $newuser.SamAccountName -Replace @{ thumbnailPhoto = $photo } -ErrorAction Stop } } catch{ Throw } }



    \_(ツ)_/




    • Edited by jrv Tuesday, February 5, 2019 11:16 PM
    Tuesday, February 5, 2019 8:29 PM
  • Sorry, i'm student and it's virtual AD environnement. This is not planned in the school program but I like to go further...

    At school, the practical work is on the batch cmd, I have already managed a .bat script that works. I would now like to do powershell because it is a tool that I would bring to use in business, unlike the cmd ... (for the system administration at least). So I can not ask for help from a teacher as it comes out of the curriculum.

    I tried this new version but the problem remains the same. in the properties of the user, in the tab "profil", the path of the HomeDirectory is thus: \\CONTROLER\Base\

    username not appear 

    Thank a lot for reply

    Tuesday, February 5, 2019 10:38 PM
  • No idea what you are trying to ask.  You will need to state what you need in a clear  way.  You also need to post the exact error message.


    \_(ツ)_/

    Tuesday, February 5, 2019 11:12 PM
  • Download the code again.  I fixed a couple of errors.


    \_(ツ)_/

    Tuesday, February 5, 2019 11:17 PM
  • Hello,

    no error code, result look like this :


    PS C:\temp> C:\temp\script v2.ps1

    PS C:\temp> 

    https://www.dropbox.com/s/fbquorj6v01gxcr/1.jpg?dl=0

    https://www.dropbox.com/s/zf534mx454qsyjt/2.jpg?dl=0

    csv file look like this :

    FirstName,LastName,SamAccountName,Title,Manager,OU
    Alan,Niven,alan,,,
    William,Bailey,william,,alan,


    • Edited by Geoffrey18 Wednesday, February 6, 2019 9:17 AM
    Wednesday, February 6, 2019 9:14 AM
  • You are asking to map "Z" to a share but you are showing a drive that already exists that is "E" and has a folder called "base"

    You should not use home folder in modern Windows.  Just use Group Policy to redirect the Documents folder.  That is how we have done this since Windows 2000.  The method you are trying to use is old NT4 method and it is inflexible.

    If you do not understand hoe this works then you will have to learn.  The code is doing what it is written to do.


    \_(ツ)_/

    Wednesday, February 6, 2019 9:24 AM
  • ah ok, sorry for my mediocrity
    Wednesday, February 6, 2019 11:47 AM
  • Not a criticism but a fact. You really need to understand how the homedirectory is used and what it actually does.  When you do this in the GUI a whole different set of components are called into play.  When done in script it just adds the folder path to the account.

    If you expect to see something when the account logs in you won't unless the path specified actually exists and the account has permissions on the path.

    Your pictures don't help us to understand what you are saying is wrong.  They are just pictures showing that the AD settings are as specified in the script.  The explorer view shows that there is no folder added because no folder exists on that share that is accessible by the user.


    \_(ツ)_/

    Wednesday, February 6, 2019 3:58 PM