Yo. Dig.  These are primarily for serialized PowerShell use, not template use (i.e. JSON-infused).  Peace.

1wearandtear

function Make-ResourceGroup {
  param (
    [parameter(Mandatory=$True)] [string] $Name,
    [parameter(Mandatory=$True)] [string] $Loc
  )
  $rgx = Get-AzureRmResourceGroup -Name $Name -Location $Loc -ErrorAction SilentlyContinue
  if ($rgx -eq $null) {
    Write-Verbose "info: creating resource group: $Name..."
    $(New-AzureRmResourceGroup -Name $Name -Location $Loc -ErrorAction SilentlyContinue)
  }
  else {
    Write-Verbose "info: resource group already exists: $Name"
    $rgx
  }
}

Verify requested image publisher, offer and SKU exist in the marketplace…

#------------------------------------------------------------------
# verify marketplace image publisher is valid
#------------------------------------------------------------------

function Test-ImagePublisher {
  param (
    [parameter(Mandatory=$True)] [string] $Publisher,
    [parameter(Mandatory=$True)] [string] $Loc
  )
  $(!(Get-AzureRmVMImagePublisher -Location $Loc | ?{$_.PublisherName -eq $Publisher}) -ne $null)
}

#------------------------------------------------------------------
# verify marketplace image publisher offername is valid
#------------------------------------------------------------------

function Test-ImageOffer {
  param (
    [parameter(Mandatory=$True)] [string] $Publisher, 
    [parameter(Mandatory=$True)] [string] $OfferName,
    [parameter(Mandatory=$True)] [string] $Loc
  )
  $(!(Get-AzureRmVMImageOffer -Location $Loc -PublisherName $Publisher | ?{$_.Offer -eq $OfferName}) -ne $null)
}

#------------------------------------------------------------------
# verify marketplace SKU exists for specified publisher and offer
#------------------------------------------------------------------

function Test-ImageSKU {
  param (
    [parameter(Mandatory=$True)] [string] $Publisher, 
    [parameter(Mandatory=$True)] [string] $OfferName, 
    [parameter(Mandatory=$True)] [string] $Sku,
    [parameter(Mandatory=$True)] [string] $Loc
  )
  $(!(Get-AzureRmVMImageSku -Location $Loc -PublisherName $Publisher -Offer $OfferName | ?{$_.Skus -eq $Sku}) -ne $null)
}

Examples…

$PubName  = "MicrosoftWindowsServer"
$Offer    = "WindowsServer"
$SkuName  = "2012-R2-Datacenter"
$Location = "East US"

if (!(Test-ImagePublisher $PubName $Location)) {
  Write-Output "Hold up! You best take yo ass back to the shed, G!"
}
if (!(Test-ImageOffer $PubName $Offer $Location)) {
  Write-Output "Oh, no no no... this is wack."
}
if (!(Test-ImageSku $PubName $Offer $Location $SkuName)) {
  Write-Output "That's it. Now I gotta choke yo ass!"
}
# all good, continue on...
Advertisements

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