After rewriting my PowerShell test environment automation script with functions I optimized some parts to improve memory usage, automate IIS Webfarm configuration and batch starting/stopping all machines is a Azure Cloud Service.
Download
For installation of software the script downloads the MSI from Azure storage. The download is done with Invoke-WebRequest which generates a lot of output. I’ve seen over 6Gb of memory usage to hold this output in a job even with Receive-Job being called every 15 seconds.
Now I’m using the .NET webclient and memory usage is back to normal. Since the installation should be a black box I only want to know when it fails and not how much bits are downloaded.
$client = New-Object net.webclient $client.Downloadfile($file, $msi)
Webfarm
During creation of the VM I run a custom script to install IIS. This however does not configure IIS for a webfarm setup. Most important is the machinekey setting for encryption/decryption of the viewstate.
# add masterkey to machine.config for webfarm $configFile = "$ENV:windir\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config" $xml=New-Object XML $xml.Load($configFile) $machinekey = $xml.CreateElement("machineKey") $machinekey.SetAttribute("decryption", "AES") $machinekey.SetAttribute("decryptionKey", "DECRYPTION_KEY_HERE") $machinekey.SetAttribute("validation", "SHA1") $machinekey.SetAttribute("validationKey", "VALIDATION_KEY_HERE") $machinekey.SetAttribute("compatibilityMode", "Framework45") $xml.configuration.'system.web'.AppendChild($machinekey) | Write-Debug $xml.Save($configFile)
To generate the keys use one of the references from this post: Setting up a machine key.
Batch start/stop
After creating a lot of machines for our test we sometimes want to keep the VM’s. When the machines are running they cost money, but once stopped only the storage is billed. Stopping all VM’s in a Azure Cloud Service can be done by using a wildcard. MSDN documentation states this is not supported but it works.
# stop all VM in a Cloud service Stop-AzureVM -ServiceName $service -Name * -Force # start all VM in a Cloud service Start-AzureVM -ServiceName $service -Name *
You’ll need at least Windows Azure PowerShell 0.6.19. Best is to install the latest version from here.