Public/Invoke-Command.ps1

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
function Invoke-Command {

    <#

    .SYNOPSIS
    Executes a R command

    .DESCRIPTION
    Executes Rscript.exe and returns the output.

    .PARAMETER Command
    Specifies the R command to be executes.

    .PARAMETER Library
    Specifies if a additinal package library should be included.

    .OUTPUTS
    string[]

    .EXAMPLE
    PS C:\> Set-RScriptPath -Path 'C:\Program Files\Microsoft\R Open\R-3.5.2\bin\x64\Rscript.exe'
    PS C:\> Invoke-RCommand 'hello world'
    hello world

    #>


    [CmdletBinding()]
    param (
        [Parameter( Mandatory )]
        [ValidateNotNullOrEmpty()]
        [string[]] $Command,

        [ValidateScript( { Test-Path $_ -PathType Container })]
        [string] $Library
    )

    $commands = @()

    if ( $Library ) {
        $commands += @( Get-AddLibraryCommand $Library )
    }

    $commands += $Command

    Invoke-RScript $commands -Timeout $null -ParseOutput | ForEach-Object { $_.Trim('"') }
}