Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions 101-automation-runbook-getvms/DeployThroughARM.ps1
Original file line number Diff line number Diff line change
@@ -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

18 changes: 18 additions & 0 deletions 101-automation-runbook-getvms/README.md
Original file line number Diff line number Diff line change
@@ -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.
39 changes: 39 additions & 0 deletions 101-automation-runbook-getvms/Runbooks/Get-AzureVMTutorial.ps1
Original file line number Diff line number Diff line change
@@ -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]
}
}
20 changes: 20 additions & 0 deletions 101-automation-runbook-getvms/azuredeploy-parameters.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
125 changes: 125 additions & 0 deletions 101-automation-runbook-getvms/azuredeploy.json
Original file line number Diff line number Diff line change
@@ -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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This url should be updated to this repos url once the PR is merged

"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": {}
}
7 changes: 7 additions & 0 deletions 101-automation-runbook-getvms/metadata.json
Original file line number Diff line number Diff line change
@@ -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"
}