2020-12-07 09:41:46 +00:00
|
|
|
[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)";
|
2021-01-29 23:26:39 +00:00
|
|
|
if( [string]::IsNullOrWhiteSpace($User) ) {
|
|
|
|
$User = $env:USER;
|
|
|
|
}
|
|
|
|
$User = "$(& id --user --name $User)";
|
|
|
|
|
2020-12-07 09:41:46 +00:00
|
|
|
if( [string]::IsNullOrWhiteSpace($Group) ) {
|
|
|
|
$Group = $env:SZ_DOCKER_GUID ?? "$(& id --group --name $User)"
|
2021-01-29 23:26:39 +00:00
|
|
|
}
|
|
|
|
if( [string]::IsNullOrWhiteSpace($Group) ) {
|
|
|
|
$Group = "$(& id --group --name $User)"
|
2020-12-07 09:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( [string]::IsNullOrWhiteSpace($FilePermission) ) {
|
|
|
|
$FilePermission = $env:SZ_FILE_PERM ?? "664"
|
|
|
|
}
|
|
|
|
|
|
|
|
if( [string]::IsNullOrWhiteSpace($DirPermission) ) {
|
2021-01-13 06:02:37 +00:00
|
|
|
$DirPermission = $env:SZ_DIR_PERM ?? "775"
|
2020-12-07 09:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$cmdArgs = "-Recurse:`$$(-not $NonRecursive) -Force:`$$Force"
|
|
|
|
}
|
|
|
|
|
|
|
|
process {
|
2021-01-29 23:26:39 +00:00
|
|
|
$local:Dirs = @()
|
|
|
|
$local:Files = @()
|
|
|
|
$local:item = $Path | Get-Item
|
|
|
|
|
2020-12-07 09:41:46 +00:00
|
|
|
$Path | Get-Item | ForEach-Object {
|
2021-11-29 22:00:43 +00:00
|
|
|
$local:p = $_.FullName -replace "'","''"
|
2020-12-07 09:41:46 +00:00
|
|
|
$local:nextCmd = ""
|
2021-01-29 23:26:39 +00:00
|
|
|
|
|
|
|
$nextCmd = "(@(Get-Item '$p') + (Get-ChildItem '$p' $cmdArgs)) | " +
|
|
|
|
"ForEach-Object { & chown ${User}:${Group} `$_.FullName }"
|
|
|
|
$local:msg = "Set $p ownership to ${User}:${Group}"
|
|
|
|
if( $PSCmdlet.ShouldProcess( "$msg`n$nextCmd`n" ) ) {
|
|
|
|
Write-Host -ForegroundColor Cyan $msg
|
|
|
|
Invoke-ExpressionEx -sudo:$sudo $nextCmd
|
2020-12-07 09:41:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 23:26:39 +00:00
|
|
|
$nextCmd = "(Get-ChildItem '$p' -Directory $cmdArgs)) | ForEach-Object { & chmod $DirPermission `$_ }"
|
|
|
|
if( $_.Attributes -band 'Directory' ) { $nextCmd = "@(Get-Item '$p') + $nextCmd" }
|
|
|
|
$nextCmd = "($nextCmd"
|
|
|
|
$msg = "Set $p dirs permissions to $DirPermission"
|
|
|
|
if( $PSCmdlet.ShouldProcess( "$msg`n$nextCmd`n" ) ) {
|
|
|
|
Write-Host -ForegroundColor Cyan $msg
|
2020-12-07 09:41:46 +00:00
|
|
|
Invoke-ExpressionEx -sudo:$sudo $nextCmd
|
|
|
|
}
|
|
|
|
|
2021-01-29 23:26:39 +00:00
|
|
|
$nextCmd = "(Get-ChildItem '$p' -File $cmdArgs)) | ForEach-Object { & chmod $FilePermission `$_ }"
|
|
|
|
if( -not ($_.Attributes -band 'Directory') ) { $nextCmd = "@(Get-Item '$p') + $nextCmd" }
|
|
|
|
$nextCmd = "($nextCmd"
|
|
|
|
$msg = "Set $p file permissions to $FilePermission"
|
|
|
|
if( $PSCmdlet.ShouldProcess( "$msg`n$nextCmd`n" ) ) {
|
|
|
|
Write-Host -ForegroundColor Cyan $msg
|
2020-12-07 09:41:46 +00:00
|
|
|
Invoke-ExpressionEx -sudo:$sudo $nextCmd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|