So, I have an MSDN subscription tenant in Azure (thanks to my generous employer and the efforts of several people I can’t thank enough).  In that realm, I have a resource group and two (2) virtual machines with vanilla Windows Server 2012 R2 installs.

$azEnv   = "AzureCloud"
$azAcct  = "####@######.com"
$azTenId = "12345678-abcd-1234-abcd-1234567890ff"
$azSubId = "abcdefgh-1234-abcd-1234-abcdefghijkl"

# Note: There are several ways to get the above property values, but one is to 
# run Login-AzureRmAccount from within PowerShell, without any arguments, 
# and it will spit out that info as well.

$azRgName = "resourcegroup1"

if ($azCred -eq $null) {
  $azCred = Login-AzureRmAccount -EnvironmentName $azEnv -AccountId $azAcct -SubscriptionId $azSubId -TenantId $azTenId
}

foreach ($vm in $azVmList) {
  $azVM = Get-AzureRmVm -ResourceGroupName $azRgName -Name $vm
  $azVmStat = Get-AzureRmVm -ResourceGroupName $azRgName -Name $vm -Status
  $azVmLoc  = $azVM.Location
  $azVmType = $azVM.HardwareProfile.VmSize
  $azVmOs   = $azVM.StorageProfile.ImageReference.Sku
  $azVmAdmUser = $azVM.OSProfile.AdminUsername
  Write-Output "$vm :: Location: $azVmLoc / Size: $azVmType / OS: $azVmOs"
  foreach ($vmSC in $azVmStat.Statuses) {
    if ($vmSC.Code.CompareTo("PowerState/deallocated") -eq 0) {
      Write-Output $vmSC.DisplayStatus
    }
  }
  Write-Output "`tStarting $vm..."
  Start-AzureRmVM -Name $vm -ResourceGroupName $azRgName
}

The output should look something like this…

VM1 :: Location: eastus / Size: Standard_D2 / OS: 2012-R2-Datacenter
Provisioning succeeded
VM deallocated
    Starting VM1...
RequestedId IsSuccessStatusCode  StatusCode  ReasonPhrase
                           True          OK  OK
VM2 :: Location: eastus / Size: Standard_D3 / OS: 2012-R2-Datacenter
VM deallocated
    Starting VM2...
                           True          OK  OK
PS C:>

And then in the Azure portal the machines will eventually show as “Running”. I run another script to invoke Stop-AzureRmVm with -Force added, to avoid draining my monthly credit balance.

I’m sure this isn’t new or unique in any way.  Just dumping this here in case I forget about it later on.

Advertisements

4 thoughts on “Goofing Around with Azure and PowerShell

  1. Dave, this is the one I use… similar but different. You need to work on your comment-based help and using parameters as opposed to hard coding values. 🙂

    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$True,Position=1, HelpMessage=”Enter ‘AzureCloud’ for Commercial or ‘AzureUSGov’ for Gov Cloud”)]
    [ValidateSet(“AzureCloud”, “AzureUSGovernment”)]
    [string]$CloudEnv
    )

    #Login
    Login-AzureRMAccount -EnvironmentName $CloudEnv | Out-Null

    #Select Subscription
    #Initialize Global i variable as zero
    $global:i=0
    Get-AzureRmSubscription | Select @{Name=”Item”;Expression={$global:i++;$global:i}},SubscriptionName, SubscriptionId, State -OutVariable subs | Format-Table -AutoSize
    $s = Read-Host “Select a subscription”
    $sub = $subs | Where-Object {$_.Item -eq $s}

    Select-AzureRmSubscription -SubscriptionId $sub.SubscriptionId | Out-Null

    Write-Host “You are now working in the” $sub.SubscriptionName “subscription” -ForegroundColor Yellow

    1. rofl, it removed my comment based help… go figure.

      <#

      .SYNOPSIS
      Sets up Powershell Environment for Azure Commercial and US Gov enrollments

      .DESCRIPTION
      To maintain consistency this script requires the -CloudEnv
      parameter to be provided with either AzureCloud for Commercial or
      AzureUSGovernment for Gov cloud.

      .PARAMETER CloudEnv
      Required to connect to the appropriate cloud service portal.

      .EXAMPLE
      Setup-AzureEnvironment -CloudEnv AzureCloud

      .EXAMPLE
      Setup-AzureEnvironment -CloudEnv AzureUSGovernment

      .NOTES
      Author: Steve Correia
      #>

      [CmdletBinding()]
      Param(
      [Parameter(Mandatory=$True,Position=1, HelpMessage="Enter ‘AzureCloud’ for Commercial or ‘AzureUSGov’ for Gov Cloud")]
      [ValidateSet("AzureCloud", "AzureUSGovernment")]
      [string]$CloudEnv
      )

      #Login
      Login-AzureRMAccount -EnvironmentName $CloudEnv | Out-Null

      #Select Subscription
      #Initialize Global i variable as zero
      $global:i=0
      Get-AzureRmSubscription | Select @{Name="Item";Expression={$global:i++;$global:i}},SubscriptionName, SubscriptionId, State -OutVariable subs | Format-Table -AutoSize
      $s = Read-Host "Select a subscription"
      $sub = $subs | Where-Object {$_.Item -eq $s}

      Select-AzureRmSubscription -SubscriptionId $sub.SubscriptionId | Out-Null

      Write-Host "You are now working in the" $sub.SubscriptionName "subscription" -ForegroundColor Yellow

      1. To clarify, this script just sets up the Powershell environment for working with a particular enrollment/subscription. Creating VMs would be a separate script that I would call after this one.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s