Skip to content

Instantly share code, notes, and snippets.

@TonyAbell
Last active July 13, 2016 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TonyAbell/2cdfc220cf5fdf5ba47594aaa415ccfa to your computer and use it in GitHub Desktop.
Save TonyAbell/2cdfc220cf5fdf5ba47594aaa415ccfa to your computer and use it in GitHub Desktop.
It is not always possible to a Azure ML Workspace in the US, and the storage account is in a different resource group. This script will create a storage acount and an Azure ML WorkSpace in 'South Central US'
Login-AzureRmAccount;
# update values below
$subId = "{ subscription guid }"
$storageAccountName="{ storage account name }"
$azMLOwnerId="{ account email }"
$azMLWorkSpaceName="{ azure ml workspace name }"
# default values should work below
$resourceGroupLocation = "South Central US"
$resourceGroupName = "Default-MachineLearning-SouthCentralUS" #keep this value so the storage account is in the ML RG
New-AzureRmResourceGroup -Name $resourceGroupName -Location $resourceGroupLocation -Force
New-AzureRmStorageAccount -ResourceGroupName $resourceGroupName -Location $resourceGroupLocation -Name $storageAccountName -Type Standard_LRS
#Get Azure AAD auth token
$clientId = "1950a258-227b-4e31-a9cf-717495945fc2"
$redirectUri = "urn:ietf:wg:oauth:2.0:oob"
$resourceClientId = "00000002-0000-0000-c000-000000000000"
$resourceAppIdURI = "https://management.core.windows.net/"
$authority = "https://login.windows.net/common"
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority,$false
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Auto")
$header = $authresult.CreateAuthorizationHeader()
$tenants = Invoke-RestMethod -Method GET -Uri "https://management.azure.com/tenants?api-version=2014-04-01" -Headers @{"Authorization"=$header} -ContentType "application/json"
$tenant = $tenants.value.tenantId
$authority = [System.String]::Format("https://login.windows.net/{0}", $tenant)
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority,$false
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Auto")
$header = $authresult.CreateAuthorizationHeader()
$storageAccount = Get-AzureRmResource -ResourceType Microsoft.Storage/storageAccounts -ResourceName $storageAccountName -ResourceGroupName $resourceGroupName
$storageAccountKeysUrl = [System.String]::Format("https://management.azure.com/subscriptions/{0}/resourcegroups/{1}/providers/Microsoft.Storage/storageAccounts/{2}/listKeys?api-version=2016-01-01", $sub.SubscriptionId, $resourceGroupName, $storageAccountName)
$storageAccountKeys = Invoke-RestMethod -Method POST -Uri $storageAccountKeysUrl -Headers @{"Authorization"=$header} -ContentType "application/json"
$storageAccountKey=(($storageAccountKeys.keys).Item(0).value)
$azMLWorkSpaceGuid="e582920d010646acbb0ec3183dc2243a" # this is a valid defulat value, I new workspace guid will be created
$azMgmtApiBaseUrl = "https://management.core.windows.net/{0}/cloudservices/amlsdk/resources/machinelearning/~/workspaces/{1}" -f $subId, $azMLWorkSpaceGuid
$azMLHeader = @{ "Authorization"=$header
"x-ms-version"="2014-10-01"
}
$azMLBody = ConvertTo-Json @{
"Name" = $azMLWorkSpaceName
"Location" = "South Central US"
"StorageAccountName" = $storageAccountName
"StorageAccountKey" = $storageAccountKey
"OwnerId" = $azMLOwnerId
"ImmediateActivation" = "true"
"Source" = "Default"
}
Invoke-RestMethod -Method PUT -Uri $azMgmtApiBaseUrl -Headers $azMLHeader -ContentType "application/json" -Body $azMLBody
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment