Public/New-LimeSession.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
function New-LimeSession {
    <#
    .Synopsis
        Connects to LimeSurvey Server and returns a Session Key.
    .DESCRIPTION
        Connects to LimeSurvey Server and returns a Session Key so that other LimeSurvey-XML commands can be authenticated when using the -Session paramenter
    .PARAMETER Credential
        A credential to use for LimeSurvey.
    .PARAMETER BaseUri
        Base URI for LimeSurvey.
    .EXAMPLE
        $MyLimeSession = New-LimeSession -Credential (Get-Credential) -BaseUri "https://host.com/limesurvey/index.php?r=admin/remotecontrol"
    .OUTPUTS
        Microsoft.PowerShell.System.String
    .FUNCTIONALITY
        Lime Survey
    #>

    [cmdletbinding()]
    Param (
        [Parameter( ValueFromPipeLine = $true,
            ValueFromPipelineByPropertyName = $true )]
        [ValidateNotNull()]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential,

        [ValidateNotNull()]
        [string]$BaseUri
    )
    Write-Verbose "Creating Lime Session"
    $RpcMethodBody = New-RPCMethod 'get_session_key' @($Credential.UserName, $Credential.GetNetworkCredential().Password)
    Write-Verbose "Sending Lime Session credentials for [$($Credential.UserName)]"
    $SessionKey = Invoke-RpcMethod -RpcServerUri $BaseUri -RequestBody $RpcMethodBody
    # if successful, returned SessionKey should be a 32-charlength alphanumeric key
    if ($SessionKey -match "^(?=.{32}$)[a-zA-Z0-9]*$") {
        $SessionKey
    }
    else {
        Write-Error $SessionKey
    }

}