Configuring SharePoint 2013 Farm on Windows Azure VM Using PowerShell

In this article we will learn how to configure a SharePoint Farm in Windows Azure VM using PowerShell.

Install Windows Azure PowerShell

The Windows Azure PowerShell provides IT Pros with Windows PowerShell cmdlets for building, deploying and managing Windows Azure services. Download “WindowsAzurePowerShell.3f.3f.3fnew.exe” and install Web Platform Installer 4.6 .Sometimes it fails to install the required components if there is any network issue. In that case I have downloaded all the components and installed them manually and it took hardly 5 minutes to install all the components. The following image lists all the components. First install the component with the red border (Windows6.1-KB2506143-x64.msu), because other installations require this component to be installed.

Window Azure PowerShell

Download and import publish settings and subscription information

1. Run Windows PowerShell as an administrator as follows:
  • Choose Start, in the Search box, type Windows PowerShell.
  • Right-click the Windows PowerShell link, and then choose Run as administrator.

2. At the Windows PowerShell command prompt, type Get-AzurePublishSettingsFile command, and then press Enter.

This will launch the browser and take you to a page where you can login using the Microsoft account associated with your Windows Azure account. Once logged in you will be prompted to download a <subscription-name(s)>.publishsettings file to your hard drive. This page will automatically generate a management certificate in each and every subscription that you are the admin or co-administrator of. Please do remember the .publishsettings file path. This will require to Import the publish file. Once the file is downloaded you can then call:

Import-AzurePublishSettingsFile "C :\< your file path>\mysubscriptions.publishsettings"

This cmdlet will install the certificate into your certificate store and configure your subscription settings for PowerShell. To see the created stored settings you can browse to the following directory on your machine:

C:\Users\username\AppData\Roaming\Windows Azure PowerShell.

Create and configure SharePoint 2013 single server Development farm:

Now you have done all the local configurations to establish a connection to the Windows Azure Environment. If you are working in a restricted environment (office network) you must connect to TERAS to successfully execute your Windows Azure PowerShell cmdlet. Without TERAS I got the following error.

configure SharePoint
 
Now it’s time to create a new VM and configure the SharePoint 2013.To make it easy and to automate the process, I have created a PowerShell script (ConfigureSharepoint2013DevEnv.ps1) file that created and configured a SharePoint 2013 Single Server Farm. I used “SpAdmin” as a super user since I am using this user to connect to the Azure VM as well as a SharePoint farm administrator and “spadmin#1” as the password. Run Windows PowerShell as an administrator and execute the ConfigureSharepoint2013DevEnv.ps1 script file. Execution of this script prompts for user name and password. I have used “SpAdmin” whereever it prompts for credentials since I have used this in my script file.

Contents of ConfigureSharepoint2013DevEnv.ps1
 
Find the contents of the script file here. So you can copy the content and create a script file for you and enjoy. I still am in the process of refining the script to make it more automated by avoiding the user, to provide their credentials to connect to the VM and configure the SharePoint farm.

##############################################

ConfigureSharepoint2013DevEnv.ps1

##############################################

write-host "Set ExecutionPolice to Unrestricted "
Set-ExecutionPolicy Unrestricted
write-host "Start loding Azure.psd1 Module"
Import-Module " C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\Azure\Azure.psd1"
Start-Sleep -Seconds 5
#Get-AzureSubscription and get the name of subscription.
write-host "Setting local variables"
$AzureSubscriptionName ="Visual Studio Premium with MSDN"
Select-AzureSubscription $AzureSubscriptionName
$AFGLocation = "East Asia"
$AFGDescription = "SP2013-Tie-AG"
$AFGName = "SP2013-Tie-AG"
$AFGLabel = "SP2013-Tie-AG"

#------------------------------------------------------------------------------------------------------------------------
# Start See if the affinity group already exists
#-----------------------------------------------------------------------------------------------------------------------

$affi=Get-AzureAffinityGroup -name $AFGName |Select Name
#if($affi.Name.Contains($AFGName))
if($affi)
{
    # Already Exists, Skip This Step
    Write-Host "Ther Affinity Group ""$AFGName"" already exists.Skipping this step..."
}
else
{
    # Affinity group does not exissts.
    New-AzureAffinityGroup -Location $AFGLocation -Description $AFGDescription -Name $AFGName -Label $AFGLabel
}
$StorageAccountName = "mytiestorage1"
$StorageAccountLabel = "Teeto SP2013 Storage Account"
$myService = “MySPEnvVM”
$myVMName=“MySPEnvImg”

#------------------------------------------------------------------------------------------------------------------------
#End See if the affinity group already exists
#-----------------------------------------------------------------------------------------------------------------------

#------------------------------------------------------------------------------------------------------------------------
# Start Set the current storage Account
#------------------------------------------------------------------------------------------------------------------------

Write-Host "Get the current storage Account"
try

    $store=Get-AzureStorageAccount -StorageAccountName $StorageAccountName |Select StorageaccountName
}
catch
{
    Write-Host " ***Error Handled***" 
}
Write-Host "current storage Account is "$store
#if($store.StorageaccountName.Contains($StorageAccountName))
if($store)
{
    Write-Host " The storage account ""$StorageAccountName"" already exists.Setting it Up..." 
}
else
{
    Write-Host " Creating a new storage account ""$AzureSubscriptionName""..."
    New-AzureStorageAccount -StorageAccountName $StorageAccountName -Label $StorageAccountLabel -AffinityGroup $AFGName
}
Write-Host "SETTING THE STORAGE LOATION......................................."
$store=Get-AzureStorageAccount -StorageAccountName $StorageAccountName |Select StorageaccountName
if($store)
{
    Write-Host " Find the Storage to configure Azure subscription"
    Set-AzureSubscription -SubscriptionName $AzureSubscriptionName -CurrentStorageAccountName $StorageAccountName
}
Write-Host "Configuring Subscription with the CurrentStorageAccount..."
Get-AzureSubscription | Set-AzureSubscription -SubscriptionName $AzureSubscriptionName -CurrentStorageAccount $StorageAccountName

#------------------------------------------------------------------------------------------------------------------------------------------
# End Set the current storage Account
#----------------------------------------------------------------------------------------------------------------------------------------------

#------------------------------------------------------------------------------------------------------------------------------------------
# Start Create Configure the VM image file for Windows Server 2012 + SharePoint 2013
#----------------------------------------------------------------------------------------------------------------------------------------------

# In order to retrieve the list of available VMs models invoke the following command and
# find your VM image name here we have selected the following one
# Get-AzureVMImage | Select ImageName

$SharePoint2013 =“03f55de797f546a1b29d1b8d66be687a__Visual-Studio-2013-Premium-12.0.21005.1-AzureSDK-2.2”
#$Password = "spadmin#1"
$DomainUsername = "SpAdmin"
$DomainPassword = " spadmin#1"
$myService = “MySPEnvSer”
$myVMName=“MySPDevEnv”
write-host "Start Creating a new VM for sharepoint 2013 develpoment environment"
$ReqVMStatus = Get-AzureVM –ServiceName $myService -Name $myVMName
if (!$ReqVMStatus)
{
     Write-Host "No VM named $myVMName exists"
     New-AzureQuickVM -ImageName $SharePoint2013 -Windows -Name $myVMName –ServiceName $myService -AdminUsername $DomainUsername -Password $DomainPassword -AffinityGroup $AFGName
}
else
{
    Write-Host "A VM named $myVMName already exists"
}

#---------------------------------------------------------------------------------------------------------------------------------
# Start Create Configure the VM image file for Windows Server 2012 + SharePoint 2013
#---------------------------------------------------------------------------------------------------------------------------------

#------------------------------------------------------------------------------------------------------------------------
 # Start Check the readyness of newly created VM
#------------------------------------------------------------------------------------------------------------------------

# Wait for server to reboot

While ($ReqVMStatus.InstanceStatus -ne "ReadyRole")
{
    write-host "Waiting...Current Required VM Status is = " $ReqVMStatus.InstanceStatus
    Start-Sleep -Seconds 20
    $ReqVMStatus = Get-AzureVM –ServiceName $myService -Name $myVMName
}
write-host "Waiting...Current Required VM Status is = " $ReqVMStatus.InstanceStatus
write-host "Vm create step complite"

#------------------------------------------------------------------------------------------------------------------------
# End Check the readyness of newly created VM
#-------------------------------------------------------------------------------------------------------------------------

#------------------------------------------------------------------------------------------------------------------------
# Start configuring newly created VM
#-------------------------------------------------------------------------------------------------------------------------

write-host " Start Installing VM certificate..."
.\InstallWinRMCertAzureVM.ps1 -SubscriptionName $AzureSubscriptionName -ServiceName $myService -Name $myVMName
write-host " Complited Installing VM certificate"
Start-Sleep -Seconds 10
$uri = Get-AzureWinRMUri -ServiceName $myService -Name $myVMName
write-host "URL Of Service is "$uri
Start-Sleep -Seconds 5
$cred = Get-Credential
#$secPassword = ConvertTo-SecureString -String $DomainPassword -AsPlainText -Force
#$cred = New-Object System.Management.Automation.PSCredential($DomainUsername, $secPassword)
write-host " Credential is ready now"
write-host " Connecting to VM..."
Enter-PSSession –ConnectionUri $uri –Credential $cred
write-host " Connection successfully established."
write-host "configuring sharepoint 2013 on Windows Azure vertual machine"
Invoke-Command -ConnectionUri $uri -credential $cred -ScriptBlock {invoke-expression "C:\ConfigureDeveloperDesktop\Scripts\ConfigureSharePointFarm.ps1"}
#Invoke-Command -ConnectionUri $uri -credential $cred -ScriptBlock {invoke-expression "C:\ConfigureDeveloperDesktop\Scripts\ConfigureSharePointFarm.ps1 -localSPFarmAccountName $DomainUsername -localSPFarmAccountPassword $DomainPassword"}
Start-Sleep -Seconds 5
write-host "SharePoint 2013 configuration done"
#------------------------------------------------------------------------------------------------------------------------
# Set Executionpolicy back to it's actual state.
#-------------------------------------------------------------------------------------------------------------------------
Write-Host "Set Executionpolicy back to it's actual state."
Set-ExecutionPolicy RemoteSigned

Helpful cmdlets

The following are a few helpful cmdlets of Windows Azure PowerShell:

  1. Get-AzureLocation | select Name // to retrieve all the available Azure locations
  2. Get-ExecutionPolicy –List // to verify the current Execution policy.
  3. Set-ExecutionPolicy RemoteSigned / Unrestricted undefined/ AllSigned // to set ExecutionPolicy value.
  4. Get-AzureSubscription // get the details of your subscription.
  5. Stop-AzureVM -Name myVMName -ServiceName myService //To stop the VM
  6. Start-AzureVM -Name myVMName -ServiceName myService //To start the VM
  7. Restart-AzureVM -Name myVMName -ServiceName myService //To Restart the VM

Other Related References

  1. Get-AzureStorageAccount
  2. configuring Remote PowerShell for Windows Azure Virtual Machines on your machine
  3. Configuring Virtual Machine Disks and Endpoints with Windows Azure PowerShell
  4. Getting Started with Windows Azure PowerShell
  5. Using Remote PowerShell with Windows Azure VMs