Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm trying to build a script that will create a folder for a new user on our file server. Then take the inherited rights away from that folder and add specific rights back in. I have it successfully adding the folder (if i give it a static entry in the script), giving domain admin rights, removing inheritance, etc...but i'm having trouble getting it to use a variable I set as the user. I don't want there to be a static user each time, I want to be able to run this script, have it ask me for a username, it then goes out and creates the folder, then gives that same user full rights to that folder based on the username i've supplied it. I can use Smithd as a user, like this:

New-Item \\fileserver\home$\Smithd –Type Directory

But can't get it to reference the user like this:

New-Item \\fileserver\home$\$username –Type Directory

Here's what i have:

Creating a new folder and setting NTFS permissions.

$username = read-host -prompt "Enter User Name"

New-Item \\\fileserver\home$\$username –Type Directory

Get-Acl \\\fileserver\home$\$username  

$acl = Get-Acl \\\fileserver\home$\$username

$acl.SetAccessRuleProtection($True, $False)

$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Administrators","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)

$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Domain\Domain Admins","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)

$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Domain\"+$username,"FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)

Set-Acl \\\fileserver\home$\$username $acl

I've tried several ways to get it to work, but no luck. Any ideas or suggestions would be welcome, thanks.

share|improve this question
    
I get no errors trying to create directories with the first two lines of your script as they appear in the question. From reading your question, it appears that that's where you're getting the error. Can you please update your question with the exact error that you're receiving? –  MDMarra Nov 16 '11 at 1:56
    
what error do you receive? You might try breaking it up like New-Item -Path \\fileserver\home$ -Name $username -Type Directory. Don't know if that will work. –  Shawn Melton Nov 16 '11 at 16:36
    
I swear that second line didn't work for me before, but it works fine now, pulling the username variable in just fine. :/ I obviously did something wrong before or am getting mixed up while testing. Stepping back through it again and I'll update the post with with specific errors and details as to where it's hanging up. Thank you all for your input. –  Don Nov 22 '11 at 22:38
    
Well I'm not sure what was going wrong the other day, but doing this today seems to work just fine with the script above, except I've changed "Domain\"+$username back to what I originally had, which was "Domain\$username". I can only assume I fat-fingered something. Thank you all for your help on this! This is one part of a script that I'm working on, the other part still has a slight catch importing the active directory module, going to see if it works itself out too. :) Thanks! –  Don Nov 23 '11 at 16:16

2 Answers 2

Check out these articles:

http://powershell.com/cs/blogs/tobias/archive/2011/09/21/applying-ntfs-permissions.aspx

and

http://consulting.risualblogs.com/blog/2011/03/25/powershell-to-assign-permissions-to-home-directories/

These would be helpful..

share|improve this answer
    
No trouble with voted down but somebody who votes down is really expecting me to write the whole script here I guess. That somebody can mix the resources above and write the script and make it run, as I did and tested of course before posting here... –  The_aLiEn Nov 16 '11 at 1:37
1  
Just linking is discouraged. This is a Q&A site, not to be mixed up with a Q&link to site site. What happens a year from now when those links are broken? Need more meat and potatoes in your answer and back it up with links. :) –  Nixphoe Nov 16 '11 at 1:42
    
Also, the OP is having a problem creating the folder, not setting the permissions. Try actually reading more than the title. Sometimes they're misleading. –  MDMarra Nov 16 '11 at 1:44

At my previous job, I recall being able to use the old 'mkdir' command to create folders in my powershell scripts, but to start I would suggest quoting your path and testing with an echo/write-host at first. Try this, just to test:

write-host "\\${file_server}\home$\${user_name}"

I don't have access to a Windows machine to test, but I'm guessing the '$' may also be an issue.

try this with a back-tick to try and escape the $:

write-host "\\${file_server}\home`$\${user_name}"

and don't continue, until you know that the string is to your liking. I don't think you need the -prompt in your read-host, though it's probably not an issue. write-hosting is always a nice sanity-check.

I hope to get access to a Windows machine soon so I can provide better help.

Also, if interested, you could provide the username as a parameter at the top of the program, so you can either repeatedly call the script from another that reads names from a file of $file_server, $user_name pairs. Just plop a param( [string] $file_server, [string] $user_name )

Good luck! :)

share|improve this answer
    
Thanks for the response! When I execute the above lines, I get \\\home$\ and \\\home'$\ respectively. I would love to have it setup where I establish the username at the top of the script, that's actually how I am trying to set it up in the long run. At the beginning have it prompt for the username, then from then on out anytime I want the username used just use $username. I just can't quite figure out how to get it to use the variable $username, as I'd like to have this script to be able to use with any new employee (and not have to manually change the code each time. Thanks! –  Don Nov 22 '11 at 22:28
    
I wish I had a Windows machine to test on. I'm totally winging it, lol. Did you try write-host "username=$username" immediately after you assign the $username? If it outputs "username=", then it must be the read-host. Also, we have a discrepancy where I use $user_name and you $username. Sorry about that. I'm sure you probably used $username, but just in case, thought I'd double check. Wish I could help more. Good luck, Don. –  kikuchiyo Nov 23 '11 at 1:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.