[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 } } }