diff --git a/101-automation-runbook-getvms/DeployThroughARM.ps1 b/101-automation-runbook-getvms/DeployThroughARM.ps1 new file mode 100644 index 000000000000..d5ad8b936bb1 --- /dev/null +++ b/101-automation-runbook-getvms/DeployThroughARM.ps1 @@ -0,0 +1,27 @@ + +#Connect to your Azure account +Add-AzureAccount + +#Select your subscription if you have more than one +#Select-AzureSubscription -SubscriptionName "My Subscription Name" + +#Create a GUID for the job +$JobGUID = [System.Guid]::NewGuid().toString() + +#Use Azure resource Manager to deploy template +Switch-AzureMode -Name AzureResourceManager + +#Set the parameter values for the template +$Params = @{ + "accountName" = "MyAccount" ; + "jobId" = $JobGUID; + "regionId" = "Japan East"; + "credentialName" = "DefaultAzureCredential"; + "userName" = "MyUserName"; + "password" = "MyPassword" +} + +$TemplateURI = "https://raw.githubusercontent.com/azureautomation/resources/master/automation-packs/101-get-vm-tutorial/deployAutomationResources.json" + +New-AzureResourceGroupDeployment -TemplateParameterObject $Params -ResourceGroupName "MyResourceGroup" -TemplateUri $TemplateURI + diff --git a/101-automation-runbook-getvms/README.md b/101-automation-runbook-getvms/README.md new file mode 100644 index 000000000000..dcab6ba12066 --- /dev/null +++ b/101-automation-runbook-getvms/README.md @@ -0,0 +1,18 @@ +# Create VM tutorial runbook, Automation credential, and start a job + +This sample shows how you can deploy a runbook, a credential the runbook relies on, and how to start a job through an Azure Resource Manager template. + +It contains a sample script, Deploy-ThroughARM, that you can use to get you started with the deployment. + +##Resources Deployed +###Automation Account +This is the account that will contain your credentials. If you want to deploy to an existing account, make sure that the Resource Group, region, tags, and SKU in the template are all the same as your existing account, otherwise the properties will be overwritten. + +###Runbook +The runbook provides an example of how you can authenticate to Azure and use Azure cmdlets in a runbook. It uses an Azure AD organizational ID to connect to Azure. It then prints out the first 10 VMs in your account. + +###Credential +The credential should contain the username and password of the Azure AD organizalation ID to connect to Azure. To learn about how to create this user, see [Get set up to automate Azure]("http://aka.ms/getsetuptoautomate") and check out this blog post [Authenticating to Azure using Active Directory]("http://azure.microsoft.com/blog/2014/08/27/azure-automation-authenticating-to-azure-using-azure-active-directory/"). + +###Job +A job will be triggered once the other resources are deployed. The job needs a unique GUID as the jobId. You can use this to identify the job later in your script and to retrieve the job output. diff --git a/101-automation-runbook-getvms/Runbooks/Get-AzureVMTutorial.ps1 b/101-automation-runbook-getvms/Runbooks/Get-AzureVMTutorial.ps1 new file mode 100644 index 000000000000..c1513fa99663 --- /dev/null +++ b/101-automation-runbook-getvms/Runbooks/Get-AzureVMTutorial.ps1 @@ -0,0 +1,39 @@ +<# + .DESCRIPTION + An example runbook which prints out the first 10 Azure VMs in your subscription (ordered alphabetically). + For more information about how this runbook authenticates to your Azure subscription, see our documentation here: http://aka.ms/fxu3mn + + .NOTES + AUTHOR: Azure Automation Team + LASTEDIT: Mar 27, 2015 +#> +workflow Get-AzureVMTutorial +{ + #The name of the Automation Credential Asset this runbook will use to authenticate to Azure. + $CredentialAssetName = 'DefaultAzureCredential' + + #Get the credential with the above name from the Automation Asset store + $Cred = Get-AutomationPSCredential -Name $CredentialAssetName + if(!$Cred) { + Throw "Could not find an Automation Credential Asset named '${CredentialAssetName}'. Make sure you have created one in this Automation Account." + } + + #Connect to your Azure Account + $Account = Add-AzureAccount -Credential $Cred + if(!$Account) { + Throw "Could not authenticate to Azure using the credential asset '${CredentialAssetName}'. Make sure the user name and password are correct." + } + + #TODO (optional): pick the right subscription to use. Without this line, the default subscription for your Azure Account will be used. + #Select-AzureSubscription -SubscriptionName "TODO: your Azure subscription name here" + + #Get all the VMs you have in your Azure subscription + $VMs = Get-AzureVM + + #Print out up to 10 of those VMs + if(!$VMs) { + Write-Output "No VMs were found in your subscription." + } else { + Write-Output $VMs[0..9] + } +} \ No newline at end of file diff --git a/101-automation-runbook-getvms/azuredeploy-parameters.json b/101-automation-runbook-getvms/azuredeploy-parameters.json new file mode 100644 index 000000000000..fdda9d1ada60 --- /dev/null +++ b/101-automation-runbook-getvms/azuredeploy-parameters.json @@ -0,0 +1,20 @@ +{ + "accountName": { + "value": "MyAutomationAccount" + }, + "jobId": { + "value": "e6abf1fd-6311-4442-ac1a-22aae3cacdae" + }, + "regionId": { + "value": "East US 2" + }, + "credentialName": { + "value": "DefaultAzureCredential" + }, + "userName": { + "value": "MyAzureADUser" + }, + "password": { + "value": "MyAzureADPassword" + } +} \ No newline at end of file diff --git a/101-automation-runbook-getvms/azuredeploy.json b/101-automation-runbook-getvms/azuredeploy.json new file mode 100644 index 000000000000..a3c7d9261437 --- /dev/null +++ b/101-automation-runbook-getvms/azuredeploy.json @@ -0,0 +1,125 @@ +{ + "$schema": "http://schemas.microsoft.org/azure/deploymentTemplate?api-version=2015-01-01-preview#", + "contentVersion": "1.0", + "parameters": { + "accountName": { + "type": "string", + "metadata": { + "description": "The name of the Azure Automation account to deploy to." + } + }, + "regionId": { + "type": "string", + "allowedValues": [ + "Japan East", + "East US 2", + "West Europe", + "Southeast Asia", + "South Central US" + ], + "metadata": { + "description": "The region to deploy the Automation account in." + } + }, + "credentialName": { + "type": "string", + "metadata": { + "description": "DefaultAzureCredential is the name of the Automation credential used in this runbook. This credential allows you to authenticate to Azure. " + } + }, + "userName": { + "type": "string", + "metadata": { + "description": "The username for the Azure Automation credential." + } + }, + "password": { + "type": "string", + "metadata": { + "description": "The password for the Azure Automation credential." + } + }, + "jobId": { + "type": "string", + "metadata": { + "description": "The GUID for the runbook job to be started." + } + } + }, + "variables": { + "runbookName": "Get-AzureVMTutorial", + "scriptUri": "https://raw.githubusercontent.com/azureautomation/resources/master/automation-packs/101-get-vm-tutorial/Get-AzureVMTutorial.ps1", + "runbookDescription": "Authenticates to Azure and lists all the Azure V1 VMs", + "sku": "Free" + }, + "resources": [ + { + "name": "[parameters('accountName')]", + "type": "Microsoft.Automation/automationAccounts", + "apiVersion": "2015-01-01-preview", + "location": "[parameters('regionId')]", + "dependsOn": [], + "tags": {}, + "properties": { + "sku": { + "name": "[variables('sku')]" + } + }, + "resources": [ + { + "name": "[variables('runbookName')]", + "type": "runbooks", + "apiVersion": "2015-01-01-preview", + "location": "[parameters('regionId')]", + "dependsOn": [ + "[concat('Microsoft.Automation/automationAccounts/', parameters('accountName'))]" + ], + "tags": {}, + "properties": { + "runbookType": "Script", + "logProgress": "false", + "logVerbose": "false", + "description": "[variables('runbookDescription')]", + "publishContentLink": { + "uri": "[variables('scriptUri')]", + "version": "1.0.0.0" + } + } + }, + { + "name": "[parameters('credentialName')]", + "type": "credentials", + "apiVersion": "2015-01-01-preview", + "location": "[parameters('regionId')]", + "dependsOn": [ + "[concat('Microsoft.Automation/automationAccounts/', parameters('accountName'))]" + ], + "tags": {}, + "properties": { + "userName": "[parameters('userName')]", + "password": "[parameters('password')]" + } + }, + { + "name": "[parameters('jobId')]", + "type": "jobs", + "apiVersion": "2015-01-01-preview", + "location": "[parameters('regionId')]", + "dependsOn": [ + "[concat('Microsoft.Automation/automationAccounts/', parameters('accountName'))]", + "[concat('Microsoft.Automation/automationAccounts/', parameters('accountName'), '/runbooks/',variables('runbookName'))]" + ], + "tags": { + "key": "value" + }, + "properties": { + "runbook": { + "name": "[variables('runbookName')]" + } + } + } + ] + } + ], + "outputs": {} +} \ No newline at end of file diff --git a/101-automation-runbook-getvms/metadata.json b/101-automation-runbook-getvms/metadata.json new file mode 100644 index 000000000000..7d6a4109fdf2 --- /dev/null +++ b/101-automation-runbook-getvms/metadata.json @@ -0,0 +1,7 @@ +{ + "itemDisplayName": "Create Azure Automation Runbook to retrieve Azure VMs", + "description": "This template creates an Azure Automation runbook that retrieves Azure V1 VMs and a credential to authenticate to Azure. It then starts the runbook job.", + "summary": "This template creates an Azure Automation runbook and credential. It then starts the runbook job.", + "githubUsername": "epc2101", + "dateUpdated": "2015-06-16" +} \ No newline at end of file