Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Azure Automation: Leveraging Runbooks
param (
[Parameter(Mandatory=$true)]
[string]
$environment
)
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID `
-ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
# Constants
$Location = "East US 2"
# Varibales by Environment
$admin_rg = @{dev = "admin-dev-rg"; test = "admin-test-rg"; prod = "admin-prod-rg"}
$storage_rg = @{dev = "storage-dev-rg"; test = "storage-test-rg"; prod = "storage-prod-rg"}
$engine_rg = @{dev = "engine-dev-rg"; test = "engine-test-rg"; prod = "engine-prod-rg"}
$VNetName = @{dev = "dev-vnet"; test = "test-vnet"; prod = "prod-vnet"}
# Create 'Admin' resource group
Get-AzureRmResourceGroup -Name $admin_rg.Get_Item($environment) -ev notPresent -ea 0
if ($notPresent)
{
New-AzureRmResourceGroup -Name $admin_rg.Get_Item($environment) -Location "East US 2"
}
else
{
Write-Output "Resource Group '$($admin_rg.Get_Item($environment))' already exists.";
}
# Create 'Storage' resource group
Get-AzureRmResourceGroup -Name $storage_rg.Get_Item($environment) -ev notPresent -ea 0
if ($notPresent)
{
New-AzureRmResourceGroup -Name $storage_rg.Get_Item($environment) -Location "East US 2"
}
else
{
Write-Output "Resource Group '$($storage_rg.Get_Item($environment))' already exists.";
}
# Create 'Engine' resource group
Get-AzureRmResourceGroup -Name $engine_rg.Get_Item($environment) -ev notPresent -ea 0
if ($notPresent)
{
New-AzureRmResourceGroup -Name $engine_rg.Get_Item($environment) -Location "East US 2"
}
else
{
Write-Output "Resource Group '$($engine_rg.Get_Item($environment))' already exists.";
}
# Create Virtual Network (VNet)
$VNet = Get-AzureRmVirtualNetwork `
-Name $VNetName.Get_Item($environment) `
-ResourceGroupName $admin_rg.Get_Item($environment) `
-ErrorAction SilentlyContinue
if (!$VNet)
{
Write-Host "The VNet '$($VNetName.Get_Item($environment))' does not exists. Creating it.";
$VNet=New-AzureRmVirtualNetwork `
-ResourceGroupName $admin_rg.Get_Item($environment) `
-Location $Location `
-Name $VNetName.Get_Item($environment) `
-AddressPrefix 10.10.0.0/16
}
else
{
Write-Output "Using existing Virtual Network (VNet) '$($VNetName.Get_Item($environment))'.";
}
param (
[Parameter(Mandatory=$true)]
[string]
$environment
)
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID `
-ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
$adminParams = @{"environment" = $environment}
$storageParams = @{"environment" = $environment; "storageName" = "enginesa01"}
Get-AzureRmSubscription –SubscriptionName "Pay-As-You-Go" | Select-AzureRmSubscription
Start-AzureRmAutomationRunbook -AutomationAccountName "automationBlog" `
-ResourceGroupName "automationBlog" `
-Parameters $adminParams `
-Name "create_rg" `
-wait
Start-AzureRmAutomationRunbook -AutomationAccountName "automationBlog" `
-ResourceGroupName "automationBlog" `
-Parameters $storageParams `
-Name "storage"
-wait
param (
[Parameter(Mandatory=$true)]
[string]
$environment,
[Parameter(Mandatory=$true)]
[string]
$storageName
)
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID `
-ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
# Constants
$location = "East US 2"
$storageType = "Standard_LRS"
# Variables by environment
$storage_rg = @{dev = "storage-dev-rg"; test = "storage-test-rg"; prod = "storage-prod-rg"}
# Create storage account
$storageAccount = Get-AzureRmStorageAccount `
-ResourceGroupName $storage_rg.Get_Item($environment) `
-AccountName $storageName `
-ErrorAction SilentlyContinue
if (!$StorageAccount)
{
Write-Output "Storage Account '$($storageName)' does not exist. Creating it.";
$storageAccount = New-AzureRmStorageAccount `
-ResourceGroupName $storage_rg.Get_Item($environment) `
-Name $storageName `
-Type $storageType `
-Location $location
Write-Output "Storage Account Created";
}
else
{
Write-Output "Using exisiting storage account '$($storageName)'";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment