Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Powershell Command Utility #662

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,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