They will work if you change this line:
$webApplication = Get-SPWebApplication $WebApplicationUrl
to these two lines each place the above occurs:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$webApplication = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($webapplicationUrl)
Mike Smith TechTrainingNotes.blogspot.com