Home > Blockchain >  Assigning fullaccess rights when creating new user-specific Homefolders
Assigning fullaccess rights when creating new user-specific Homefolders

Time:03-07

So I have been trying to figure out how to create user-specific home folders and give the user FullAccess rights to his/her own (new) Homefolder. The location of the Homefolders is always the same, so I want to make it work by just changing the names to the correct usernames. So far I have come up with the following code. I will include the first part which does work, because it might help to understand what I'm aiming at. Also, I'm fairly new at powershell, so apologies for any amateur coding you may find...

###Create a new Homefolder
##Set the user(s) that need a new Homefolder (Use the SamAccountName)
$UserList = "Guest1","Guest2","Guest3"

foreach ($User in $UserList)
{
    ##Set the properties of the folder
    $File = [PSCustomObject]@{
        Name = "$User.test"
        Path = "C:\Users\ItsMe\Documents\Homefolder test\"
        ItemType = "directory"
    }

    ##Create the directory in the specified path
    New-Item -Path $File.Path -Name $File.Name -ItemType $File.ItemType
    
    #Test if the folder is successfully created, before moving on.
    Test-Path "C:\Users\ItsMe\Documents\Homefolder test\Guest1.test"

    #Get the path of the new directory
    $DirPath = $("$File.Path\$File.Name")

    ###Set Acl to assign FullAccess rights
    $NewAcl = Get-Acl -Path "$DirPath"
    ## Set properties
    $identity = "$User"
    $fileSystemRights = "FullControl"
    $type = "Allow"
}

The code goes a little further, but this is where the main error lies. The output gives me the folders Guest1.test, Guest2.test and Guest3.test in the correct location (the ".test" after the SamAccountName is necessary, as it will be replaced by the Company name in the real script). After that however, I get the following error a couple times.

Get-Acl : Cannot find drive. A drive with the name '@{Name=Guest1.test; Path=C' does not exist.

The $DirPath variable does not take the right path of the newly created folders. The Test-Path command confirms that the folder is created before the error. By using Write-Host $DirPath I found that it saved the following value:

Write-Host $DirPath
@{Name=Guest3.test; Path=C:\Users\ItsMe\Documents\Homefolder test\; ItemType=directory}.Path\@{Name=Guest3.test; Path=C:\Users\ItsMe\Documents\Homefolder test\; ItemType=directory}.Name

When I run Get-Acl by manually setting the path (that I want the $DirPath variable to be), after the folders have been created, it works like intended:

Get-Acl -Path "C:\Users\ItsMe\Documents\Homefolder test\Guest1.test"

I have tried to take the whole Acl part out of this "foreach" section and create another "foreach" to assign the FullAccess rights for each user, but so far I have not been able to make that work either (It could be the best way, but I just have not figured it out yet). Any tips on how to make this work will be appreciated. I feel like the current structure might be wrong.

CodePudding user response:

You might try:

$DirPath = $($File.Path\$File.Name)

Also, if you run test-path inside the foreach loop after you create the folder, you’ll know whether it’s now there or not.

CodePudding user response:

Create a separate variable for each value in $File; it’s a hashtable. I actually don’t know that it’s buying you anything based on the use-case. ItemType is unchanging; I’d define it above the loop. I’d build the user’s folder path by concatenating the base to the Username with .test

  • Related