Monday, April 25, 2011

Setting the current culture of a website

Rather than relying on the settings of the server, it can be prudent to set the current culture of your ASP.NET application using the following setting:


  ...
  <system.web>
    ...
    <globalization culture="en-NZ" uiCulture="en-NZ" />
    ...
  </system.web>

Thursday, April 21, 2011

Misleading error message in PowerShell script: "Invalid assignment expression. The left hand side of an assignment operator needs to be something that can be assigned to like a variable or a property"

I recently ran into the following error message while modifying a PowerShell script:

Invalid assignment expression. The left hand side of an assignment operator needs to be something that can be assigned to like a variable or a property

The cause of the error was that I had added a line of code in front of my param declaration:

Set-StrictMode -Version 2

param(
    [string][parameter(mandatory=$true )] $WebsitePhysicalPath,

Tuesday, April 19, 2011

Serialise value to XML in one line of C#

This one-liner is useful in the immediate window of Visual Studio to serialise an object to XML:

new XmlSerializer(OBJECT.GetType()).Serialize(new System.IO.StreamWriter(@"c:\temp\x.xml"), OBJECT);

Note that this is not production ready because it does not dispose of objects that should be disposed!

Sunday, April 17, 2011

Event log permissions in Server 2008

Configuring the permissions for an event log in Server 2003 and earlier required processing the SDDL at this registry entry:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Eventlog\Application\CustomSD

To get the SDDL in Server 2008, use the following PowerShell:
$currentSddl = ([xml](wevtutil.exe gl $logName /f:xml)).channel.channelAccess

And to write it back:
wevtutil sl $logName /ca:$newSddl

Tuesday, March 22, 2011

Setting up Unity in a new project

Using the Common Service Locator and Unity is fairly simple with Nuget. This post records the steps in case I need them again.


  1. Add the CommonServiceLocator and Unity packages to the project using Nuget.
  2. Add the following initialisation code (for example in Application_Start):
    UnityContainer container = new UnityContainer();
                
    //configure container
    ...
    
    var unityServiceLocator = new UnityServiceLocator(container);
    ServiceLocator.SetLocatorProvider(() => unityServiceLocator);
    
  3. Use the IServiceLocator and ServiceLocator interface and class for DI and service location, respectively.

Monday, March 21, 2011

Retrieving streamed Flash video on Linux

This blog post on commandlinefu.com shows a fantastic one-liner to retrieve a streaming Flash file as it is being played. It seems that Flash writes to a file, but then deletes that file even as it is being used.

Reproduced here in case the other blog disappears one day:
for h in `find /proc/*/fd -ilname "/tmp/Flash*" 2>/dev/null`; do ln -s "$h" `readlink "$h" | cut -d' ' -f1`; done

Tuesday, March 8, 2011

Finding all explicit ACLs in NTFS

Sometimes file permissions can become a little messed up, so this script will list all the explicitly set (i.e. not inherited) ACEs. I suggest that you use the Out-GridView, Format-Table, or ConvertTo-Csv Cmdlets to better visualise the result.

function Get-ExplicitAcl
{
    [CmdletBinding()]
    param([string]$Path, [switch]$Recurse)
 
    Process
    {
        $allFiles = Get-ChildItem $Path -Recurse:$Recurse | select -ExpandProperty FullName
        $allFiles += Get-Item $Path | select -ExpandProperty FullName
        $result = $allFiles | % {
            $current = $_
            try 
            {
                $acl = get-acl -path $current
                if($acl)
                {
                    $dacl = $acl.GetAccessRules($true, $false, [System.Security.Principal.NTAccount])
                    foreach($ace in $dacl)
                    {
                        $detail = new-object psobject -Property @{ 
                            Path = $current;
                            FileSystemRights = $ace.FileSystemRights;
                            AccessControlType = $ace.AccessControlType;
                            IdentityReference = $ace.IdentityReference;
                            InheritanceFlags = $ace.InheritanceFlags;
                            PropagationFlags = $ace.PropagationFlags;
                        }
                        $detail | select Path, AccessControlType, IdentityReference, FileSystemRights, InheritanceFlags, PropagationFlags
                    }
                }
            }
            catch [System.UnauthorizedAccessException]
            {
                Write-Error "Failed to read DACL from $current.FullName."    
            }
        }
        $result | sort path
    }
}