Last active
September 11, 2018 02:37
-
-
Save bdwyertech/4e21f83ac637e0d94e88 to your computer and use it in GitHub Desktop.
Setter for ChefDK Environmental Variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Filename: Set-ChefDK_Enviro_Vars.ps1 | |
| # Brian Dwyer - Intelligent Digital Services - 11/5/14 | |
| # ***USAGE*** | |
| # To Setup the ChefDK Ruby Variables | |
| # ./Set-ChefDK_Enviro_Vars.ps1 set | |
| # To Remove the ChefDK Ruby Variables | |
| # ./Set-ChefDK_Enviro_Vars.ps1 unset | |
| # System-Wide Environmental Variables | |
| $System_Vars='HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' | |
| # User-Specific Environmental Variables | |
| $User_Vars='HKCU:\Environment' | |
| # Make Sure we don't expand/evaluate environmental variables on the return | |
| $DoNotExpand=[Microsoft.WIN32.RegistryValueOptions]::DoNotExpandEnvironmentNames | |
| # Check if ChefDK is installed | |
| If (!$env:Path.Contains('chefdk\bin')) | |
| { | |
| echo "" | |
| echo "/======| Error: ChefDK does not seem to be installed. |=====\" | |
| echo "" | |
| pause | |
| exit | |
| } | |
| # Get ChefDK Installation Directory | |
| $ChefDK_DIR = $env:Path.Split(';') -like "*chefdk\bin*" | Out-String -Stream | Split-Path -Parent | |
| # Determine ChefDK Ruby Version | |
| $ChefDK_RubyVer = Get-ChildItem -Name $ChefDK_DIR\embedded\lib\ruby\gems | |
| # Setup | |
| If ( $args[0] -eq 'set' ) | |
| { | |
| echo "/======| Setting up Registry Keys... |=====\" | |
| Set-ItemProperty $User_Vars -Name 'CHEFDK_RUBY' -Value "$env:USERPROFILE\.chefdk\gem\ruby\$ChefDK_RubyVer\bin;$ChefDK_DIR\embedded\bin" | |
| Set-ItemProperty $User_Vars -Name 'GEM_ROOT' -Value "$ChefDK_DIR\embedded\lib\ruby\gems\$ChefDK_RubyVer" | |
| Set-ItemProperty $User_Vars -Name 'GEM_HOME' -Value "$env:USERPROFILE\.chefdk\gem\ruby\$ChefDK_RubyVer" | |
| Set-ItemProperty $User_Vars -Name 'GEM_PATH' -Value "$env:USERPROFILE\.chefdk\gem\ruby\$ChefDK_RubyVer;$ChefDK_DIR\lib\ruby\gems\$ChefDK_RubyVer" | |
| If (!(Get-Item $User_Vars).GetValue('PATH','Default',$DoNotExpand).Contains('CHEFDK_RUBY')) | |
| { | |
| If (((Get-ItemProperty $User_Vars).PATH) -eq $null) | |
| { | |
| Set-ItemProperty $User_Vars -Name 'PATH' -Value '%CHEFDK_RUBY%' | |
| } | |
| Else | |
| { | |
| $NewVal=(Get-Item $User_Vars).GetValue('PATH','Default',$DoNotExpand) + ';%CHEFDK_RUBY%' | |
| Set-ItemProperty $User_Vars -Name 'PATH' -Value $NewVal | |
| } | |
| } | |
| pause | |
| exit | |
| } | |
| Elseif ( $args[0] -eq 'unset' ) | |
| { | |
| echo "/======| Removing Registry Keys... |=====\" | |
| Set-ItemProperty $User_Vars -Name 'PATH' -Value (Get-Item $User_Vars).GetValue('PATH','Default',$DoNotExpand).replace(';%CHEFDK_RUBY%', '') | |
| Set-ItemProperty $User_Vars -Name 'PATH' -Value (Get-Item $User_Vars).GetValue('PATH','Default',$DoNotExpand).replace('%CHEFDK_RUBY%', '') | |
| Remove-ItemProperty $User_Vars -Name 'CHEFDK_RUBY' | |
| Remove-ItemProperty $User_Vars -Name 'GEM_ROOT' | |
| Remove-ItemProperty $User_Vars -Name 'GEM_HOME' | |
| Remove-ItemProperty $User_Vars -Name 'GEM_PATH' | |
| pause | |
| exit | |
| } | |
| Else | |
| { | |
| echo '------------------------------------------------' | |
| echo "|*| - Set ChefDK Environmental Variables - |*|" | |
| echo '------------------------------------------------' | |
| echo "| Use 'set' or 'unset' to control script |" | |
| echo '------------------------------------------------' | |
| pause | |
| exit | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is really cool! Thank you!