Create Azure VM from PowerShell with IIS Web Server auto installed

I saw someone asking on a forum about how to create Azure VM with Windows feature pre-installed, i thought it would be nice weekend exercise to see if i can get a Azure virtual machine created from PowerShell with IIS Web Server auto installed.

Now, by no means, i am saying that the PowerShell script given below is the best way to do it, there could be better, easier and correct way of doing it, but hey … this got my job done, so it should help you as well.

So in below PowerShell script the only thing that you have to change is, replace wherever you see XXXXXX with your cloud service name (the name that appears in Azure cloud service section), i know i can add a variable and make it easier for you, perhaps in next version of script.

so let’s see the script.


#Let's select the VMImage first that we want to use to build Server
$getvmimage = "a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-R2-201412.01-en.us-127GB.vhd"

# keep some variable handy which we will reuse many times
$user = "webadmin"
$pass = "P@ssw0rd"
$name = "Web60" #####This will be the name of your VM, you can changet if you wish to########

# Create new AzureVMconfig for Web60 and provide it as input to Azureprovisioningconfig and eventually to New-AzureVM
$newvm = New-AzureVMConfig -Name $name -InstanceSize Medium -ImageName $getvmimage | Add-AzureProvisioningConfig -Windows -AdminUsername $user -Password $pass
New-AzureVM -ServiceName XXXXXXXXXXX -VMs $newvm -WaitForBoot -Verbose

#Some Port & Session Variables
$port = Get-AzureEndpoint -Name PowerShell -VM (Get-AzureVM -ServiceName XXXXXXXXXXX -Name $name) | Select -ExpandProperty Port

Write-Host "$port"   ####No real importance, writing it on console only for testing##############

$credential = Get-Credential -Credential $user -Verbose
$sessionoption = New-PSSessionOption -SkipCACheck
$session =  New-PSSession -ComputerName XXXXXXXXX.cloudapp.net -Credential $credential -Port $port -UseSSL -SessionOption $sessionoption -Verbose

$getstatus = Get-AzureVM -ServiceName XXXXXXXXXX -name $name | select InstanceStatus, PowerState -Verbose
if ($getstatus -match "ReadyRole" -and "Started") {Write-Host "Server is Ready, Connecting Now"; Enter-PSSession -Session $session}
else {Write-Host "Unable to Connect, Something Wrong, Try Again"; $getstatus }

#Once PSSession  starts then it should install Webserver Role in remote server
#Install-WindowsFeature -Name Web-Server -IncludeAllSubFeature -IncludeManagementTools -Verbose

Invoke-Command -Session $session  -ScriptBlock {Install-WindowsFeature -Name Web-Server -IncludeAllSubFeature -IncludeManagementTools -Verbose} -Verbose

There are many more stuff that you can do it from this script by making some easy changes, like instead of Web-server you can deploy any other Windows Feature by just making some changes, or you can leave the windows feature about completely and just use the PowerShell remoting to login to remote server and do whatever you want to.

If you come across any bug, or if you know about a better way of doing it, just share in comment.

Till then happy learning.

Hardeep

Sitecore8 Logout Bug

I might be “the fewer ones” who got chance to work on an upgrade project from Sitecore 6.6 to Sitecore8. During our initial installation phase when I was playing around with Sitecore8 discovered a bug with the Logout link available at the top right corner of Sitecore Launch Pad.

Sitecore8 Logout

Do you ever logout from Sitecore? I have seen most of the users directly closing the browser, instead of doing it a correct way and logging out. Logging out correctly doesn’t matters if your license covers huge number of logins, however if you have limited logins licensed and users do not logout, often you might come across a message stating maximum number of allowed users has been reached. If you have come across such a message here is a very nice article explaining the solution.

So, you have just logged in Sitecore8 to get a snitch of it and clicked on “Logout” link it comes up with error as shown below.

Sitecore8-Logout-error

Sitecore8-Logout-error

This happens only for the first time when you click on Logout link. Using browser back button and clicking again on Logout will not reproduce this issue. It’s just for the first time in a new browser session.

Sitecore Support has provided a solution for it and that is what I am sharing it here,

1. Backup /Website/Views/Shared/ExperienceExplorerView.cshtml

2. Download ExperienceExplorerView.zip, extract and replace ExperienceExplorerView.cshtml at /Website/Views/Shared folder.

That’s all I have to share in this post, keep Sitecoring and sharing.

Create New Storage Account with PowerShell

Creating New Storage Account in Azure.
It’s pretty easy when you want to create an Azure storage account using management portal, however let’s see how to create one using PowerShell, gradually you’ll see all this smaller tutorials adding up to one complete how to tutorial for Azure VM creation.

1. Let’s first see if you have any storage account available in Azure subscription

You can query your subscription with “Get-AzureSubscription” and under “CurrentStorageAccountName” property you will see the name of storage account, if the property is blank than that means either no storage account is created or the storage account is not yet set in subscription.

08-Get-AzureSubscription

As you can see we do not have any account available as of now under “CurrentStorageAccountName”.

2. So let’s now create a storage account in Azure using PowerShell cmdlets and later on we will set the account in subscription

#New-AzureStorageAccount –Location “East US 2” –StorageAccountName “hi1” –Label “HI1”

11-New-AzureStorageAccount

We have successfully created the storage account, you should be able to verify it in your management portal as well, for me it took about a min for information to appear on management portal.

3. Now let’s verify in your PowerShell and see if PowerShell can detect the new storage account automatically.

#Get-AzureSubscription

08-Get-AzureSubscription

AS you can see “CurrentStorageAccountName” property doesn’t have any value set, so let’s update the properties of a Microsoft Azure Storage account in current subscription.

4. Let’s set the newly created storage account in Azure Subscription.

The “Set-Azuresubscription” cmdlet establishes and changes the properties of an Azure Subscription. You can use this cmdlet to create a new subscription or change the properties of an existing subscription.

Required parameter are: -CurrentStorageAccountName
: -SubscriptionName

# Set-AzureSubscription -SubscriptionName -CurrentStroageAccountName “write the name of your account here.”

Now if you do not come across any error, let’s verify and check the properties of “CurrentStorageAccount” whether it’s update or not with new value.

15-Blur-AzureSub

Keeping learning, Keep Sharing

Thanks

Hardeep