Skip to content

Instantly share code, notes, and snippets.

@DexterPOSH
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DexterPOSH/e9dceb72a6f171bd3d97 to your computer and use it in GitHub Desktop.
Save DexterPOSH/e9dceb72a6f171bd3d97 to your computer and use it in GitHub Desktop.
workflow New-TestVM
{
param(
[parameter(Mandatory)]
[String]
$AzureSubscriptionName,
[parameter(Mandatory)]
[String]
$ServiceName,
[parameter(Mandatory)]
[String]
$VMName,
[parameter()]
[String]
$InstanceSize = "Medium"
)
$verbosepreference = 'continue'
#Get the Credentials to authenticate agains Azure
Write-Verbose -Message "Getting the Credentials"
$Cred = Get-AutomationPSCredential -Name "AuthAzure"
$LocalCred = Get-AutomationPSCredential -Name "LocalDexterPOSH"
$DomainCred = Get-AutomationPSCredential -Name "DomainDexterPOSH"
#Add the Account to the Workflow
Write-Verbose -Message "Adding the AuthAzure Account to Authenticate"
Add-AzureAccount -Credential $Cred
#select the Subscription
Write-Verbose -Message "Selecting the $AzureSubscriptionName Subscription"
Select-AzureSubscription -SubscriptionName $AzureSubscriptionName
#Set the Storage for the Subscrption
Write-Verbose -Message "Setting the Storage Account for the Subscription"
Set-AzureSubscription -SubscriptionName $AzureSubscriptionName -CurrentStorageAccountName "dexterposhstorage"
#Select the most recent Server 2012 R2 Image
Write-Verbose -Message "Getting the Image details"
$imagename = Get-AzureVMImage |
where-object -filterscript { $_.ImageFamily -eq “Windows Server Technical Preview” } |
Sort-Object -Descending -Property PublishedDate |
Select-Object -First 1 |
select -ExpandProperty ImageName
#use the above Image selected to build a new VM and wait for it to Boot
$Username = $LocalCred.UserName
$Password = $LocalCred.GetNetworkCredential().Password
New-AzureQuickVM -Windows -ServiceName $ServiceName -Name $VMName -ImageName $imagename -Password $Password -AdminUsername $Username -SubnetNames "Rest_LAB" -InstanceSize $InstanceSize -WaitForBoot
Write-Verbose -Message "The VM is created and booted up now..Doing a checkpoint"
#CheckPoint the workflow
CheckPoint-WorkFlow
Write-Verbose -Message "Reached CheckPoint"
#Call the Function Connect-VM to import the Certificate and give back the WinRM uri
$WinRMURi = Get-AzureWinRMUri -ServiceName $ServiceName -Name $VMName | Select-Object -ExpandProperty AbsoluteUri
InlineScript
{
do
{
#open a PSSession to the VM
$Session = New-PSSession -ConnectionUri $Using:WinRMURi -Credential $Using:LocalCred -Name $using:VMName -SessionOption (New-PSSessionOption -SkipCACheck ) -ErrorAction SilentlyContinue
Write-Verbose -Message "Trying to open a PSSession to the VM $Using:VMName "
} While (! $Session)
#Once the Session is opened, first step is to join the new VM to the domain
if ($Session)
{
Write-Verbose -Message "Found a Session opened to VM $using:VMname. Now will try to add it to the domain"
Invoke-command -Session $Session -ArgumentList $Using:DomainCred -ScriptBlock {
param($cred)
Add-Computer -DomainName "dex.com" -DomainCredential $cred
Restart-Computer -Force
}
}
}
} #Workflow end
@jodoglevy
Copy link

Cool solution. Want to add it to the Azure Automation part of ScriptCenter (http://gallery.technet.microsoft.com/scriptcenter/site/search?f%5B0%5D.Type=RootCategory&f%5B0%5D.Value=WindowsAzure&f%5B0%5D.Text=Windows%20Azure&f%5B1%5D.Type=SubCategory&f%5B1%5D.Value=WindowsAzure_automation&f%5B1%5D.Text=Automation) so it shows up in the new Azure Automation runbook gallery?

Also, some feedback:
AzureConnectionName is kind of confusing here. It is really the name of your Azure subscription, and has nothing to do with an Azure connection asset.

Also, your comment says you use Connect-AzureVM (http://gallery.technet.microsoft.com/scriptcenter/Connect-to-an-Azure-85f0782c) to set up the connection to the VM, but it looks like you are just getting the WinRM URI. Does this work, given you aren't setting up the cert needed to talk between the VM and the Azure Automation sandbox (like Connect-AzureVM does)?

@DexterPOSH
Copy link
Author

Hey Joe,

Thanks for the comment. I will definitely post this to the technet gallery.

I started out with using Connect-AzureVM and using one of the scripts from the gallery. So am still using the AzureConnectionName , I will change it to AzureSubscriptionName as that makes more sense.

While exploring I found that opening a PSSession by ignoring the certificate errors works. So the Connect-AzureVM is not needed anymore.
This has worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment