-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-EnvironmentVariables.psm1
84 lines (64 loc) · 4.17 KB
/
load-EnvironmentVariables.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<#
.SYNPOSIS
Utilities for working with environment variables in powershell
#>
function Set-EnvironmentVariable
{
<#
#>
param(
[Parameter(Position=0)][String] $variable,
[Parameter(Position=1)][String] $value,
[String] $defaultValue=[String]::Empty)
if([String]::IsNullOrEmpty( $value))
{
$value=$defaultValue;
}
Set-Content -Path "env:\$variable" -Value $value
}
function Set-EnvironmentVariablesFromHashTable
{
<#
.SYNOPSIS
Sets environment variables from a hashtable
.EXAMPLE
Set-EnvironmentVariablesFromHashTable @{x_coffee="bitter";x_apple="sweet"}
#>
param([Parameter(Position=0)][System.Collections.Hashtable] $hashTableWithVariables)
if($hashTableWithVariables -eq $Null -or $hashTableWithVariables.Count -eq 0)
{
return;
}
foreach ($key in $hashTableWithVariables.Keys)
{
Set-EnvironmentVariable $key $hashTableWithVariables.get_Item($key)
}
}
function invoke-cmdScript
{
<#
.SYNOPSIS
Load environment variables from a command script
.DESCRIPTION
Poached from OReilly - Windows PowerShell Cookbook, http://oreilly.com/catalog/9780596528492/
Minor modifications
.EXAMPLE
invoke-cmdScript -script "BatchInteractionWithPowershell.bat" -parameters "CAT DOG"
Note that you must figure out the quoting of arguments for yourself.
#>
param([string] $script, [string] $parameters)
$tempFile = [IO.Path]::GetTempFileName()
$useFile=(Resolve-Path $script).Path;
## Store the output of cmd.exe. We also ask cmd.exe to output
## the environment table after the batch file completes
cmd /c " `"$useFile`" $parameters && set > `"$tempFile`" "
## Go through the environment variables in the temp file.
## For each of them, set the variable in our local environment.
Get-Content $tempFile | Foreach-Object {
if($_ -match "^(.*?)=(.*)$")
{
Set-Content "env:\$($matches[1])" $matches[2]
}
}
Remove-Item $tempFile
}