====== Berechtigungen Home-Share korrigieren ====== Dieses Script passt die Berechtigungen auf ein neues Homeshare gemäß der Vorgaben auf einer alten Freigabe an. Zudem schreibt es alle wichtigen Aktivitäten in ein Ereignisprotokoll auf c:\tmp\log.txt Damit dieses Script korrekt arbeitet, muss die Powershell-Konsole mit erweiterten Adminrechten gestartet werden. function setfilesystemrights ([string]$file, [string]$PrincipalAccount, [int]$FileSystemrights, [string]$InheritFlags, [string]$Propagationflags, [string]$Policy) { $acl = get-acl $file $acenew = new-object System.Security.AccessControl.filesystemaccessrule($PrincipalAccount,$FileSystemrights,$Inheritflags,$Propagationflags,$Policy) $acl.addaccessrule($acenew) Set-ACL $file $acl #Get-ChildItem -Path $file -recurse | Set-ACL -aclObject $acl } function setfilesystemowner ([string]$file, [string]$PrincipalAccount) { $acl = get-acl $file $acc = new-object System.Security.Principal.NTAccount($PrincipalAccount) $acl.setowner($acc) Set-ACL $file $acl #Get-ChildItem -Path $file -recurse | Set-ACL -aclObject $acl } Get-ChildItem C:\tmp\log.txt | Remove-Item -Force -Confirm:$false Import-Module ActiveDirectory $Folderlist = Get-ChildItem z: foreach ($Folder in $Folderlist) { Try { $ACLOld = Get-ACL $Folder.FullName } catch [System.UnauthorizedAccessException] { "Kein Zugriff auf Ordner " + $Folder.FullName } $Userlist = @() for ($i = 0; $i -lt $ACLOld.Access.count; $i++) { $Identity = $ACLOld.Access[$i].IdentityReference.Value $SamAccountName = $Identity.Split('\') $BoolnoVordefiniert = $SamAccountName[0] -ne "VORDEFINIERT" $BoolnoNTAutoritaet = $SamaccountName[0] -ne "NT-AUTORITÄT" $BoolnoSID = -not $SamAccountName[0].StartsWith("S-1-") if ($SamAccountName[1]) { $BoolnoGroup = -not $SamAccountName[1].StartsWith("0") $BoolnoDomAdmins = -not $SamAccountName[1].StartsWith("Domänen") } else { $BoolnoGroup = $False $BoolnoDomAdmins = $False } $BoolnoSystem = $SamAccountName[1] -ne "SYSTEM" if ($BoolnoVordefiniert -and $BoolnoNTAutoritaet -and $BoolnoGroup -and $BoolnoSID -and $BoolnoDomAdmins -and $BoolnoSystem) { if ($Userlist -contains $Identity) { } else { $Userlist += @($Identity) } } } if ($Userlist.Count -eq 0) {$Folder.FullName + " hat niemanden mehr, der berechtigt ist!" >> c:\tmp\log.txt} if ($Userlist.Count -gt 1) {$Folder.FullName + " hat mehr als einen Benutzer, der berechtigt ist!" >> c:\tmp\log.txt} if ($Userlist.Count -eq 1) { $NewFolder = "\\evagfs02.evag.evg.int\home$\" + $Folder.Name + "\" Try { setfilesystemowner $NewFolder $Userlist[0] $NewFolder + " Besitzer auf " + $Userlist[0] + " geändert" >> c:\tmp\log.txt } catch { "Konnte Besitzer nicht ändern: " + $NewFolder >> c:\tmp\log.txt } Try { setfilesystemrights $NewFolder $Userlist[0] 1245631 "ContainerInherit, ObjectInherit" "None" "Allow" $newFolder + " Berechtigungen gesetzt auf " + $NewFolder >> c:\tmp\log.txt } catch { "Konnte Ändern-Berechtigung nicht setzen auf " + $NewFolder >> c:\tmp\log.txt } } } {{tag>[Powershell]}}