Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
##################################################
#
##################################################
param(
[Parameter(Mandatory = $true)][String]$subscriptionId,
[Parameter(Mandatory = $true)][String]$storageAccountName,
[Parameter(Mandatory = $true)][String]$affinityGroupName,
[Parameter(Mandatory = $true)][String]$imageName = 'MSFT__Windows-Server-2012-Datacenter-201208.01-en.us-30GB.vhd',
[Parameter(Mandatory = $true)][String]$adminPassword,
[Parameter(Mandatory = $true)][String]$serviceName,
[Parameter(Mandatory = $true)][String]$vmName,
[ValidateSet('ExtraSmall', 'Small', 'Medium', 'Large', 'ExtraLarge')]
[Parameter(Mandatory = $true)][String]$instanceSize
)
# Variables
$endpoints = @(@('WebDeploy', 8172, 8172), @('Web', 80, 80))
$subscriptionName = (Get-AzureSubscription | where { $_.SubscriptionId -eq $subscriptionId }).SubscriptionName
# Select the Subscription
Get-AzureSubscription | where { $_.SubscriptionId -eq $subscriptionId } | Select-AzureSubscription
# Set CurrentStorageAccount
Set-AzureSubscription -SubscriptionId $subscriptionId -SubscriptionName $subscriptionName -CurrentStorageAccount $storageAccountName
# Create a New VM
New-AzureQuickVM -Windows -ServiceName $serviceName -Name $vmName -ImageName $imageName -Password $adminPassword -AffinityGroup $affinityGroupName -InstanceSize $instanceSize
# Add Azure Endpoints
foreach($endpoint in $endpoints) {
Get-AzureVM -ServiceName $serviceName -Name $vmName | Add-AzureEndpoint -Name "$($endpoint[0])" -Protocol TCP -LocalPort $endpoint[1] -PublicPort $endpoint[2] | Update-AzureVM
}
##################################################
# If you would like to create endpoints seperately, you can use this.
# The Init-WebServerAzureVM.ps1 is already doing this.
##################################################
param(
$subscriptionId,
$serviceName,
$vmName
)
# Variables
$endpoints = @(@('WebDeploy', 8172, 8172), @('Web', 80, 80))
# Select the Subscription
Get-AzureSubscription | where { $_.SubscriptionId -eq $subscriptionId } | Select-AzureSubscription
# Add Azure Endpoints
foreach($endpoint in $endpoints) {
Get-AzureVM -ServiceName $serviceName -Name $vmName | Add-AzureEndpoint -Name "$($endpoint[0])" -Protocol TCP -LocalPort $endpoint[1] -PublicPort $endpoint[2] | Update-AzureVM
}
##################################################
# Resosurces:
## ServerManager Module: http://technet.microsoft.com/en-us/library/cc732263.aspx
## NetSecurity Module: http://technet.microsoft.com/en-us/library/hh831755.aspx
## Others:
## http://www.iis.net/learn/manage/remote-administration/remote-administration-for-iis-manager
## http://blogs.technet.com/b/heyscriptingguy/archive/2012/05/12/weekend-scripter-use-powershell-to-easily-modify-registry-property-values.aspx
## http://www.iis.net/learn/install/web-platform-installer/web-platform-installer-v4-command-line-webpicmdexe-rtw-release
## http://www.tugberkugurlu.com/archive/script-out-everything-initialize-your-windows-azure-vm-for-your-web-server-with-iis-web-deploy-and-other-stuff
##
## Instructions:
## When you install the Web Platform Installer v4 RTW
## (http://download.microsoft.com/download/7/0/4/704CEB4C-9F42-4962-A2B0-5C84B0682C7A/WebPlatformInstaller_amd64_en-US.msi),
## you will get Web Platform Installer v4 Command Line Tool OOB. Under C:\Program Files\Microsoft\Web Platform Installer directory,
## There are three files:
## * Microsoft.Web.PlatformInstaller.dll
## * WebpiCmd.exe
## * WebpiCmd.exe.config
## These files can be copied around. Take those three files and put this script under the same path.
## Set your PowerShell Execution Policy to Unrestricted:
## PS> Set-ExecutionPolicy Unrestricted
## Lastly, run this script file:
## PS> .\Init-WebServerVM.ps1
## Now, go and watch your favorite TV Show till your VM gets ready for ya :)
##################################################
# Variables
$ScriptRoot = (Split-Path -parent $MyInvocation.MyCommand.Definition)
$additionalFeatures = @('Web-Mgmt-Service', 'Web-Asp-Net45', 'Web-Dyn-Compression', 'Web-Scripting-Tools')
$webPiProducts = @('WDeployPS', 'UrlRewrite2')
# Import Modules
Import-Module -Name ServerManager
Import-Module -Name NetSecurity
Import-Module -Name Microsoft.PowerShell.Management
# Add Windows Features
Install-WindowsFeature -Name Web-Server -IncludeManagementTools -LogPath "$env:TEMP\init-webservervm_webserver_install_log_$((get-date).ToString("yyyyMMddHHmmss")).txt"
foreach($feature in $additionalFeatures) {
if(!(Get-WindowsFeature | where { $_.Name -eq $feature }).Installed) {
Install-WindowsFeature -Name $feature -LogPath "$env:TEMP\init-webservervm_feature_$($feature)_install_log_$((get-date).ToString("yyyyMMddHHmmss")).txt"
}
}
# Make Sure That Remote Connection is Enabled
if((Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\WebManagement\Server\ -Name EnableRemoteManagement).EnableRemoteManagement -eq 0) {
Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\WebManagement\Server\ -Name EnableRemoteManagement -Value 1
}
# Set WMSvc to Automatic Startup
Set-Service -Name WMSvc -StartupType Automatic
# Check If WMSvc (Web Management Service) is running
if((Get-Service WMSvc).Status -ne 'Running') {
Start-Service WMSvc
}
# Install WebPI Products
& "$ScriptRoot\WebPICMD.exe" /Install /Products:"$($webPiProducts -join ',')" /AcceptEULA /Log:"$env:TEMP\webpi_products_install_log_$((get-date).ToString("yyyyMMddHHmmss")).txt"
# Add WMSvc Port Firewall Allow Rule
New-NetFirewallRule -DisplayName "Allow Web Management Service In" -Direction Inbound -LocalPort 8172 -Protocol TCP -Action Allow
# Restart the Web Management Service
Restart-Service WMSvc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment