Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

New folder and modify access rights

$
0
0

I have played around with a script.

My script looks like this:

#Variables
$GroupNameTIA="F_KSMCommon-common-ScanTIA_m"
$Directory = Read-Host "Folder name: "
$DirectoryPath = "z:\$Directory"
    #Mount directory
    Net use Z: \\stg-w75\ksmcommon\common\ScanTIA
    #Create new folder
    New-Item -Type directory -path $DirectoryPath
    takeown.exe /F $DirectoryPath
    #Read and modify ACL
    $ACL = Get-Acl $DirectoryPath
    $ACL.SetAccessRuleProtection($True, $True)
    Set-Acl $DirectoryPath $ACL
    $user_Account = $GroupNameTIA
    $Acl = (Get-Item $DirectoryPath).GetAccessControl(“Access”)
    $Ar = New-Object system.Security.AccessControl.FileSystemAccessRule($user_account,“FullControl”, “ContainerInherit, ObjectInherit”, “None”, “Allow”)
    $Acl.Setaccessrule($Ar)
    Set-Acl $DirectoryPath $Acl
    $Ace = New-Object System.Security.AccessControl.FileSystemAccessRule (
    $GroupNameTIA,"Delete", 
        [System.Security.AccessControl.InheritanceFlags]::None, 
        [System.Security.AccessControl.PropagationFlags]::None, 
        [System.Security.AccessControl.AccessControlType]::Allow
        )
    $SD = Get-Acl $DirectoryPath
    $SD.RemoveAccessRule($Ace)
    (Get-Item $DirectoryPath).SetAccessControl($SD) 
    #delete mounted directory
    net use /d z:

If I run this script I got an error:

Set-Acl : Attempted to perform an unauthorized operation.
At C:\PSScripts\NeuerScanOrdner.ps1:187 char:12
+     Set-Acl <<<<  $DirectoryPath $ACL
    + CategoryInfo          : PermissionDenied: (Z:\tia-test5:String) [Set-Acl], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetAclCommand

Set-Acl : Attempted to perform an unauthorized operation.
At C:\PSScripts\NeuerScanOrdner.ps1:193 char:12
+     Set-Acl <<<<  $DirectoryPath $Acl
    + CategoryInfo          : PermissionDenied: (Z:\tia-test5:String) [Set-Acl], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetAclCommand

It makes no differencewhetherI let the scriptrunwith an administatoraccount or"runas administrator".
Although Iam theowner of the folder, I get thiserror message.
I do not know why this happend. Whats wrong?


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>