POWERSHELL 9
Install krew Guest on 15th May 2023 09:43:58 PM
  1. #SCRIPT_USER_HOMEDIR="C:\Windows\System32\Config\systemprofile\"
  2. $scriptUserHomedir = $env:USERPROFILE
  3. $krewVersion = "0.4.3"
  4. $krewTargetBinary = "krew-v${krewVersion}.exe"
  5.  
  6. Function Test-CommandExists
  7. {
  8.   Param ($command)
  9.  
  10.   $oldPreference = $ErrorActionPreference
  11.   $ErrorActionPreference = ‘stop’
  12.  
  13.   try {if(Get-Command $command){RETURN $true}}
  14.  
  15.   Catch {Write-Host$command does not exist”; RETURN $false}
  16.  
  17.   Finally {$ErrorActionPreference=$oldPreference}
  18. }
  19.  
  20. # Get current paths
  21. $currentSystemEnvPaths = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
  22.  
  23.  
  24. # Die if git is not installed.
  25. if(!(Test-CommandExists git)) {
  26.   Write-Error "Git does not exist, let's die" -ErrorAction Stop
  27.  
  28. } else {
  29.   $gitPath = Get-Command git
  30.   Write-Host "Found git: $gitPath"
  31. }
  32.  
  33.  
  34. # Store current location
  35. $startLocation = $PWD.Path
  36.  
  37. # Note: How is the krew.exe binary made available to C:\Krew ?
  38.  
  39. # If krew-v{krewVersion}.exe does not exist, pull it.
  40.  
  41. if (-not(Test-Path -Path "${scriptUserHomedir}\${krewTargetBinary}" -PathType Leaf)) {
  42.   try {
  43.     Invoke-WebRequest -Uri "https://github.com/kubernetes-sigs/krew/releases/download/v${krewVersion}/krew.exe" -OutFile "${scriptUserHomedir}\${krewTargetBinary}"
  44.     Write-Host "Pulled krew-v${krewVersion}.exe from github.com, deleting old krew binary"
  45.     Remove-Item "${scriptUserHomedir}\krew*.exe" -Exclude $krewTargetBinary
  46.    }
  47.    catch {
  48.      throw $_.Exception.Message
  49.    }
  50.    
  51.  
  52. # If the file already exists, show the message and do nothing.
  53. }
  54. else {
  55.   Write-Host "${krewTargetBinary} already exists, not pulling."
  56. }
  57.  
  58.  
  59. # Install Krew & Plugins
  60. Write-Host "Install krew + plugins"
  61. set-location $scriptUserHomedir
  62. Start-Process -FilePath $(".\" + $krewTargetBinary) -ArgumentList "install krew" -Wait -WindowStyle Hidden
  63. Start-Process -FilePath $(".\" + $krewTargetBinary) -ArgumentList "install oidc-login" -Wait -WindowStyle Hidden
  64. Start-Process -FilePath $(".\" + $krewTargetBinary) -ArgumentList "install auth-proxy" -Wait -WindowStyle Hidden
  65.  
  66. # Krew is installed in the homedirectory of the System user executing this script
  67. $Source = "${scriptUserHomedir}\.krew"
  68.  
  69. # Krew is then copied to all users
  70. $Destination = 'C:\users\*\.krew'
  71.  
  72. # Copy krew to all available users on the system, except for the user executing this script
  73. $userfolders=Get-ChildItem c:\users
  74. $fullpath= "c:\users\"+$env:username
  75. ForEach ($userfolder in $userfolders) {
  76.  
  77.   if($userfolder.fullname -ne $fullpath) {
  78.     Write-Host "Copy krew to $userfolder"
  79.     Copy-Item -Path $Source -Destination "$($userfolder.fullname)" -Recurse -Force
  80.   } else {
  81.     Write-Host "Don't copy to self ($env:Username)"
  82.   }
  83. }
  84.  
  85.  
  86. # Add krew to System Environment Variables permanently if it is not set
  87. if(!($currentSystemEnvPaths.Contains("krew"))) {
  88.   Write-Host "Adding krew to System Environment Path"
  89.  
  90.   $newSystemPath = "$currentSystemEnvPaths;%USERPROFILE%\.krew\bin"
  91.   Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newSystemPath
  92.  
  93. } else {
  94.   Write-Host "Krew already in System Environment Path:\n$currentSystemEnvPaths"
  95. }
  96.  
  97.  
  98. refreshenv
  99. set-location $startLocation

RSO cPaste е място за публикуване на код или текст за по-лесно отстраняване на грешки.

Влез или се Регистрай за да редактираш, изтриваш или преглеждаш хронология на твоето публикувано съдържание

Необработен текст

Влез или се Регистрирай за да редактираш или задържиш това съдържание.