Skip to content

Instantly share code, notes, and snippets.

@atruskie
Last active February 28, 2017 06:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atruskie/4f53dc7f0c28747d937e to your computer and use it in GitHub Desktop.
Save atruskie/4f53dc7f0c28747d937e to your computer and use it in GitHub Desktop.
Mock the bash export function in powershell
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