Added Repair-Permissions

This commit is contained in:
lksz 2020-12-07 04:41:46 -05:00
parent 3844492976
commit abd9cc4abc
1 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,60 @@
[CmdletBinding(SupportsShouldProcess)]param(
[Parameter(Mandatory,ValueFromPipeline)]
[string[]]$Path,
[switch]$sudo,
[string]$User,
[string]$Group,
[string]$FilePermission,
[string]$DirPermission,
[switch]$Force,
[switch]$NonRecursive
)
begin {
if( -not $sudo -and -not (Test-IsAdmin) ) {
throw "This command must be run in Admin context"
}
if( [string]::IsNullOrWhiteSpace($User) ) {
$User = $env:SZ_DOCKER_PUID ?? $env:USER;
}
$User = "$(& id --user --name $User)";
if( [string]::IsNullOrWhiteSpace($Group) ) {
$Group = $env:SZ_DOCKER_GUID ?? "$(& id --group --name $User)"
}
if( [string]::IsNullOrWhiteSpace($FilePermission) ) {
$FilePermission = $env:SZ_FILE_PERM ?? "664"
}
if( [string]::IsNullOrWhiteSpace($DirPermission) ) {
$DirPermission = $env:SZ_DIR_PERM ?? "664"
}
$cmdArgs = "-Recurse:`$$(-not $NonRecursive) -Force:`$$Force"
}
process {
$Path | Get-Item | ForEach-Object {
$local:p = $_.FullName
$local:nextCmd = ""
if( $_.Attributes -band [System.IO.FileAttributes]::Directory ) {
$nextCmd = "Get-ChildItem $p $cmdArgs | ForEach-Object { & chown ${User}:${Group} `$_ }"
if( $PSCmdlet.ShouldProcess( "Set $p ownership to ${User}:${Group}`n$nextCmd`n" ) ) {
Invoke-ExpressionEx -sudo:$sudo $nextCmd
}
}
$nextCmd = "Get-ChildItem $p -Directory $cmdArgs | ForEach-Object { & chmod $DirPermission `$_ }"
if( $PSCmdlet.ShouldProcess( "Set $p dirs permissions to $DirPermission`n$nextCmd`n" ) ) {
Invoke-ExpressionEx -sudo:$sudo $nextCmd
}
$nextCmd = "Get-ChildItem $p -File $cmdArgs | ForEach-Object { & chmod $FilePermission `$_ }"
if( $PSCmdlet.ShouldProcess( "Set $p file permissions to $FilePermission`n$nextCmd`n" ) ) {
Invoke-ExpressionEx -sudo:$sudo $nextCmd
}
}
}