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."
}
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]@{
Begin = '#### SZ Auto Profile Setup - BEGIN ####'
End = '#### SZ Auto Profile Setup - END ####'
Removed = '#### SZ Auto Profile Setup'
})
function New-ProfileSetupStatus {
@ -93,10 +104,11 @@ $_profiles | Foreach-Object {
if ( $_ -match $ProfileSignature.End ) {
$gate = $true;
}
} else {
}
else {
if ( $_ -match $ProfileSignature.Begin ) {
$gate = $false;
$cleanupValue = ' '
$cleanupValue = $ProfileSignature.Removed
return $randomSeed;
}
return $_
@ -104,9 +116,11 @@ $_profiles | Foreach-Object {
}
}
$local:insertProfileCode = $false;
if ( ($p -eq $_profile) -and -not $RemoveOnly ) {
$insertProfileCode = $true
Write-Verbose "Inserting ProfileCode into $($_)..."
$profileContent = $profileContent -replace $randomSeed,$((@(
$cleanupValue = $((@(
'',
$ProfileSignature.Begin,
'',
@ -127,7 +141,7 @@ $_profiles | Foreach-Object {
$local:status = 'Skipped'
if ( $VerbosePreference -eq 'Continue' ) { $ShowSkipped = $true }
if ( -not ([string]::IsNullOrWhiteSpace($profileContent)) ) {
$status = "Need $(if($cleanupValue){'Setup'}else{'Cleanup'})"
$status = "Need $(if($insertProfileCode){'Setup'}else{'Cleanup'})"
if ( $sudo -or $userFile ) {
Write-Verbose "Writing content to $($_)..."
$local:tmpOutput = New-TemporaryFile -WhatIf:$false
@ -142,27 +156,33 @@ $_profiles | Foreach-Object {
if ( $errMsg ) {
Write-Error "$errMsg"
$status = $status -replace 'Need ', 'Failed '
} else {
}
else {
$status = $status -replace 'Need ', ''
}
} catch {
}
catch {
Write-Error "Writing $p failed!"
}
} else {
}
else {
$status = "Need $status"
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'
if ( $sudo -or $userFile ) {
Write-Verbose "Removing [$_]'$p'..."
try {
Invoke-ExpressionEx -sudo:$shouldSudo Remove-Item $p
if ( -not (Test-Path $p) ) { $status = 'Removed' }
} catch {
}
catch {
Write-Error "Removal of $p failed!"
}
} else {
}
else {
Write-Error "Cannot remove [$_]'$p'! Please re-run with sudo."
}
}
@ -175,6 +195,9 @@ $_profiles | Foreach-Object {
Remove-Item function:ProfileCode* -Confirm:$false
}
#######################################################################
## ProfileCode_common (source) containing mandatory code for $PROFILE
#######################################################################
function ProfileCode_common {
function Get-PowerShellPath {
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 ) {
& $sudo_exec $sudo_cmd
} else {
}
else {
Write-Error "Didn't find a known sudo command"
}
}
if ( $PSVersionTable.Platform -eq 'Unix' ) {
/usr/bin/env sudo $(Get-PowerShellPath) "-noprofile" "-EncodedCommand" $(ConvertTo-Base64 $sudo_cmd)
} else {
}
else {
$local:currentIdenity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
if ( $currentIdenity.UserClaims.Value -contains 'S-1-5-32-544' ) {
do_sudo_win $sudo_cmd
} else {
}
else {
Write-Error "User [$($currentIdenity.Name)]does not have permissions to run as admin."
}
}
@ -310,7 +336,6 @@ function ProfileCode_common {
return
}
}
}
function Export-FunctionSource {
@ -319,7 +344,6 @@ function ProfileCode_common {
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
#[ValidateSet([Functions])]
[string[]]$FunctionName
)
$local:src = ""
@ -371,7 +395,6 @@ function ProfileCode_common {
$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) ) {
Write-Verbose "Calling ProfileCode inline..."
. ProfileCode_common