瀏覽代碼

add set-fsowner

Tobias Simetsreiter 2 年之前
父節點
當前提交
8670118b6d
共有 2 個文件被更改,包括 49 次插入1 次删除
  1. 1 1
      bin/Get-EffectiveAccessRecurse.ps1
  2. 48 0
      bin/Set-FSOwner.ps1

+ 1 - 1
bin/Get-EffectiveAccessRecurse.ps1

@@ -1,6 +1,6 @@
 param(
     $Path,
-    $Account,
+    $Account=$env:USERNAME,
     [Int]
     $Depth=99
 )

+ 48 - 0
bin/Set-FSOwner.ps1

@@ -0,0 +1,48 @@
+param(
+    $Path,
+    $Filter="WABTEC\*",
+    $NewOwner=$env:USERNAME,
+    [switch]
+    $Set=$false,
+    [Int]
+    $Depth=99
+)
+$ErrorActionPreference = "stop"
+$NewOwnerAccount = New-Object System.Security.Principal.Ntaccount($NewOwner);
+
+Get-ChildItem -Recurse -Depth $Depth -Path $path | ForEach-Object {
+    $currentPath = $_.FullName
+    try
+    {
+        $ACL = Get-Acl $currentPath
+    }
+    catch
+    {
+        Write-Output ("Error getting permissions of: " + $currentPath)
+        throw $_
+    }
+    $Owner = ($ACL).Owner
+
+    $out = [PSCustomObject]@{
+        Path=$currentPath;
+        Owner=$Owner;
+        NewOwner=$NewOwnerAccount;
+    }
+    
+    $ACL.SetOwner($NewOwnerAccount) | Out-Null
+
+    if (-not ($Owner -like $Filter)) {
+        $out
+        if ($Set) {
+            try
+            {
+                $ACL | Set-Acl -Path $currentPath;
+            }
+            catch
+            {
+                Write-Output ("Error setting permissions of: " + $currentPath)
+                throw $_
+            }
+        }
+    }
+}