-
Notifications
You must be signed in to change notification settings - Fork 601
Expand file tree
/
Copy pathGeneratePowershellCommand.ps1
More file actions
56 lines (42 loc) · 1.74 KB
/
GeneratePowershellCommand.ps1
File metadata and controls
56 lines (42 loc) · 1.74 KB
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
### Takes a file path to an artifactFile and spits out a parameterized powerhsell command
param (
[Parameter(Mandatory=$true, HelpMessage="The path of an artifactFile")]
[string] $file
)
if ($false -eq (Test-Path $file)) {
throw "File does not exist '$file'"
}
$json = Get-Content -Path $file -Raw
$config = ConvertFrom-Json $json
$parameters = $config.parameters
$command = "./your-file.ps1 "
foreach($property in $parameters.PSObject.Properties)
{
# Access the name of the property
$parameterName = $property.Name
if ([string]::IsNullOrEmpty($parameterName)) {
throw "Missing name property on a parameter"
}
# Access the value of the property
$parameter = $property.Value
$type = $parameter.type
if ([string]::IsNullOrEmpty($type)) {
throw "Missing type property on parameter '$parameterName'"
}
if ($type -eq "string") {
$result = "-$parameterName ''', parameters('$parameterName'), ''' "
} elseif ($type -eq 'int') {
$result = "-$parameterName ', parameters('$parameterName'), ' "
} elseif ($type -eq "securestring") {
$result = "-$parameterName `$(if (`$false -eq [string]::IsNullOrEmpty(''', parameters('$parameterName'), ''')) { (ConvertTo-SecureString ''', parameters('$parameterName'), ''' -AsPlainText -Force) } else { `$null }) "
} elseif ($type -eq "bool") {
$result = "-" + $parameterName + ":$', parameters('$parameterName'), ' "
} else {
throw "parameter type not supported '$type' for '$parameterName'"
}
$command += $result
}
$prefix = '[concat(''powershell.exe -ExecutionPolicy bypass \"'
$postfix = '\"'')]'
$result = $prefix + $command + $postfix
Write-Host $result