次の方法で共有


Deploying Virtual Machines in Azure using Service Manager and SMA Part II

This is the 2nd post in a 3-part series on deploying VMs in Azure using System Center Service Manager and Service Management Automation (SMA). In this post we’ll cover deploying your Azure VM using PowerShell and SMA.

Part I: Preparing your VM for upload, uploading it to Azure, and configuring Azure

Part II: Deploying your VM in PowerShell and SMA

Part III: Deploying your VM using the Service Manager Self-Service Portal

 

Summary

The following steps will take you through using PowerShell and SMA to deploy the VM we uploaded in Part I. Each section below will be an argument passed to the PowerShell script and/or SMA Runbook.

Download the Azure Settings File (azureSettingsFile)

  • In order for scripts to connect to Azure we need to download an Azure settings file for authentication. You can download this file from the following URL: https://windows.azure.com/download/publishprofile.aspx.
  • Note the full path that you downloaded the file. In my testing the file needed to be local where I ran the script so you’ll need it where you’re running the PowerShell script in addition to the server where your SMA Runbook will execute.

Get the name of the VM Image you plan on using (imageName)

  • In the Azure Portal, go under Virtual Machines\Images
  • I will use w2012r2base3

image

Get your storage account name where your VHD is stored (azureStorageAccountName)

  • In the Azure Portal, go under Storage
  • I will use rslaten

image

Get the name of the Affinity Group you plan on using (affinityGroup)

  • In the Azure Portal, go under Settings\Affinity Groups
  • I will use Lab

image

Get the name of the Network you plan on using (networkName)

  • In the Azure Portal, go under Networks
  • Mine is called Lab

image

Get the name of the Subnet you plan on using (subnetName)

  • In the Azure Portal, go under Networks\<Your Network>\Configure
  • I will use subnet-1

image

Get the following additional information

  • azureAccountName: The Microsoft Account name you use to logon to Azure
  • adminUserName: The local administrator account you want on the VM
  • adminPassword: The local administrator account password you want on the VM
  • domain: The FQDN of the domain you want to join
  • domainUserName: The domain user to use to join the domain (without the domain\ in front of the user)
  • domainPassword: The password for the user account above
  • vmName: The name you want the VM to be called. The script will add an integer to the end and keep incrementing it as necessary.
  • instanceSize: A0 – A9, see https://msdn.microsoft.com/en-us/library/azure/dn197896.aspx for more information.

Run the script on a machine with Azure PowerShell installed using the following syntax

.\DeployAzureVM.ps1 -azureSettingsFile c:\temp\myAzureSettings.publishsettings –azureAccountName myAzureAccount@myemailaccount.com -imageName w2012r2base3 -vmName rslatenTest -instanceSize A0 -adminUserName rslaten –adminPassword <‘P@$SW0Rd1’> -domain contoso.com -domainUserName rslaten -domainPassword <‘P@$SW0Rd1’> -affinityGroup Lab -networkName Lab -azureStorageAccountName rslaten -subnetName Subnet-1

 param(
  $azureSettingsFile,
  $azureAccountName,
  $imageName,
  $vmName,
  $instanceSize,
  $adminUserName,
  $adminPassword,
  $domain,
  $domainUserName,
  $domainPassword,
  $affinityGroup,
  $networkName,
  $azureStorageAccountName,
  $subnetName
)

#Set Instance Size
if ($instanceSize -eq "A0") { $instanceSize = 'ExtraSmall' }
elseif ($instanceSize -eq "A1") { $instanceSize = 'Small' }
elseif ($instanceSize -eq "A2") { $instanceSize = 'Medium' }
elseif ($instanceSize -eq "A3") { $instanceSize = 'Large' }
elseif ($instanceSize -eq "A4") { $instanceSize = 'ExtraLarge' }

#Import Azure PowerShell Module
Import-Module Azure

#Remove Existing Azure Sessions
Remove-AzureAccount -Name $azureAccountName -Force

#Import Azure Settings File
Import-AzurePublishSettingsFile -PublishSettingsFile $azureSettingsFile

#Set Azure Subscription
Get-AzureSubscription | Set-AzureSubscription -currentstorageaccountname $azureStorageAccountName

#Increment VM Name
$i = 0
foreach ($vm in Get-AzureVM)
{
  if ($vm.InstanceName.Contains($vmName))
  {
    [int]$increment = $vm.InstanceName.SubString($vmName.Length)
    if ($increment -gt $i) { $i = $increment }
  }
}
$i++
$vmName = $vmName + $i

#Create New Azure Cloud Service
New-AzureService -ServiceName $vmName -AffinityGroup $affinityGroup

#Create Azure VM
if ($domain)
{
  New-AzureVMConfig -Name $vmName -InstanceSize $instanceSize -ImageName $imageName | 
  Add-AzureProvisioningConfig -WindowsDomain -Password $adminPassword -JoinDomain $domain -Domain $domain -DomainUserName $domainUserName -DomainPassword $domainPassword -AdminUsername $adminUserName | 
  Set-AzureSubnet $subnetName |
  New-AzureVM -ServiceName $vmName -VNetName $networkName
}
else
{
  New-AzureVMConfig -Name $vmName -InstanceSize $instanceSize -ImageName $imageName | 
  Add-AzureProvisioningConfig -Windows -AdminUsername $adminUserName -Password $adminPassword | 
  Set-AzureSubnet $subnetName |
  New-AzureVM -ServiceName $vmName -VNetName $networkName
}
  • Confirm the VM was created successfully and joined to your domain

Create and Test an SMA Runbook based on this script

  • Install Windows PowerShell for Azure (under Command-line tools) on your SMA server
  • Logon to the Service Management Portal and click on Automation\Runbooks\New
  • Click Quick Create, enter a name for the Runbook such as DeployVMInAzure, and click the Check Mark

image

  • Page over to the new Runbook, click on it, select Author, and click Draft
  • Paste in the following script (change the top line if you used something other than DeployVMInAzure as your runbook name)
 workflow DeployVMInAzure
{
  param(
  $azureSettingsFile,
  $azureAccountName,
  $imageName,
  $vmName,
  $instanceSize,
  $adminUserName,
  $adminPassword,
  $domain,
  $domainUserName,
  $domainPassword,
  $affinityGroup,
  $networkName,
  $azureStorageAccountName,
  $subnetName
  )
  
  #Set Instance Size
  if ($instanceSize -eq "A0") { $instanceSize = 'ExtraSmall' }
  elseif ($instanceSize -eq "A1") { $instanceSize = 'Small' }
  elseif ($instanceSize -eq "A2") { $instanceSize = 'Medium' }
  elseif ($instanceSize -eq "A3") { $instanceSize = 'Large' }
  elseif ($instanceSize -eq "A4") { $instanceSize = 'ExtraLarge' }
   
  inlinescript
  {
    #Import Azure PowerShell Module
    Import-Module Azure

    #Remove Existing Azure Sessions
    Remove-AzureAccount -Name $Using:azureAccountName -Force
    
    #Import Azure Settings File
    Import-AzurePublishSettingsFile -PublishSettingsFile $Using:azureSettingsFile
    
    #Set Azure Subscription
    Get-AzureSubscription | Set-AzureSubscription -currentstorageaccountname $Using:azureStorageAccountName
    
    #Increment VM Name
    $i = 0
    foreach ($vm in Get-AzureVM)
    {
      if ($vm.InstanceName.Contains($Using:vmName))
      {
        [int]$increment = $vm.InstanceName.SubString($Using:vmName.Length)
        if ($increment -gt $i) { $i = $increment }
      }
    }
    $i++
    $vmName = $Using:vmName + $i

    #Create New Azure Cloud Service
    New-AzureService -ServiceName $vmName -AffinityGroup $Using:affinityGroup

    #Create Azure VM
    if ($Using:domain)
    {
      New-AzureVMConfig -Name $vmName -InstanceSize $Using:instanceSize -ImageName $Using:imageName | 
      Add-AzureProvisioningConfig -WindowsDomain -Password $Using:adminPassword -JoinDomain $Using:domain -Domain $Using:domain -DomainUserName $Using:domainUserName -DomainPassword $Using:domainPassword -AdminUsername $Using:adminUserName | 
      Set-AzureSubnet $Using:subnetName |
      New-AzureVM -ServiceName $vmName -VNetName $Using:networkName
    }
    else
    {
      New-AzureVMConfig -Name $vmName -InstanceSize $Using:instanceSize -ImageName $Using:imageName | 
      Add-AzureProvisioningConfig -Windows -AdminUsername $Using:adminUserName -Password $Using:adminPassword | 
      Set-AzureSubnet $Using:subnetName |
      New-AzureVM -ServiceName $vmName -VNetName $Using:networkName
    }
  }
}
  • Click Publish
  • In the Portal, go back to Runbooks and Start the new Runbook you just created
  • Fill in the parameters the same way you did for the PowerShell Script
  • Confirm the VM was created successfully and joined to your domain

 

Continue on to Part III

DeployVMInAzure.zip