Last active
February 28, 2017 06:03
-
-
Save atruskie/4f53dc7f0c28747d937e to your computer and use it in GitHub Desktop.
Mock the bash export function in powershell
This file contains 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
function Set-EnvironmentVariable($key, $value) { | |
Set-Content -Value $value -Path ("env:" + $key) | |
New-Variable -Scope Global -Name $key -Value $value -Force | |
} | |
function export($envVar) { | |
$split = $envVar.Trim().Split("="); | |
Set-EnvironmentVariable $split[0] $split[1] | |
} | |
function Decrypt-SecureString { | |
param( | |
[Parameter(ValueFromPipeline=$true,Mandatory=$true,Position=0)] | |
[System.Security.SecureString] | |
$sstr | |
) | |
if(!$sstr) { | |
return | |
} | |
$marshal = [System.Runtime.InteropServices.Marshal] | |
$ptr = $marshal::SecureStringToBSTR( $sstr ) | |
$str = $marshal::PtrToStringBSTR( $ptr ) | |
$marshal::ZeroFreeBSTR( $ptr ) | |
$str | |
} | |
function read($prompt=$null, $sr=$null) { | |
$result; | |
$command = "Read-Host" | |
if ($prompt) { | |
$command += " -Prompt " + $prompt.ToString() | |
} | |
if ($sr) { | |
$command += " -AsSecureString" | |
} | |
$result = Invoke-Expression $command | |
if ($sr) { | |
$result = $result | Decrypt-SecureString | |
Set-EnvironmentVariable $sr $result | Out-Null | |
} | |
else { | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment