Set Home directory from CSV file

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Aapeli Hietikko Aapeli Hietikko 7 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #35616
    Profile photo of Nicolai Magnussen
    Nicolai Magnussen
    Participant

    Hi guys,

    I have been struggling With this task for some time now.
    I don't understand how the CSV and foreach are working..

    Here is the script

    As you can see I'am importing a CSV file.

    The csv file have a first column named Samaccountname
    And underneath that in Excel the usernames are pasted in.

    Can somebody help me With this?

    #35617
    Profile photo of Nicolai Magnussen
    Nicolai Magnussen
    Participant

    #35632

    well it looks like you have a couple of issues, first reading the $path comment
    +$("$") this section portion looks wrong, you are trying to call a variable $
    try $path="\\file02\"+$($_.samAccountName)+"$"
    that should result in a path of \\file01\samaccountname$
    (thats what it appears you are trying to set based upon my reading)

    Secondly, per the TechNet for set-aduser for HomeDrive:
    HomeDrive
    Specifies a drive that is associated with the UNC path defined by the HomeDirectory property. The drive letter is specified as ":" where indicates the letter of the drive to associate. The must be a single, uppercase letter and the colon is required. This parameter sets the HomeDrive property of the user object. The LDAP Display Name (ldapDisplayName) for this property is "homeDrive".

    you need to change -HomeDrive $path to not use the Path variable, you need to provide a drive letter here

    #35636
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    This works. But I agree with David — HomeDrive is supposed to be a drive letter.

    # http://powershell.org/forums/topic/set-home-directory-from-csv-file/
    Import-Csv -Path C:\Ephemeral\Import_AD_Users_TBO.csv | foreach {
        $san = $PSItem.SamAccountName
        $path = "\\file02\${san}$"
        Write-Verbose "HomeDirectory for $san is $path"
        Set-ADUser $san -HomeDrive $path -HomeDirectory $path
    }
    

    It really should be something like below. Read the help file for Set-ADUser.

    Set-ADUser $san -HomeDrive "H:" -HomeDirectory $path
    

    And by the way, you really should be exploring Group Policy Preferences for this setting in the future.

    #35637
    Profile photo of Nicolai Magnussen
    Nicolai Magnussen
    Participant

    Hi.
    Thanks for answering my question.
    I'm going to test it in my LAB environment.
    I'am fully aware of GPO.
    But we are moving all Our files from fileserver to another fileserver
    And we are not going to do it all at once.
    And therefore we are creating an Excel file instead of using GPO.

    #35665
    Profile photo of Nicolai Magnussen
    Nicolai Magnussen
    Participant

    This worked perfectly Bob,
    I see how you use $PSitem.SamaccountName
    Is that the same as using $_.samAccountName when reffering to a csv column.
    I like how simple Your code seem's to be
    Do you have a blog or some forum content online?

    Best regard
    Nicolai Magnussen

    #35712
    Profile photo of Aapeli Hietikko
    Aapeli Hietikko
    Participant

    Nicolai, you are correct they are the same thing. PSv3 introduced alias psitem for the mystic underscore.

    $_
    Same as $PSItem. Contains the current object in the pipeline object.
    You can use this variable in commands that perform an action on every
    object or on selected objects in a pipeline.

    There is bunch of other automatic variables which you can look up with command. Lot of cool stuff there!

    Get-Help about_Automatic_Variables -ShowWindow
    
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.