locked
Need to MAP home folder on file share server, create folder and set permission RRS feed

  • Question

  • Hi All,

    I have a requirement to map  home folder to users in a CSV file , I have following requirement.

    >> We should create the folder automatically once the home folder is mapped to the user in the file share server.
    >> Only the user in who's name the folder is created and administrator should have permission on that folder.

    Thank you.

    Wednesday, December 25, 2019 7:16 AM

Answers

  • Sorry but we do not fix or rewrite scripts you have found on the Internet.

    I will also note that we do not do what you ask with a script.  It is done by configuring Group Policy.  GP will do aloof this automatically for each new user and can be used to adjust existing users.  The accepted best practice is to use GP.

    The security settings you are trying to use will be automatically set by GP when it creates or redirects the folder.

    Also the code you copied will never work.  It is not even written correctly.  Thisi swhy you need to learn user administration and PowerShell before even trying to do any of these things.

    Please read the links I posted carefully before attempting to post in this forum.  You have not posted the code correctly. 


    \_(ツ)_/



    • Edited by jrv Wednesday, December 25, 2019 8:39 AM
    • Marked as answer by Raghavendran KR Wednesday, December 25, 2019 8:53 AM
    Wednesday, December 25, 2019 8:37 AM

All replies

  • Sorry but we do not so homework problems or write scripts on request.

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


    \_(ツ)_/

    Wednesday, December 25, 2019 7:47 AM
  • Hi,

    My apologies. I managed to find a script which does the job. Following is the code.

    param([Parameter(Mandatory=$true)][String]$samAccountName)
     
    $fullPath = "\\domain.local\public\Home-Folder$\{0}" -f $samAccountName
    $driveLetter = "I:"
     
    $User = Import-Csv -Path "C:\temp\Users1.csv"
     
    if($User -ne $Null) {
        Set-ADUser $User -HomeDrive $driveLetter -HomeDirectory $fullPath
        $homeShare = New-Item -path $fullPath -ItemType Directory -force
     
        $acl = Get-Acl $homeShare
     
        $FileSystemRights = [System.Security.AccessControl.FileSystemRights] = "Modify"
        $AccessControlType = [System.Security.AccessControl.AccessControlType] = "Allow"
        $InheritanceFlags = [System.Security.AccessControl.InheritanceFlags] = "ContainerInherit, ObjectInherit"
        $PropagationFlags = [System.Security.AccessControl.PropagationFlags] = "InheritOnly"
     
        $AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule ($User.SID, $FileSystemRights, $InheritanceFlags, $PropagationFlags, $AccessControlType)
        $acl.AddAccessRule($AccessRule)
     
        Set-Acl -Path $homeShare -AclObject $acl
     
        Write-Host ("HomeDirectory created at {0}" -f $fullPath)
    }

    But when I run the code, I see the following error and its not taking the sAMAccountName mentioned in the CSV file.

    =============

    Set-ADUser : Object reference not set to an instance of an object.
    At C:\Temp\homeshare.ps1:9 char:5
    +     Set-ADUser $User -HomeDrive $driveLetter -HomeDirectory $fullPath
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (Microsoft.Activ...nagement.ADUser:ADUser) [Set-ADUser], NullReferenceExce
       ption
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.NullReferenceException,Microsoft.ActiveDirectory.Management
       .Commands.SetADUser

    New-Object : Cannot find an overload for "FileSystemAccessRule" and the argument count: "5".
    At C:\Temp\homeshare.ps1:19 char:19
    + ... ccessRule = New-Object System.Security.AccessControl.FileSystemAccess ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    Exception calling "AddAccessRule" with "1" argument(s): "Value cannot be null.
    Parameter name: rule"
    At C:\Temp\homeshare.ps1:20 char:5
    +     $acl.AddAccessRule($AccessRule)
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ArgumentNullException

    ====================

    Wednesday, December 25, 2019 8:01 AM
  • Sorry but we do not fix or rewrite scripts you have found on the Internet.

    I will also note that we do not do what you ask with a script.  It is done by configuring Group Policy.  GP will do aloof this automatically for each new user and can be used to adjust existing users.  The accepted best practice is to use GP.

    The security settings you are trying to use will be automatically set by GP when it creates or redirects the folder.

    Also the code you copied will never work.  It is not even written correctly.  Thisi swhy you need to learn user administration and PowerShell before even trying to do any of these things.

    Please read the links I posted carefully before attempting to post in this forum.  You have not posted the code correctly. 


    \_(ツ)_/



    • Edited by jrv Wednesday, December 25, 2019 8:39 AM
    • Marked as answer by Raghavendran KR Wednesday, December 25, 2019 8:53 AM
    Wednesday, December 25, 2019 8:37 AM