AzureML.psm1

function Set-AzureMLWebServiceConnection
{
    PARAM(
        [Parameter(Mandatory=$True)]
        [string]$URI,
        [Parameter(Mandatory=$True)]
        [string]$APIKey
    )
    $global:AzureMLURI=$URI
    $global:AzureMLAPIKey=$APIKey
}

function Get-AzureMLPrediction
{
    PARAM(
        [Parameter(Mandatory=$True)]
        [array]$QueryArray
    )
    if (($AzureMLURI+$AzureMLAPIKey) -eq "")
    {
        write-error("Error: Webserice adress and/or API-Key unknown. Please use Set-AzureMLWebServiceConnection bevor.")
    } else
    {
        $webServiceInput = "input1"
        #Building request
        $requestP1="{`n`t""Inputs"": {`n`t`t""input1"": {`n`t`t`t""ColumnNames"": ["
        $requestP2=""
        $requestP3="`n`t`t`t],`n`t`t`t""Values"": [`n`t`t`t`t["
        $requestP4=""
        $requestP5="`n`t`t`t`t]`n`t`t`t]`n`t`t}`n`t},`n`t""GlobalParameters"": {}`n}"
        foreach ($v in $QueryArray) 
        {
            $key=$v[0]
            $value=$v[1]
            $requestP2=$requestP2+"`n`t`t`t`t"""+$key+""","
            $requestP4=$requestP4+"`n`t`t`t`t"""+$value+""","
        }
        $requestP2=$requestP2.Substring(0,$requestP2.length-1)
        $requestP4=$requestP4.Substring(0,$requestP4.length-1)
        $request=$requestP1+$requestP2+$requestP3+$requestP4+$requestP5
        write-verbose ("Azure Machine Learning request:")
        write-verbose($request+"`n")
        #Do request
        $webRequest=Invoke-WebRequest -Method Post -Uri $AzureMLURI -Header @{ Authorization = "BEARER "+$AzureMLAPIKey} -ContentType "application/json" -Body $request        
        #Get response
        $response=$webRequest.Content
        write-verbose("Azure Machine Learning response:")
        write-verbose($response+"`n")
        #Fix response to be convertable to json-object
        $response=$response -replace "\[\[","["
        $response=$response -replace "\]\]","]"
        #Convert and parse response
        $responseObject = ConvertFrom-Json $response
        $output = New-Object PSObject
        for ($i=0;$i -lt $responseObject.Results.output1.value.ColumnNames.Count;$i++)
        {
            $a=$responseObject.Results.output1.value.ColumnNames[$i]
            $b=$responseObject.Results.output1.value.ColumnTypes[$i]
            $c=$responseObject.Results.output1.value.Values[$i]
            #Build custom object with true types
            switch ($b) 
            { 
                "double"    {$output | add-member Noteproperty $a ([double]$c)}
                "int"        {$output | add-member Noteproperty $a ([int]$c)}
                "long"        {$output | add-member Noteproperty $a ([long]$c)}
                "datetime"    {$output | add-member Noteproperty $a ([datetime]$c)}
                "Boolean"    {$output | add-member Noteproperty $a ([boolean]$c)}
                "Int16"        {$output | add-member Noteproperty $a ([int16]$c)}
                "Int32"        {$output | add-member Noteproperty $a ([int32]$c)}
                "Int64"        {$output | add-member Noteproperty $a ([int64]$c)}
                "Single"    {$output | add-member Noteproperty $a ([single]$c)}
                "Byte"        {$output | add-member Noteproperty $a ([byte]$c)}
                default        {$output | add-member Noteproperty $a ($c)}
            }
        }
        write-verbose("Return values:")
        if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent)
        {
            $output|fl
        }
        return $output
    }
}