Last active
August 29, 2015 14:07
-
-
Save DexterPOSH/e9dceb72a6f171bd3d97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.