Functions/commands.ps1

function Invoke-SimpleCommands(
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [object[]]$commands,
    [string[]]$excludeList) {
    foreach ($command in ($commands | Where-Object {($_.PSObject.Properties.Name -CNotIn $excludeList)})) {
        Invoke-SimpleCommand $command
    }
}

function Invoke-SimpleCommand(
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [object]$command) {
    if ($command -isnot [string]) {
        $cmdName = $command.PSObject.Properties.Name
        $cmdExec = $command.PSObject.Properties.Value
        Write-Host "Execute command '$cmdName': '$cmdExec'..." -ForegroundColor Cyan
    }
    else {
        $cmdExec = $command
        Write-Host "Execute command '$cmdExec'..." -ForegroundColor Cyan
    }
    Invoke-Expression $cmdExec
}

function Install-ConfigSection(
    [Parameter(Mandatory = $true)]
    [object]$options,
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [string]$sectionName,
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [string]$PropertyCommandListName = "commands"
) {
    if (!$options -or !$options.commandName -or $options.ignore) {
        Write-Warning "${sectionName}: 'options' or 'commandName' not found or section is 'ignored'. Skipped"
        return
    }

    if ($options.$PropertyCommandListName -and $options.$PropertyCommandListName.Count -gt 0) {
        $command = "$($options.commandName) $($options.prefix) "
        foreach ($package in $options.$PropertyCommandListName) {
            $command += "${package} "
        }
        Invoke-SimpleCommand $command
    }
    else {
        Write-Host "${sectionName}: ${PropertyCommandListName} not found!"
    }
}

function Invoke-PowerShellScript(
    [Parameter(Mandatory = $true, Position = 0)]
    [ValidateNotNullOrEmpty()]
    [string]$script,

    [AllowNull()]
    [AllowEmptyString()]
    [Parameter(Position = 1)]
    [string[]]$scriptParams = @()) {
    $params = Convert-ArrayToStringParams $scriptParams
    Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0} " {1}' -f $script, $params)
}