AppendPsModulePath.ps1

# Copyright 2015-2016 Google Inc. All Rights Reserved.
# Licensed under the Apache License Version 2.0.
#
# Appends the Cloud Tools for PowerShell module location to the registry's PSModulePath environment
# variable

param($installPath) # Let a user manually select a Cloud SDK install path
$installPath = $installPath -or $args[0]

# Find the Google Cloud SDK install path from the registry.
if (-not $installPath) {
    $hklmPath = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Google Cloud SDK"
    $hkcuPath = "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Google Cloud SDK"
    if (Test-Path $hklmPath) {
        $installPath = Get-ItemPropertyValue $hklmPath InstallLocation
    } elseif (Test-Path $hkcuPath) {
        $installPath = Get-ItemPropertyValue $hkcuPath InstallLocation
    } else {
        Write-Error "Can not find Cloud SDK from the registry."
        return
    }
}
$installPath = $installPath -replace '"' # Registry values had quotes for some reason
# The folder above where the PowerShell cmdlets are installed. PowerShell will crawl subfolders
# looking for .psd1 files. (So it should find .\GoogleCloud\GoogleCloud.psd1.)
$googlePowerShellPath = Join-Path $installPath "google-cloud-sdk\platform\PowerShell"

if (-not (Test-Path $googlePowerShellPath)) {
    Write-Error "Can not find Google PowerShell. $googlePowerShellPath does not exist."
    return
}

# Get the value of the two registry keys that initialize the environment variable.
$hklmLocation = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
$hklmValue = (Get-ItemProperty $hklmLocation).PSModulePath
$hkcuLocation = "HKCU:\Environment"
$hkcuValue = (Get-ItemProperty $hkcuLocation).PSModulePath
$regValue = (($hklmValue, $hkcuValue | ?{$_}) -join ";")

if(($regValue -split ";" -contains $googlePowerShellPath))
{
    Write-Warning "Path already exists."
    return
}

$wid = [Security.Principal.WindowsIdentity]::GetCurrent()
$wip = New-Object Security.Principal.WindowsPrincipal $wid
$isElevated = $wip.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)

if ($isElevated -and (Test-Path $hklmPath))
{
    # If we are running as administrator, and the Cloud SDK is installed for all users,
    # append to the PSModluePath in the local machine registry location.
    Write-Verbose "Adding to registry key for all users."
    Push-Location $hklmLocation
    # Don't add a semicolon if $hklmValue is $null
    Set-ItemProperty . PSModulePath (($hklmValue, $googlePowerShellPath | ?{$_}) -join ";")
}
else
{
    # If we are running as a local user, or Cloud SDK is installed for just the current user,
    # append to the PSModluePath in the current user registry location.
    Write-Verbose "Adding to registry key for user $env:UserName."
    Push-Location $hkcuLocation
    # Don't add a semicolon if $hkcuValue is $null
    Set-ItemProperty . PSModulePath (($hkcuValue, $googlePowerShellPath | ?{$_}) -join ";")
}
Write-Output (Get-ItemProperty . PSModulePath)
Pop-Location

# SIG # Begin signature block
# MIIUEgYJKoZIhvcNAQcCoIIUAzCCE/8CAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG
# KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCD01HaJnWAqSsq2
# ZD/WjPjIqXLcOniMqrC/P3Xn8S2PtaCCDtswggSZMIIDgaADAgECAg8WiPA5JV5j
# jmkUOQfmMwswDQYJKoZIhvcNAQEFBQAwgZUxCzAJBgNVBAYTAlVTMQswCQYDVQQI
# EwJVVDEXMBUGA1UEBxMOU2FsdCBMYWtlIENpdHkxHjAcBgNVBAoTFVRoZSBVU0VS
# VFJVU1QgTmV0d29yazEhMB8GA1UECxMYaHR0cDovL3d3dy51c2VydHJ1c3QuY29t
# MR0wGwYDVQQDExRVVE4tVVNFUkZpcnN0LU9iamVjdDAeFw0xNTEyMzEwMDAwMDBa
# Fw0xOTA3MDkxODQwMzZaMIGEMQswCQYDVQQGEwJHQjEbMBkGA1UECBMSR3JlYXRl
# ciBNYW5jaGVzdGVyMRAwDgYDVQQHEwdTYWxmb3JkMRowGAYDVQQKExFDT01PRE8g
# Q0EgTGltaXRlZDEqMCgGA1UEAxMhQ09NT0RPIFN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