GitClone fix

GitClone will work even when Scripts dir already exist
This commit is contained in:
lksz 2020-09-30 11:30:28 -04:00
parent 607fff132f
commit 7117e78a7a
1 changed files with 325 additions and 302 deletions

View File

@ -45,13 +45,24 @@ if ( $GitClone -and -not (Test-Path $(Join-Path $MyPSScriptRoot '.git')) ) {
throw "No git command found, you may either omit the -GitClone switch or install git and try again." throw "No git command found, you may either omit the -GitClone switch or install git and try again."
} }
if ( $PSCmdlet.ShouldProcess("Pull git repo from $GitURL into $MyPSScriptRoot ?") ) { if ( $PSCmdlet.ShouldProcess("Pull git repo from $GitURL into $MyPSScriptRoot ?") ) {
git clone $GitURL $MyPSScriptRoot Push-Location $MyPSScriptRoot
$local:tmpGitDir = New-TemporaryFile
Remove-Item $tmpGitDir
New-Item -Type Directory $tmpGitDir.FullName | Out-Null
$local:GitOutput = "$(& git clone $GitURL $tmpGitDir.FullName --no-checkout)"
Write-Verbose $GitOutput
Move-Item (Join-Path $tmpGitDir.FullName .git) ./.git
Remove-Item $tmpGitDir.FullName
$GitOutput = "$(& git checkout --force)"
Write-Verbose $GitOutput
Pop-Location
} }
} }
$local:ProfileSignature = [PSCustomObject]([ordered]@{ $local:ProfileSignature = [PSCustomObject]([ordered]@{
Begin = '#### SZ Auto Profile Setup - BEGIN ####' Begin = '#### SZ Auto Profile Setup - BEGIN ####'
End = '#### SZ Auto Profile Setup - END ####' End = '#### SZ Auto Profile Setup - END ####'
Removed = '#### SZ Auto Profile Setup'
}) })
function New-ProfileSetupStatus { function New-ProfileSetupStatus {
@ -93,10 +104,11 @@ $_profiles | Foreach-Object {
if ( $_ -match $ProfileSignature.End ) { if ( $_ -match $ProfileSignature.End ) {
$gate = $true; $gate = $true;
} }
} else { }
else {
if ( $_ -match $ProfileSignature.Begin ) { if ( $_ -match $ProfileSignature.Begin ) {
$gate = $false; $gate = $false;
$cleanupValue = ' ' $cleanupValue = $ProfileSignature.Removed
return $randomSeed; return $randomSeed;
} }
return $_ return $_
@ -104,9 +116,11 @@ $_profiles | Foreach-Object {
} }
} }
$local:insertProfileCode = $false;
if ( ($p -eq $_profile) -and -not $RemoveOnly ) { if ( ($p -eq $_profile) -and -not $RemoveOnly ) {
$insertProfileCode = $true
Write-Verbose "Inserting ProfileCode into $($_)..." Write-Verbose "Inserting ProfileCode into $($_)..."
$profileContent = $profileContent -replace $randomSeed,$((@( $cleanupValue = $((@(
'', '',
$ProfileSignature.Begin, $ProfileSignature.Begin,
'', '',
@ -127,7 +141,7 @@ $_profiles | Foreach-Object {
$local:status = 'Skipped' $local:status = 'Skipped'
if ( $VerbosePreference -eq 'Continue' ) { $ShowSkipped = $true } if ( $VerbosePreference -eq 'Continue' ) { $ShowSkipped = $true }
if ( -not ([string]::IsNullOrWhiteSpace($profileContent)) ) { if ( -not ([string]::IsNullOrWhiteSpace($profileContent)) ) {
$status = "Need $(if($cleanupValue){'Setup'}else{'Cleanup'})" $status = "Need $(if($insertProfileCode){'Setup'}else{'Cleanup'})"
if ( $sudo -or $userFile ) { if ( $sudo -or $userFile ) {
Write-Verbose "Writing content to $($_)..." Write-Verbose "Writing content to $($_)..."
$local:tmpOutput = New-TemporaryFile -WhatIf:$false $local:tmpOutput = New-TemporaryFile -WhatIf:$false
@ -142,27 +156,33 @@ $_profiles | Foreach-Object {
if ( $errMsg ) { if ( $errMsg ) {
Write-Error "$errMsg" Write-Error "$errMsg"
$status = $status -replace 'Need ', 'Failed ' $status = $status -replace 'Need ', 'Failed '
} else { }
else {
$status = $status -replace 'Need ', '' $status = $status -replace 'Need ', ''
} }
} catch { }
catch {
Write-Error "Writing $p failed!" Write-Error "Writing $p failed!"
} }
} else { }
else {
$status = "Need $status" $status = "Need $status"
Write-Error "Cannot write into [$_]'$p'! Please re-run with sudo." Write-Error "Cannot write into [$_]'$p'! Please re-run with sudo."
} }
} elseif( ($Force -or $RemoveOnly) -and $exists ) { }
elseif ( ($Force -or $RemoveOnly) -and $exists ) {
$status = 'Need Removal' $status = 'Need Removal'
if ( $sudo -or $userFile ) { if ( $sudo -or $userFile ) {
Write-Verbose "Removing [$_]'$p'..." Write-Verbose "Removing [$_]'$p'..."
try { try {
Invoke-ExpressionEx -sudo:$shouldSudo Remove-Item $p Invoke-ExpressionEx -sudo:$shouldSudo Remove-Item $p
if ( -not (Test-Path $p) ) { $status = 'Removed' } if ( -not (Test-Path $p) ) { $status = 'Removed' }
} catch { }
catch {
Write-Error "Removal of $p failed!" Write-Error "Removal of $p failed!"
} }
} else { }
else {
Write-Error "Cannot remove [$_]'$p'! Please re-run with sudo." Write-Error "Cannot remove [$_]'$p'! Please re-run with sudo."
} }
} }
@ -175,6 +195,9 @@ $_profiles | Foreach-Object {
Remove-Item function:ProfileCode* -Confirm:$false Remove-Item function:ProfileCode* -Confirm:$false
} }
#######################################################################
## ProfileCode_common (source) containing mandatory code for $PROFILE
#######################################################################
function ProfileCode_common { function ProfileCode_common {
function Get-PowerShellPath { function Get-PowerShellPath {
Get-Process -PID $PID | ForEach-Object { $_.Path, $_.Parent.Path } | Where-Object { $_ -match 'powershell|pwsh' } | Select-Object -First 1 Get-Process -PID $PID | ForEach-Object { $_.Path, $_.Parent.Path } | Where-Object { $_ -match 'powershell|pwsh' } | Select-Object -First 1
@ -215,18 +238,21 @@ function ProfileCode_common {
} }
if ( $sudo_exec ) { if ( $sudo_exec ) {
& $sudo_exec $sudo_cmd & $sudo_exec $sudo_cmd
} else { }
else {
Write-Error "Didn't find a known sudo command" Write-Error "Didn't find a known sudo command"
} }
} }
if ( $PSVersionTable.Platform -eq 'Unix' ) { if ( $PSVersionTable.Platform -eq 'Unix' ) {
/usr/bin/env sudo $(Get-PowerShellPath) "-noprofile" "-EncodedCommand" $(ConvertTo-Base64 $sudo_cmd) /usr/bin/env sudo $(Get-PowerShellPath) "-noprofile" "-EncodedCommand" $(ConvertTo-Base64 $sudo_cmd)
} else { }
else {
$local:currentIdenity = [System.Security.Principal.WindowsIdentity]::GetCurrent() $local:currentIdenity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
if ( $currentIdenity.UserClaims.Value -contains 'S-1-5-32-544' ) { if ( $currentIdenity.UserClaims.Value -contains 'S-1-5-32-544' ) {
do_sudo_win $sudo_cmd do_sudo_win $sudo_cmd
} else { }
else {
Write-Error "User [$($currentIdenity.Name)]does not have permissions to run as admin." Write-Error "User [$($currentIdenity.Name)]does not have permissions to run as admin."
} }
} }
@ -310,7 +336,6 @@ function ProfileCode_common {
return return
} }
} }
} }
function Export-FunctionSource { function Export-FunctionSource {
@ -319,7 +344,6 @@ function ProfileCode_common {
[Parameter(Position = 0, ValueFromRemainingArguments = $true)] [Parameter(Position = 0, ValueFromRemainingArguments = $true)]
#[ValidateSet([Functions])] #[ValidateSet([Functions])]
[string[]]$FunctionName [string[]]$FunctionName
) )
$local:src = "" $local:src = ""
@ -371,7 +395,6 @@ function ProfileCode_common {
$env:PATH = $p -join $PathEnvDelimiter $env:PATH = $p -join $PathEnvDelimiter
} }
if ( -not $MyPSScriptRoot -or (Test-Path function:Test-IsAdmin, function:ConvertTo-Base64, function:Invoke-ExpressionEx, function:Get-PowerShellPath, function:Export-FunctionSource | Where-Object { -not $_ } | Measure-Object | Select-Object -ExpandProperty Count) ) { if ( -not $MyPSScriptRoot -or (Test-Path function:Test-IsAdmin, function:ConvertTo-Base64, function:Invoke-ExpressionEx, function:Get-PowerShellPath, function:Export-FunctionSource | Where-Object { -not $_ } | Measure-Object | Select-Object -ExpandProperty Count) ) {
Write-Verbose "Calling ProfileCode inline..." Write-Verbose "Calling ProfileCode inline..."
. ProfileCode_common . ProfileCode_common