Powershell optimization

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.

About erictummers

My work as a recruited developer changes almost every month. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s