Andy
Andy

Reputation: 11

Issue passing a variable through a Powershell script

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?

Upvotes: 1

Views: 484

Answers (1)

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

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

Upvotes: 1

Related Questions