diff options
author | Jaymala Sinha <jsinha@chef.io> | 2019-09-27 11:51:03 -0400 |
---|---|---|
committer | Jaymala Sinha <jsinha@chef.io> | 2019-09-27 12:19:04 -0400 |
commit | 30f68cbc1af21da6b3069ca9c87824cc36e34611 (patch) | |
tree | ac8a4f32e0ccce4b1558550518959df6de79aaca | |
parent | 1ede4136ffffa9205011e8cac045c7c4a9d20222 (diff) | |
download | chef-jsinha/test_win_tests.tar.gz |
Force remove chocolateyjsinha/test_win_tests
Signed-off-by: Jaymala Sinha <jsinha@chef.io>
-rw-r--r-- | scripts/bk_tests/bk_rm_choco.ps1 | 47 | ||||
-rw-r--r-- | scripts/bk_tests/bk_win_functional.ps1 | 2 |
2 files changed, 48 insertions, 1 deletions
diff --git a/scripts/bk_tests/bk_rm_choco.ps1 b/scripts/bk_tests/bk_rm_choco.ps1 new file mode 100644 index 0000000000..2ae07f49b4 --- /dev/null +++ b/scripts/bk_tests/bk_rm_choco.ps1 @@ -0,0 +1,47 @@ +if (!$env:ChocolateyInstall) { + Write-Warning "The ChocolateyInstall environment variable was not found. `n Chocolatey is not detected as installed. Nothing to do" + return + } + if (!(Test-Path "$env:ChocolateyInstall")) { + Write-Warning "Chocolatey installation not detected at '$env:ChocolateyInstall'. `n Nothing to do." + return + } + + $userPath = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment').GetValue('PATH', '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames).ToString() + $machinePath = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment\').GetValue('PATH', '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames).ToString() + + @" + User PATH: + $userPath + + Machine PATH: + $machinePath + + if ($userPath -like "*$env:ChocolateyInstall*") { + Write-Output "Chocolatey Install location found in User Path. Removing..." + # WARNING: This could cause issues after reboot where nothing is + # found if something goes wrong. In that case, look at the backed up + # files for PATH. + [System.Text.RegularExpressions.Regex]::Replace($userPath, [System.Text.RegularExpressions.Regex]::Escape("$env:ChocolateyInstall\bin") + '(?>;)?', '', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase) | %{[System.Environment]::SetEnvironmentVariable('PATH', $_.Replace(";;",";"), 'User')} + } + + if ($machinePath -like "*$env:ChocolateyInstall*") { + Write-Output "Chocolatey Install location found in Machine Path. Removing..." + # WARNING: This could cause issues after reboot where nothing is + # found if something goes wrong. In that case, look at the backed up + # files for PATH. + [System.Text.RegularExpressions.Regex]::Replace($machinePath, [System.Text.RegularExpressions.Regex]::Escape("$env:ChocolateyInstall\bin") + '(?>;)?', '', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase) | %{[System.Environment]::SetEnvironmentVariable('PATH', $_.Replace(";;",";"), 'Machine')} + } + + # Adapt for any services running in subfolders of ChocolateyInstall + $agentService = Get-Service -Name chocolatey-agent -ErrorAction SilentlyContinue + if ($agentService -and $agentService.Status -eq 'Running') { $agentService.Stop() } + # TODO: add other services here + + # delete the contents (remove -WhatIf to actually remove) + Remove-Item -Recurse -Force "$env:ChocolateyInstall" + + [System.Environment]::SetEnvironmentVariable("ChocolateyInstall", $null, 'User') + [System.Environment]::SetEnvironmentVariable("ChocolateyInstall", $null, 'Machine') + [System.Environment]::SetEnvironmentVariable("ChocolateyLastPathUpdate", $null, 'User') + [System.Environment]::SetEnvironmentVariable("ChocolateyLastPathUpdate", $null, 'Machine')
\ No newline at end of file diff --git a/scripts/bk_tests/bk_win_functional.ps1 b/scripts/bk_tests/bk_win_functional.ps1 index 8edf178cc3..41e613fb86 100644 --- a/scripts/bk_tests/bk_win_functional.ps1 +++ b/scripts/bk_tests/bk_win_functional.ps1 @@ -3,7 +3,7 @@ $Properties = 'Caption', 'CSName', 'Version', 'BuildType', 'OSArchitecture' Get-CimInstance Win32_OperatingSystem | Select-Object $Properties | Format-Table -AutoSize # Remove previous installations of chocolatey -mv C:\ProgramData\Chocolatey C:\ProgramData\NO-CHOCOHERE -ErrorAction SilentlyContinue +scripts/bk_tests/bk_rm_choco.ps1 # Install Chocolatey (and essentials) echo "Installing chocolatey" |