AzureML.psm1
# Azure Machine Learning access module # Marcel Meurer, Twitter: MarcelMeurer # Tutorial: https://www.sepago.com/blog/2015/11/30/zugriff-mit-powershell-auf-azure-machine-learning-api-azureml function Set-AzureMLWebServiceConnection { <# .DESCRIPTION Set the authentication property and the uri of the prediction web service for Get-AzureMLPrediction. You can also use -URI and -APIKey as parameter with Get-AzureMLPrediction module version 1.0.1 or higher .PARAMETER URI URI of the prediction web service .PARAMETER APIKey API key to access the web service (authorization bearer) .EXAMPLE Set-AzureMLWebServiceConnection -URI "https://europewest.services.azureml.net/workspaces/017690d3ae8c55829bb02f4c5adc5418/services/97ec99a0d9944bfeacfb33f7d7bbea2c/execute?api-version=2.0&details=true" -APIKey "/KYD/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX==" .NOTES Author: Marcel Meurer, marcel.meurer@sepago.de, Twitter: MarcelMeurer #> PARAM( [Parameter(Mandatory=$True)] [string]$URI, [Parameter(Mandatory=$True)] [string]$APIKey ) $global:AzureMLURI=$URI $global:AzureMLAPIKey=$APIKey } function Get-AzureMLPrediction { <# .DESCRIPTION Connects to the prediction web API and get the results of the prediction. For an example go to https://www.sepago.com/blog/2015/11/30/zugriff-mit-powershell-auf-azure-machine-learning-api-azureml .PARAMETER QueryArray Input vector (2 dimensions) for the prediction. E.g.: $VectorI=@("t","0","t-1",15,"t-2",14,"t-3",15,"t-4",11) .PARAMETER URI URI of the prediction web service .PARAMETER APIKey API key to access the web service (authorization bearer) .EXAMPLE $Prediction=Get-AzureMLPrediction -QueryArray $QA -URI $URI -APIKey $APIKey Get the prediction for the given input vector and cast the results to the prediction object $Prediction. .NOTES Author: Marcel Meurer, marcel.meurer@sepago.de, Twitter: MarcelMeurer #> PARAM( [Parameter(Mandatory=$True)] [array]$QueryArray, [string]$URI, [string]$APIKey ) if (!$URI -eq "") { $AzureMLURI=$URI } if (!$APIKey -eq "") { $AzureMLAPIKey=$APIKey } 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 } } |