#SCRIPT_USER_HOMEDIR="C:\Windows\System32\Config\systemprofile\" $scriptUserHomedir = $env:USERPROFILE $krewVersion = "0.4.3" $krewTargetBinary = "krew-v${krewVersion}.exe" Function Test-CommandExists { Param ($command) $oldPreference = $ErrorActionPreference $ErrorActionPreference = ‘stop’ try {if(Get-Command $command){RETURN $true}} Catch {Write-Host “$command does not exist”; RETURN $false} Finally {$ErrorActionPreference=$oldPreference} } # Get current paths $currentSystemEnvPaths = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path # Die if git is not installed. if(!(Test-CommandExists git)) { Write-Error "Git does not exist, let's die" -ErrorAction Stop } else { $gitPath = Get-Command git Write-Host "Found git: $gitPath" } # Store current location $startLocation = $PWD.Path # Note: How is the krew.exe binary made available to C:\Krew ? # If krew-v{krewVersion}.exe does not exist, pull it. if (-not(Test-Path -Path "${scriptUserHomedir}\${krewTargetBinary}" -PathType Leaf)) { try { Invoke-WebRequest -Uri "https://github.com/kubernetes-sigs/krew/releases/download/v${krewVersion}/krew.exe" -OutFile "${scriptUserHomedir}\${krewTargetBinary}" Write-Host "Pulled krew-v${krewVersion}.exe from github.com, deleting old krew binary" Remove-Item "${scriptUserHomedir}\krew*.exe" -Exclude $krewTargetBinary } catch { throw $_.Exception.Message } # If the file already exists, show the message and do nothing. } else { Write-Host "${krewTargetBinary} already exists, not pulling." } # Install Krew & Plugins Write-Host "Install krew + plugins" set-location $scriptUserHomedir Start-Process -FilePath $(".\" + $krewTargetBinary) -ArgumentList "install krew" -Wait -WindowStyle Hidden Start-Process -FilePath $(".\" + $krewTargetBinary) -ArgumentList "install oidc-login" -Wait -WindowStyle Hidden Start-Process -FilePath $(".\" + $krewTargetBinary) -ArgumentList "install auth-proxy" -Wait -WindowStyle Hidden # Krew is installed in the homedirectory of the System user executing this script $Source = "${scriptUserHomedir}\.krew" # Krew is then copied to all users $Destination = 'C:\users\*\.krew' # Copy krew to all available users on the system, except for the user executing this script $userfolders=Get-ChildItem c:\users $fullpath= "c:\users\"+$env:username ForEach ($userfolder in $userfolders) { if($userfolder.fullname -ne $fullpath) { Write-Host "Copy krew to $userfolder" Copy-Item -Path $Source -Destination "$($userfolder.fullname)" -Recurse -Force } else { Write-Host "Don't copy to self ($env:Username)" } } # Add krew to System Environment Variables permanently if it is not set if(!($currentSystemEnvPaths.Contains("krew"))) { Write-Host "Adding krew to System Environment Path" $newSystemPath = "$currentSystemEnvPaths;%USERPROFILE%\.krew\bin" Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newSystemPath } else { Write-Host "Krew already in System Environment Path:\n$currentSystemEnvPaths" } refreshenv set-location $startLocation