1

Trying to create a powershell script to perform the following tasks: - Asks for a username to use when creating folder and assigning permissions - Create folder on our NAS with that username for a name - Create DFS folder with same name and assign it a target path - Assign explicit permissions for the user to that DFS folder

My script looks like the following:

$Username = read-host "Type Username"

#Create new folder on the NAS and set necessary permissions
New-Item -Path \\NAS\USER_SHARE\$Username -ItemType Directory

#Create new DFS folder and target
New-DfsnFolder -Path "\\DOMAIN.local\user\$Username" -TargetPath "\\NAS\USER_SHARE\$Username"

#Assign explicit permissions to DFS folder for user
dfsutil property SD grant \\DOMAIN.local\user\$Username DOMAIN\$Username:F protect

It performs everything but the last line. It creates the DFS folder but doesn't give permissions. No error message is given, just prints out the help info for the dfsutil command. When I run the last line independently with a static username, it is successful. So I believe there is something with the syntax of the $Username:F part of that last line of code messing it up.

How can I separate that last section to make this work?

Andy
  • 11
  • 1

1 Answers1

1

Try the invoke operator: &. Also, your variable call to $Username is going to cause issues with the colon, so you want to use {} around the variable name.

$Params = @('property','SD','grant',
            "\\DOMAIN.local\user\$Username",
            "DOMAIN\${Username}:F",
            'protect')
& dfsutil @Params
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • Thank you for the response. I should place that code at the beginning with the other variable? – Andy Aug 15 '17 at 18:44
  • 1
    @Andy No, I just cleaned up how I would rewrite your `dfsutil` section. You can get away with throwing a `&` in front of the call, and using the `{}` braces around `${Username}:F` – Maximilian Burszley Aug 15 '17 at 18:45