Thursday 20 May 2010

Disable the Output Cache Programmatically with PowerShell

Restoring a content database from a production environment to a development machine for dev purposes can sometimes get a little bit interesting.

Our production environments generally have output caching enabled for performance reasons(see /_Layouts/sitecachesettings.aspx in your own environment) but developing or troubleshooting an issue in dev normally requires output caching be disabled to you can view the results of your work. Of course the output cache settings are stored within the site collection so using a production backup will normally bring the output cache setting along with everything else.

Of course you can manually disable output caching but that means remembering to do so. As our attach process is scripted in a batch file for repeatability and automation purposes, I recently added a step to disable the output cache programmatically. To do this, I wrote a little PowerShell script and call it from the batch file:

Param($Url)

write-host "User must be a site collection administrator or have full control within web application policy!"

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Publishing”)

$cacheSettings = new-object Microsoft.SharePoint.Publishing.SiteCacheSettingsWriter($Url);
$cacheSettings.EnableCache = $false;

$cacheSettings.Update();

The script takes a single named parameter (Url) which specifies the site collection URL. As you can see, I'm working with the SiteCacheSettingsWriter class to set the EnableCache property to false before sending through an Update() command; of course the same thing can easily be accomplished in C#.

I call the script like so from my batch script:

powershell "& ./DisableOutputCache.ps1 -Url http://dev-moss"

I've parameterised this script since I restore content databases from multiple sites and didn't want the URL hard-coded.

As per my note, the account used to execute this script must be a site collection owner or have rights configured the web application policy or you'll encounter an access denied error.

 

How to supply named arguments to a PowerShell script

Calling a parameterised PowerShell script from a batch file or command window using name parameters is pretty easy, just flag each paramater name and supply a value like so:

powershell.exe "& ./MyScript.ps1 -MyParam1 'MyValue 1' –MyParam2 MyValue2"

Note I’ve wrapped MyValue1 in single quotes because of the space; the single quotes are stipped off the argument when it’s presented to the script.

MyScript.ps1 would use the parameters like so:

Param($MyParam1, $MyParam2)
write-host $MyParam1
write-host $MyParam2

The Param statement declares the formal parameters accepted by the script and must be the first executable line of code in the script. The parameters can now be used like any other variable, as demonstrated by the two write-host calls.

Disclaimer: I’m a PowerShell rookie! 

Building SharePoint 2010 Solutions with Visual Studio 2010

[Update: a video recording of this presentation is now available here]

I’m presenting the second of tonight’s Perth SharePoint User Group sessions:

Building SharePoint 2010 Solutions with Visual Studio 2010

Description:

SharePoint 2010 development is now a first class citizen with a new level of tool support in Visual Studio 2010. This session will demonstrate the basics steps to create a SharePoint project, configure a solution package, and create and work with project items like features and the new visual web part. Debugging, LINQ to SharePoint, and considerations for working with stand-alone applications will also be discussed.

Target Audience: Developers

Hope to see you there and don’t forget to register (for catering purposes) if you’re planning to attend: http://perthsharepointmay2010.eventbrite.com/

Thursday 6 May 2010

How to merge Hyper-V snapshots to a parent .vhd

Jason Neurohr has a detailed, illustrated post on the subject of merging Hyper-V snapshots to a parent .vhd, leaving you with a single .vhd file you can run without having to worry about the snapshots. I found Jason’s post excellent and the steps described worked like a charm the first time I had to do this (while I didn’t persist, I found exporting a VM with snapshots from the Hyper-V R2 console just wasn’t working—the snapshots weren’t exported as expected).

The merge process is actually quite simple and I’m therefore reproducing just the relevant details from Jason’s post for my own future benefit:

  • Backup the parent .vhd and all snapshot disks (.avhd)
  • Change the extension for all snapshot differencing disks from .avhd to vhd
  • Within the Hyper-V MMC, select the Edit Disk… option
  • On the Locate Disk screen, locate the snapshot disk to be merged
  • Select the Merge option and then the To the parent virtual hard disk option
  • Finish the wizard and create a new virtual machine using the merged disk

If multiple snapshots exist, merge the most recent snapshot into the parent disk and work in reverse snapshot date order all the way back to the parent.

Tuesday 4 May 2010

Hassles installing Office 2010

Had a painful time trying to get Office 2010 Professional Plus (MSDN RTM) installed in a Windows Server 2008 R2 environment. Attempting to run a customised install including Outlook, Word, Excel and PowerPoint died repeatedly after seemingly getting two thirds of the way through:

Error 1935. An error occurred during the installation of assembly component {89EDD3A9-944B-3257-8484-D6EB6A00DDF5}. HRESULT: 0x80070BC9.

The event log wasn’t any more helpful.

In the end, installing each application one-by-one—although tedious—did the trick.