Jump to content

Create a folder with users name and set permission for them and line manager...


Recommended Posts

Posted

So we are trying to create a folder structure with the users account name as the folder name and also give them and their line manager permissions on the folder, first part has been sorted I have a script that creates the folder it pulls the list of users from a txt file and assigns them full control the stumbling block is how I give their account manager access as well, I can get a CSV with the username and their account manager in the next row if that helps....

 

Current script:

$Users = Get-Content "C:\data\names.txt"
ForEach ($user in $users)
{
$newPath = Join-Path "C:\data\Users$" -childpath $user
New-Item $newPath -type directory
$acl = Get-Acl $newpath
$permission = "DOMAIN\$user","FullControl","Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
$acl.SetAccessRule($accessRule)
$acl | Set-Acl $newpath
}

Posted

Use a .csv and the Import-Csv cmdlet in powershell.

You can use headings in your csv file then refer to them like so:

 

$Users = Import-Csv -Path C:\users.csv
ForEach ($User in $Users) {
   # You refer to the headings like so: $User.Heading
   New-Item -Path "C:\data\Users\$($User.Username)"
   #etc
}

Posted

Exactly, something like:

 

<#
   CSV format would be like:

   Username,LineManager
   username,linemanagersusername
   username,linemanagersusername
   username,linemanagersusername
   username,linemanagersusername

   etc..
#>
$Users = Import-Csv -Path C:\users.csv

ForEach ($User in $Users) {
   $newPath = Join-Path "C:\data\Users$" -childpath $User.Username
   New-Item $newPath -type directory
   $acl = Get-Acl $newpath
   $permission = "DOMAIN\$($User.Username)","FullControl","Allow"
   $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
   $acl.SetAccessRule($accessRule)
   $permission = "DOMAIN\$($User.LineManager)","FullControl","Allow"
   $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
   $acl.SetAccessRule($accessRule)
   
   $acl | Set-Acl $newpath
}

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...