Core/ServerRake.Core.Utility.ps1

#################
### UTILITY ###
#################

function Send-RemoteScript {

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string[]] $ComputerName,

        [Parameter(Mandatory = $true)]
        [scriptblock] $RemoteScript,

        [Parameter(Mandatory = $false)]
        $ArgumentList
    )

    if (!$secret) {

        throw "No secret sessions are currently opened. Start a session with 'Open-SecretSession' and try again."
    
    }

    #$script = $RemoteScript | Out-String

    if ($true -eq $doublehop) {

        Write-Host "Running command from jumphost." -ForegroundColor Yellow

        $payload = Invoke-Command -Session $jh -ArgumentList $ComputerName, $RemoteScript, $argumentList, $secret -ScriptBlock {

            param ($ComputerName, $RemoteScript, $argumentList, $secret)

            $responses = @()
            $RemoteScript = [scriptblock]::Create($RemoteScript)

            $responses += Invoke-Command -ComputerName $ComputerName -ScriptBlock $RemoteScript -ArgumentList $argumentList -Credential $secret

            return $responses

        }

        return $payload

    } else {

        $responses = @()
        #$script = [scriptblock]::Create($script)

        $responses += Invoke-Command -ComputerName $ComputerName -ScriptBlock $RemoteScript -ArgumentList $argumentList -Credential $secret

        return $responses

    }
    
}