-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathRemoveShareACLsForUser.ps1
57 lines (45 loc) · 2.13 KB
/
RemoveShareACLsForUser.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Param([string]$userName)
Function RemoveSharePermission ([string] $ShareName, [string] $DomainUser)
{
$domainUserSplit = $DomainUser.Split("\")
$shss = Get-WmiObject -Class Win32_LogicalShareSecuritySetting -Filter "Name='$ShareName'"
$sd = Invoke-WmiMethod -InputObject $shss -Name GetSecurityDescriptor | Select -ExpandProperty Descriptor
$sclass = [wmiclass] "ROOT\CIMV2:Win32_SecurityDescriptor"
$newsd = $sclass.CreateInstance()
$newsd.ControlFlags = $sd.ControlFlags
foreach ($oace in $sd.DACL)
{
if ($oace.Trustee.Domain -eq $domainUserSplit[0] -and `
$oace.Trustee.Name -eq $domainUserSplit[1] -and `
$oace.AceType -eq 1) {
# Remove Deny ACLs only - we simply don't copy it to the new ACL.
Write-Host "Removing ACL for [$DomainUser] on share [$ShareName]..."
} else {
# Copy this ACE to the new ACL.
$newsd.DACL += [System.Management.ManagementBaseObject] $oace
}
}
$share = Get-WmiObject -Class Win32_LogicalShareSecuritySetting -Filter "Name='$ShareName'"
$setResult = $share.SetSecurityDescriptor($newsd)
#return $setResult.ReturnValue
}
# Verify the script is being run as an administrator
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] “Administrator”))
{
Write-Warning "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
Break
}
if ($userName -eq "") {
# Request the username
Write-Host "This script will remove the Deny ACLs that were created on shares`nto protect against crypto virus infection.`n"
$DomainUser = Read-Host -Prompt "User account (DOMAIN\User)"
} else {
$DomainUser = $userName
}
# Let's try altering share permissions..
$Username = $DomainUser.Split("\")[1]
$affectedShares = Get-WmiObject -Class Win32_Share |
Select Name, Path, Type |
Where { $_.Type -eq 0 }
$affectedShares | % { RemoveSharePermission -ShareName $_.Name -DomainUser $DomainUser }
#Write-Host $affectedShares