Skip to content

Instantly share code, notes, and snippets.

@csharpforevermore
Created February 21, 2020 13:06
Show Gist options
  • Save csharpforevermore/c0e53438b228a60698167624ab4785f9 to your computer and use it in GitHub Desktop.
Save csharpforevermore/c0e53438b228a60698167624ab4785f9 to your computer and use it in GitHub Desktop.
Powershell script to add 3 roles to files and folders in a set location
cls
# Define the owner account/group
$Account = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList 'Everyone';
$Source = 'D:\Applications\Leafletdrop.Q4CmsChange'
# Get a list of folders and files
$ItemList = Get-ChildItem -Path $Source -Recurse;
# Iterate over files/folders
foreach ($Item in $ItemList) {
try {
$Path = $Item.FullName;
$acl = Get-Acl $Source
$AccessRule =
$AccessRule1 = New-Object System.Security.AccessControl.FileSystemAccessRule("IIS_IUSRS","FullControl","Allow");
$AccessRule2 = New-Object System.Security.AccessControl.FileSystemAccessRule("NETWORK SERVICE","FullControl","Allow");
$AccessRule3 = New-Object System.Security.AccessControl.FileSystemAccessRule("IUSR","FullControl","Allow");
$acl.SetAccessRule($AccessRule1)
$acl.SetAccessRule($AccessRule2)
$acl.SetAccessRule($AccessRule3)
$acl | Set-Acl -Path $Patha
#Write-Host "$Path - $AccessRule.IdentityReference";
}
catch {
Write-Host "An error occurred:"
Write-Host $_
Write-Host "Press any key to continue..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment