Setting folder permissions only works when using $vars?

I’m trying to create a script to create a folder and set permissions. It seems like all example code I can find uses variables to do this. When I try to run the same command without variables (manually set the path, permission, etc) I always get an error. When I run the same command using variables, the command runs.

As far as I can tell, this:

$UserName = Read-Host -Prompt 'User Name?' md \pathtofolder$UserNameFolder1 $Rights = "Modify" $InheritSettings = "Containerinherit, ObjectInherit" $PropogationSettings = "None" $RuleType = "Allow" $path = "\pathtofolder$UserNameFolder1" $acl = Get-Acl $path $perm = $UserName, $Rights, $InheritSettings, $PropogationSettings, $RuleType $rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $perm $acl.SetAccessRule($rule) $acl | Set-Acl -Path $path 

Should be the same as this:

$UserName = Read-Host -Prompt 'User Name?' md \pathtofolder$UserNameFolder1 Get-Acl \pathtofolder$UserNameFolder1.SetAccessRule(New-Object System.Security.AccessControl.FileSystemAccessRule -ArgumentList $UserName, Modify, Containerinherit, ObjectInherit, None, Allow) Get-Acl \pathtofolder$UserNameFolder1 | Set-Acl -Path \10.115.55.10workingENGINEER$UserNameGM 

But it isn’t? Instead I always get the following error:

New-Object : Cannot find an overload for "FileSystemAccessRule" and the argumen t count: "6". At C:New Userstest.ps1:4 char:78 + Get-Acl \pathtofolder$UserNameFolder1.SetAccessRule(New-Object <<<< System.Security.AccessControl.FileSystemAccessRule -ArgumentList $UserNa me, Modify, Containerinherit, ObjectInherit, None, Allow) + CategoryInfo : InvalidOperation: (:) [New-Object], MethodExcept ion + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power Shell.Commands.NewObjectCommand 

I’m not sure what it is I’m missing here. I could always run the command using $vars, but I don’t like to set a bunch of variables that I’ll only use once, and I like to keep my scripts as tidy as possible. Either way, I’d love to know why my edited script doesn’t work.

submitted by /u/TheSmJ
[link] [comments]

Leave a Reply