Monday, December 26, 2011

New Zealand radio stations online

Below are some New Zealand radio stations that can be listened to live online. Let me know if you find any channels I haven't listed here or if any of these links don't work.


And the following live TV channels:

Tuesday, December 6, 2011

Validating a single field using MVC 3 unobtrusive validation

I recently ran into a problem using the jQuery autocomplete control with ASP.NET MVC3 unobtrusive validation. Everything worked correctly except for the scenario where the user clicked an option in the drop-down using the mouse. When this happened, the validation event was not fired against the item the user clicked because no keyboard action had taken place. The solution was to fire the validation event in the select event of the autocomplete control.

To validate the single field using MVC 3 programatically, I used the following JavaScript:
$("form").validate().element($(elementToValidate)); 

Wednesday, November 30, 2011

What's new in PowerShell 3.0 / Windows 8?

After telling some Linux aficionados that PowerShell (finally!) allows you to script most parts of the operating system, I was curious to get a feeling for how far Microsoft has backed this scripting environment in the next version of Windows. I strongly believe that lack of a credible scripting environment has really hampered automation of repetitive tasks, and especially deployments.

Thankfully, it looks like Microsoft has put a lot of effort into the next version of PowerShell on Windows 8.

Below are a list of new aliases, functions, and cmdlets. Note that not all these cmdlets are loaded into the PowerShell session by default - you will need to import the appropriate module first.


Add-AppxPackage
Add-BCDataCacheExtension
Add-BitLockerKeyProtector
Add-BitLockerPassphraseProtector
Add-BitLockerStartupKey
Add-DnsClientNrptRule
Add-Driver
Add-DtcClusterTMMapping
Add-EnrollmentPolicyServer
Add-ExternalKeyProtectorInternal
Add-InitiatorIdToMaskingSet
Add-JobTrigger
Add-KdsRootKey
Add-NetIpHTTPsCertBinding
Add-NetLbfoTeamMember
Add-NetSwitchTeamMember
Add-OdbcDsn
Add-Package
Add-PartitionAccessPath
Add-PasswordProtectorInternal
Add-PhysicalDisk
Add-Printer
Add-PrinterDriver
Add-PrinterPort
Add-ProvisionedAppxPackage
Add-RecoveryPasswordProtectorInternal
Add-SidProtectorInternal
Add-TargetPortToMaskingSet
Add-TpmAndPinAndStartupKeyProtectorInternal
Add-TpmAndPinProtectorInternal
Add-TpmAndStartupKeyProtectorInternal
Add-TpmProtectorInternal
Add-VirtualDiskToMaskingSet
Apply-Unattend
Backup-BitLockerKeyProtector
Backup-Protector
Block-SmbShareAccess
Clear-BCCache
Clear-BitLockerAutoUnlock
Clear-CorruptMountPoint
Clear-Disk
Clear-DNSClientCache
Clear-KdsCache
Clear-Tpm
Close-SmbOpenFile
Close-SmbSession
cnsn
Complete-DtcDiagnosticTransaction
Confirm-SecureBootUEFI
Connect-IscsiDiscoveredTarget
Connect-PSSession
ConvertFrom-Json
ConvertFrom-SecureBootPolicy
ConvertTo-Json
ConvertTo-TpmOwnerAuth
Copy-NetConnectionSecurityRule
Copy-NetFirewallRule
Copy-NetMainModeCryptoSet
Copy-NetMainModeRule
Copy-NetPhase1AuthenticationSet
Copy-NetPhase2AuthenticationSet
Copy-NetQuickModeCryptoSet
Disable-BC
Disable-BCDowngrading
Disable-BCServeOnBattery
Disable-Bitlocker
Disable-BitLockerAutoUnlock
Disable-DAManualSiteSelection
Disable-Feature
Disable-JobTrigger
Disable-MMAgent
Disable-NetAdapter
Disable-NetAdapterBinding
Disable-NetAdapterChecksumOffload
Disable-NetAdapterEncapsulatedPacketTaskOffload
Disable-NetAdapterIPsecOffload
Disable-NetAdapterLso
Disable-NetAdapterPowerManagement
Disable-NetAdapterQos
Disable-NetAdapterRdma
Disable-NetAdapterRsc
Disable-NetAdapterRss
Disable-NetAdapterSriov
Disable-NetAdapterVmq
Disable-NetConnectionSecurityRule
Disable-NetDnsTransitionConfiguration
Disable-NetFirewallRule
Disable-NetIpHTTPsProfile
Disable-NetMainModeRule
Disable-NetNatTransitionConfiguration
Disable-OdbcPerfCounter
Disable-PSTrace
Disable-PSWSManCombinedTrace
Disable-ScheduledJob
Disable-TpmAutoProvisioning
Disable-WdacBidTrace
Disable-WSManTrace
Disconnect-IscsiDiscoveredTarget
Disconnect-PSSession
Dismount-WindowsImage
dnsn
Enable-BCDistributed
Enable-BCDowngrading
Enable-BCHostedClient
Enable-BCHostedServer
Enable-BCLocal
Enable-BCServeOnBattery
Enable-Bitlocker
Enable-BitLockerAutoUnlock
Enable-BitLockerInternal
Enable-DAManualSiteSelection
Enable-Feature
Enable-JobTrigger
Enable-MMAgent
Enable-NetAdapter
Enable-NetAdapterBinding
Enable-NetAdapterChecksumOffload
Enable-NetAdapterEncapsulatedPacketTaskOffload
Enable-NetAdapterIPsecOffload
Enable-NetAdapterLso
Enable-NetAdapterPowerManagement
Enable-NetAdapterQos
Enable-NetAdapterRdma
Enable-NetAdapterRsc
Enable-NetAdapterRss
Enable-NetAdapterSriov
Enable-NetAdapterVmq
Enable-NetConnectionSecurityRule
Enable-NetDnsTransitionConfiguration
Enable-NetFirewallRule
Enable-NetIpHTTPsProfile
Enable-NetMainModeRule
Enable-NetNatTransitionConfiguration
Enable-OdbcPerfCounter
Enable-PSTrace
Enable-PSWSManCombinedTrace
Enable-Sample
Enable-Sample2
Enable-Sample3
Enable-ScheduledJob
Enable-TpmAutoProvisioning
Enable-WdacBidTrace
Enable-WSManTrace
Export-BCCachePackage
Export-BCSecretKey
Export-Certificate
Export-CimCommand
Export-PfxCertificate
Format-SecureBootUEFI
Format-Volume
Get-AppxLastError
Get-AppxPackage
Get-AppxPackageManifest
Get-AutoEnrollmentPolicy
Get-BCClientSettings
Get-BCContentServerSettings
Get-BCDataCache
Get-BCDataCacheExtension
Get-BCHashCache
Get-BCHostedCacheServerSettings
Get-BCNetworkSettings
Get-BCStatus
Get-BitLockerVolume
Get-BitLockerVolumeInternal
Get-Certificate
Get-CertificateNotificationTask
Get-CimAssociatedInstance
Get-CimClass
Get-CimInstance
Get-CimSession
Get-ClusteredScheduledTask
Get-ClusteredScheduledTaskInfo
Get-ControlPanelItem
Get-DAClientExperienceSettings
Get-DAConnectionStatus
Get-DAPolicyChange
Get-DASiteTableEntry
Get-Disk
Get-DNSClient
Get-DNSClientCache
Get-DnsClientEffectiveNrptPolicy
Get-DnsClientNrptGlobal
Get-DnsClientNrptRule
Get-DNSGlobalSettings
Get-DNSServerAddress
Get-Driver
Get-Dtc
Get-DtcAdvancedHostSetting
Get-DtcAdvancedSetting
Get-DtcClusterDefault
Get-DtcClusterTMMapping
Get-DtcDefault
Get-DtcLog
Get-DtcNetworkSetting
Get-DtcTransaction
Get-DtcTransactionsStatistics
Get-DtcTransactionsTraceSession
Get-DtcTransactionsTraceSetting
Get-Edition
Get-EncryptableVolume
Get-EncryptableVolumes
Get-EnrollmentPolicyServer
Get-ExceptionForHrInternal
Get-Feature
Get-InitiatorId
Get-InitiatorPort
Get-IscsiConnection
Get-IscsiDiscoveredTarget
Get-IscsiPersistentTarget
Get-IscsiSession
Get-IscsiTargetPortal
Get-JobTrigger
Get-KdsConfiguration
Get-KdsRootKey
Get-LogProperties
Get-MaskingSet
Get-NCSIPolicyConfiguration
Get-Net6to4Configuration
Get-Net6to4State
Get-NetAdapter
Get-NetAdapterAdvancedProperty
Get-NetAdapterBinding
Get-NetAdapterChecksumOffload
Get-NetAdapterEncapsulatedPacketTaskOffload
Get-NetAdapterHardwareInfo
Get-NetAdapterIPsecOffload
Get-NetAdapterLso
Get-NetAdapterPowerManagement
Get-NetAdapterQos
Get-NetAdapterRdma
Get-NetAdapterRsc
Get-NetAdapterRss
Get-NetAdapterSriov
Get-NetAdapterSriovVf
Get-NetAdapterStatistics
Get-NetAdapterVmq
Get-NetAdapterVMQQueue
Get-NetAdapterVPort
Get-NetAddressFilter
Get-NetApplicationFilter
Get-NetConnectionSecurityRule
Get-NetDnsTransitionConfiguration
Get-NetDnsTransitionMonitoring
Get-NetFirewallProfile
Get-NetFirewallRule
Get-NetInterfaceFilter
Get-NetInterfaceTypeFilter
Get-NetIPAddress
Get-NetIpHTTPsConfiguration
Get-NetIpHTTPsState
Get-NetIPInterface
Get-NetIPv4Protocol
Get-NetIPv6Protocol
Get-NetISATAPConfiguration
Get-NetISATAPState
Get-NetLbfoTeam
Get-NetLbfoTeamMember
Get-NetLbfoTeamNic
Get-NetMainModeCryptoSet
Get-NetMainModeRule
Get-NetMainModeSA
Get-NetNatTransitionConfiguration
Get-NetNatTransitionMonitoring
Get-NetNeighbor
Get-NetOffloadGlobalSetting
Get-NetPhase1AuthenticationSet
Get-NetPhase2AuthenticationSet
Get-NetPrefixPolicy
Get-NetProtocolPortFilter
Get-NetQosPolicy
Get-NetQuickModeCryptoSet
Get-NetQuickModeSA
Get-NetRoute
Get-NetSecurityFilter
Get-NetSecuritySettingData
Get-NetServiceFilter
Get-NetSwitchTeam
Get-NetSwitchTeamMember
Get-NetTCPConnection
Get-NetTCPSetting
Get-NetTeredoConfiguration
Get-NetTeredoState
Get-NetTransportFilter
Get-NetUDPConnection
Get-NetUDPSetting
Get-OdbcDriver
Get-OdbcDsn
Get-OdbcPerfCounter
Get-Package
Get-Partition
Get-PartitionAccessPath
Get-PartitionSupportedSizes
Get-PfxData
Get-PhysicalDisk
Get-PrintConfig
Get-Printer
Get-PrinterDriver
Get-PrinterPort
Get-PrintJob
Get-Protectors
Get-ProvisionedAppxPackage
Get-RecoveryKeyProtectorsCountInternal
Get-ScheduledJob
Get-ScheduledJobOption
Get-ScheduledTask
Get-ScheduledTaskInfo
Get-SecureBootUEFI
Get-SmbClientConfiguration
Get-SmbClientNetworkInterface
Get-SmbConnection
Get-SmbConnectionNetworkInterface
Get-SmbOpenFile
Get-SmbServerConfiguration
Get-SmbServerNetworkInterface
Get-SmbSession
Get-SmbShare
Get-SmbShareAccess
Get-SmbWitnessCluster
Get-SmbWitnessClusterClient
Get-SmbWitnessClusterResource
Get-StorageAttributes
Get-StoragePool
Get-StorageProvider
Get-StorageSettings
Get-StorageSubSystem
Get-TargetPort
Get-Tpm
Get-TypeData
Get-VirtualDisk
Get-VirtualDiskSupportedSizes
Get-Volume
Get-WdacBidTrace
Get-Win32EncryptableVolumeInternal
Get-WindowsImage
Grant-SmbShareAccess
Hide-VirtualDisk
Import-BCCachePackage
Import-BCSecretKey
Import-Certificate
Import-PfxCertificate
Import-PSWorkflow
Import-TpmOwnerAuth
Initialize-Disk
Initialize-Tpm
Initialize-Volume
Install-Dtc
Install-SecureBootPolicy
Invoke-CimMethod
Invoke-RestMethod
Invoke-WebRequest
irm
iwr
Join-DtcDiagnosticResourceManager
Lock-BitLocker
Mount-WindowsImage
Move-SmbWitnessClusterClient
New-CertificateNotificationTask
New-CimInstance
New-CimSession
New-CimSessionOption
New-DASiteTableEntry
New-DtcDiagnosticTransaction
New-IscsiTargetPortal
New-JobTrigger
New-MaskingSet
New-NetAdapterAdvancedProperty
New-NetAuthenticationProposal
New-NetConnectionSecurityRule
New-NetFirewallRule
New-NetIPAddress
New-NetIpHTTPsConfiguration
New-NetLbfoTeam
New-NetLbfoTeamNic
New-NetMainModeCryptoProposal
New-NetMainModeCryptoSet
New-NetMainModeRule
New-NetNatTransitionConfiguration
New-NetNeighbor
New-NetPhase1AuthenticationSet
New-NetPhase2AuthenticationSet
New-NetPrefixPolicy
New-NetQosPolicy
New-NetQuickModeCryptoProposal
New-NetQuickModeCryptoSet
New-NetRoute
New-NetSwitchTeam
New-NetTransportFilter
New-Partition
New-PSSessionConfigurationFile
New-PSTransportOption
New-PSWorkflowExecutionOption
New-ScheduledJobOption
New-ScheduledTask
New-ScheduledTaskAction
New-ScheduledTaskPrincipal
New-ScheduledTaskSettings
New-ScheduledTaskTrigger
New-SelfSignedCertificate
New-SmbConnection
New-SmbShare
New-StoragePool
New-StorageSubSystemVirtualDisk
New-VirtualDisk
New-VirtualDiskClone
New-VirtualDiskSnapshot
New-WinEvent
Open-NetGPO
Optimize-Volume
oss
Publish-BCFileHashes
Publish-BCWebHashes
rcsn
Receive-DtcDiagnosticTransaction
Receive-PSSession
Register-CimIndicationEvent
Register-ClusteredScheduledTask
Register-IscsiPersistentTarget
Register-JobEvent
Register-ScheduledJob
Register-ScheduledTask
Remove-AppxPackage
Remove-BCDataCacheExtension
Remove-BitlockerKeyProtector
Remove-CertificateNotificationTask
Remove-CimInstance
Remove-CimSession
Remove-DASiteTableEntry
Remove-DnsClientNrptRule
Remove-Driver
Remove-DtcClusterTMMapping
Remove-EnrollmentPolicyServer
Remove-InitiatorId
Remove-InitiatorIdFromMaskingSet
Remove-IscsiTargetPortal
Remove-JobTrigger
Remove-KeyProtectorByTypeInternal
Remove-MaskingSet
Remove-NetAdapterAdvancedProperty
Remove-NetConnectionSecurityRule
Remove-NetFirewallRule
Remove-NetIPAddress
Remove-NetIpHTTPsCertBinding
Remove-NetIpHTTPsConfiguration
Remove-NetLbfoTeam
Remove-NetLbfoTeamMember
Remove-NetLbfoTeamNic
Remove-NetMainModeCryptoSet
Remove-NetMainModeRule
Remove-NetMainModeSA
Remove-NetNatTransitionConfiguration
Remove-NetNeighbor
Remove-NetPhase1AuthenticationSet
Remove-NetPhase2AuthenticationSet
Remove-NetPrefixPolicy
Remove-NetQosPolicy
Remove-NetQuickModeCryptoSet
Remove-NetQuickModeSA
Remove-NetRoute
Remove-NetSwitchTeam
Remove-NetSwitchTeamMember
Remove-NetTransportFilter
Remove-OdbcDsn
Remove-Package
Remove-Partition
Remove-PartitionAccessPath
Remove-PhysicalDisk
Remove-Printer
Remove-PrinterDriver
Remove-PrinterPort
Remove-PrintJob
Remove-ProvisionedAppxPackage
Remove-SmbConnection
Remove-SmbShare
Remove-StoragePool
Remove-TargetPortFromMaskingSet
Remove-TypeData
Remove-VirtualDisk
Remove-VirtualDiskFromMaskingSet
Rename-Computer
Rename-DASiteTableEntry
Rename-MaskingSet
Rename-NetAdapter
Rename-NetConnectionSecurityRule
Rename-NetFirewallRule
Rename-NetIpHTTPsConfiguration
Rename-NetLbfoTeam
Rename-NetMainModeCryptoSet
Rename-NetMainModeRule
Rename-NetPhase1AuthenticationSet
Rename-NetPhase2AuthenticationSet
Rename-NetQuickModeCryptoSet
Rename-NetSwitchTeam
Rename-Printer
Repair-SecureBootPolicy
Repair-VirtualDisk
Repair-Volume
Repair-WindowsImage
Reset-BC
Reset-DAClientExperienceSettings
Reset-DASiteTableEntry
Reset-DtcLog
Reset-NCSIPolicyConfiguration
Reset-Net6to4Configuration
Reset-NetAdapterAdvancedProperty
Reset-NetDnsTransitionConfiguration
Reset-NetIpHTTPsConfiguration
Reset-NetISATAPConfiguration
Reset-NetNatTransitionConfiguration
Reset-NetRoute
Reset-NetTeredoConfiguration
Resize-Partition
Resize-VirtualDisk
Resolve-DnsName
Restart-NetAdapter
Restart-PrintJob
Resume-BitLocker
Resume-Job
Resume-PrintJob
Revoke-SmbShareAccess
rujb
Save-Help
Save-NetGPO
Save-WindowsImage
Send-DtcDiagnosticTransaction
Set-AutoEnrollmentPolicy
Set-BCAuthentication
Set-BCCache
Set-BCMinSMBLatency
Set-BCSecretKey
Set-BitLockerVolumeInternal
Set-CimInstance
Set-ClusteredScheduledTask
Set-DAClientExperienceSettings
Set-DASiteTableEntry
Set-Disk
Set-DNSClient
Set-DnsClientNrptGlobal
Set-DnsClientNrptRule
Set-DNSGlobalSettings
Set-DtcAdvancedHostSetting
Set-DtcAdvancedSetting
Set-DtcClusterDefault
Set-DtcClusterTMMapping
Set-DtcDefault
Set-DtcLog
Set-DtcNetworkSetting
Set-DtcTransaction
Set-DtcTransactionsTraceSession
Set-DtcTransactionsTraceSetting
Set-Edition
Set-InitiatorPort
Set-IscsiInitiator
Set-JobTrigger
Set-KdsConfiguration
Set-LogProperties
Set-MMAgent
Set-NCSIPolicyConfiguration
Set-Net6to4Configuration
Set-NetAdapter
Set-NetAdapterAdvancedProperty
Set-NetAdapterBinding
Set-NetAdapterChecksumOffload
Set-NetAdapterEncapsulatedPacketTaskOffload
Set-NetAdapterIPsecOffload
Set-NetAdapterLso
Set-NetAdapterPowerManagement
Set-NetAdapterQos
Set-NetAdapterRdma
Set-NetAdapterRsc
Set-NetAdapterRss
Set-NetAdapterSriov
Set-NetAdapterVmq
Set-NetAddressFilter
Set-NetApplicationFilter
Set-NetConnectionSecurityRule
Set-NetDnsTransitionConfiguration
Set-NetFirewallProfile
Set-NetFirewallRule
Set-NetInterfaceFilter
Set-NetInterfaceTypeFilter
Set-NetIPAddress
Set-NetIpHTTPsConfiguration
Set-NetIPInterface
Set-NetIPv4Protocol
Set-NetIPv6Protocol
Set-NetISATAPConfiguration
Set-NetLbfoTeam
Set-NetLbfoTeamMember
Set-NetLbfoTeamNic
Set-NetMainModeCryptoSet
Set-NetMainModeRule
Set-NetNatTransitionConfiguration
Set-NetNeighbor
Set-NetOffloadGlobalSetting
Set-NetPhase1AuthenticationSet
Set-NetPhase2AuthenticationSet
Set-NetPrefixPolicy
Set-NetProtocolPortFilter
Set-NetQosPolicy
Set-NetQuickModeCryptoSet
Set-NetRoute
Set-NetSecurityFilter
Set-NetSecuritySettingData
Set-NetServiceFilter
Set-NetTCPSetting
Set-NetTeredoConfiguration
Set-NetUDPSetting
Set-OdbcDriver
Set-OdbcDsn
Set-Partition
Set-PhysicalDisk
Set-PrintConfig
Set-Printer
Set-ProductKey
Set-ScheduledJob
Set-ScheduledJobOption
Set-ScheduledTask
Set-SecureBootUEFI
Set-SmbClientConfiguration
Set-SmbServerConfiguration
Set-SmbShare
Set-StorageAttributes
Set-StoragePool
Set-StorageSettings
Set-StorageSubSystem
Set-TpmOwnerAuth
Set-VirtualDisk
Set-Volume
Show-BitLockerRequiredActionsInternal
Show-BitLockerStatus
Show-Command
Show-ControlPanelItem
Show-NetConnectionSecurityRule
Show-NetFirewallRule
Show-VirtualDisk
sls
Start-Dtc
Start-DtcDiagnosticResourceManager
Start-DtcTransactionsTraceSession
Start-ScheduledTask
Start-Trace
Stop-Dtc
Stop-DtcDiagnosticResourceManager
Stop-DtcTransactionsTraceSession
Stop-ScheduledTask
Stop-Trace
sujb
Suspend-BitLocker
Suspend-Job
Suspend-PrintJob
Switch-Certificate
Sync-NetConnectionSecurityRule
TabExpansion2
Test-BitLocker
Test-BitLocker2
Test-Certificate
Test-Dtc
Test-KdsRootKey
Test-PhysicalDisk
Test-PSSessionConfigurationFile
Test-SecureBootPolicy
Test-SystemEntropyForBitLockerInternal
Test-TpmForBitLockerInternal
Test-TpmProtectorNeededInternal
Unblock-File
Unblock-SmbShareAccess
Unblock-Tpm
Undo-DtcDiagnosticTransaction
Uninstall-Dtc
Unlock-AdAccountOrGroupInternal
Unlock-BitLocker
Unlock-PasswordInternal
Unlock-RecoveryKeyInternal
Unlock-RecoveryPasswordInternal
Unregister-ClusteredScheduledTask
Unregister-IscsiPersistentTarget
Unregister-ScheduledJob
Unregister-ScheduledTask
Update-Help
Update-HostStorageCache
Update-IscsiDiscoveredTarget
Update-IscsiTargetPortal
Update-NetConnectionSecurityRule
Update-StorageProviderCache
Use-Unattend
Write-DtcTransactionsTraceSession
gmo -l | select -expandproperty name | % { ipmo $_ }
gcm | select -ExpandProperty name | sort | clip
Compare-Object (type .\win7.txt | sort) (type .\win8.txt | sort) | select -ExpandProperty inputobject | select -Unique | % {"$_
"} | clip

Thursday, November 24, 2011

Protecting against CSRF in MVC 3

It is important to protect your customers and other users of your website against CSRF attacks. To do this in ASP.NET MVC 3, you would normally use the ValidateAntiForgeryTokenAttribute. The test below will ensure that all appropriate actions on all controllers have this protection.
[TestMethod]
public void All_post_actions_validate_anti_forgery_tokens()
{
    var modelController = typeof(MyController);  //this can be any one of your controllers in this particular area
    var allControllers = modelController.Assembly.GetTypes().Where(t => t.Namespace == modelController.Namespace && t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase));
    foreach (var controller in allControllers)
    {
        AssertControllerValidatesAntiForgeryToken(controller);
    }
}

static void AssertControllerValidatesAntiForgeryToken(Type controller)
{
    Console.WriteLine("Checking " + controller.Name);
    var violations = (
        from method in controller.GetMethods()
        let attributes = method.GetCustomAttributes(true)
        let parameters = method.GetParameters()
        let isProperty = !method.Name.StartsWith("set_", StringComparison.OrdinalIgnoreCase) && !method.Name.StartsWith("get_", StringComparison.OrdinalIgnoreCase)
        let isPostMethod = parameters.Length != 0 && (!attributes.Any(a => a is ActionMethodSelectorAttribute) || attributes.Any(a => a is HttpPostAttribute))
        let noAntiForgeryToken = !attributes.Any(a => a is ValidateAntiForgeryTokenAttribute)
        let excludedMethods = typeof(ControllerBase).GetMethods().Select(m => m.Name)
        let isExcludedMethod = excludedMethods.Contains(method.Name)
        where isProperty && isPostMethod && noAntiForgeryToken && !isExcludedMethod
        select method.Name).ToArray();

    foreach (var violation in violations)
        Console.WriteLine("Failed: {0}/{1}", controller.Name, violation);

    Assert.AreEqual(0, violations.Count());
}         

Sunday, November 13, 2011

Printing customised top maps from LINZ

Land Information New Zealand (LINZ) has topographical maps that cover New Zealand available for free download.

I am planning a trip over the Tongariro Crossing, and wanted to print a topo map that covers the walk. Unfortunately, as so often happens, the walk traverses two maps. To solve this problem, I decided to merge two maps and then print only the appropriate part of the map.

To merge the maps, the simplest method was to download the two maps in GeoTiff format and merge them using the GDAL tools, which are compiled by Frank Warmerdam into a handy Windows package:

gdal_merge -v -q -o merged.tif -of GTiff BH34_GeoTifv1-01.tif BH35_GeoTif_1-00.tif

The next step was to crop the appropriate part of the map so that the scale was preserved when I printed it on A3 paper. A key point to note is that the original LINZ GeoTiff is set to 300 DPI or 118.11 pixels/cm. In the drawing tool I used (Paint.NET), I was able to set the DPI to 300 and then set a crop area of 42 x 29.7 cm to select the area I wanted. Bear in mind that many printers are not able to print onto the full paper size, so a smaller area may be required to preserve the scale.

Note that the -v -q arguments were required for the version of FWTools that I downloaded due to a known issue in GDAL.

Useful Software

This is intended to be a general purpose list of useful software I come across, and complements my list of Windows Software I can't live without.

I want to...

So I use...
Join two topo maps in geotiff format. GDAL Utilities
Draw images (scalable) Inkscape
Edit photos (scalable) Gimp
Bulk manipulation of photos Imagemagick
Automatic rotation of photos from exif data jhead -autorot *.jpg
Finding which files / folders are using my disk space WinDirStat
Photo editing GIMP
Generating an xsd from an xml file. TRANG
Determining motherboard / RAM configurations Speccy
Burn CD / DVD / ISO CDBurnerXP
Screen sharing / assistance join.me

Sunday, November 6, 2011

Orcon Genius wireless connection drops when using the phone

I am using the Orcon Genius, and so far have been pretty happy with the reduced telephone bill and features of the Genius.

However, I did run into an unusual problem where the wireless connection to my laptop would cut out whenever I answered an incoming call on the handset; surprisingly, outgoing calls did not affect the wireless connection.

After troubleshooting with the (very friendly) Orcon technician, the solution turned out to be to change the wireless channel setting from "Auto" to "6".

To do this, I:

  1. Pointed my browser at http://10.1.1.1.
  2. Entered my password (I believe the default is 'admin', but don't forget to change it) to log in.
  3. Clicked on "2. Set up my wireless".
  4. Set the drop down box to the right of "Channel" to 6.
  5. Clicked "Save settings".

Problem solved!

Thursday, October 27, 2011

Listing all columns in a SQL Server database that have empty strings in them using PowerShell

I needed to find all the columns in a database that have empty strings in them so that we could track down the source of this (these?) data. This proved to be fairly straightforward using PowerShell, but is still something that I will jot down here to save time in case I need to do the same again in the future.

To run this one-liner, open SQL Server Management Studio and right click on the database you want to run it on, then choose Start PowerShell.

Modify the following command and run it in the PowerShell window you just opened. Note that it may be possible to write a simpler command than this, but this one is simple and it works.

dir | % { dir ($_.displayname + '\columns') } | ? { $_.datatype -match 'varchar' } | select parent, datatype, name | % { invoke-sqlcmd -suppressprovidercontextwarning ("select '" + $_.parent + "', '" + $_.name + "', count(*) from " + $_.parent + " where `"" + $_.name + "`" = ''")  } | ? { $_[2] -ne 0 }

Monday, October 24, 2011

x64 assembly sample program

To get a better understanding of how the new Microsoft x64 calling convention works, I thought it would be a good idea to reacquaint myself with assembly language, this time using ml64.exe. While I do not write assembly language in my day-to-day job, I still find it surprisingly useful to be able to read and decipher assembly language. Understanding calling conventions and stack usage are a critical part of being able to debug assembly language.

A key takeaway for me from this process is that the x64 convention passes the first four arguments in volatile registers, therefore making it difficult to review the parameters as passed to a function because they will not be stored in memory. Note that space is allocated on the stack for the parameters that are passed via register, but the memory is generally left uninitialised in release-mode builds. The Advanced Windows Debugging and Troubleshooting blog has a good run-down of the difficulties this poses.

Below is a sample program I wrote, along with the command line to compile it:

extrn ExitProcess: PROC 
extrn MessageBoxA: PROC
extrn WriteConsoleA: PROC
extrn AllocConsole: PROC
extrn GetStdHandle: PROC

.data
caption db 'x64 Assembly Program', 0
message db 'Hello World!', 0

.code
main proc
  sub rsp, (5 + 1 + 1) * 8 ;5 parameters + return + stdhandle + [written length]; 

  call AllocConsole
  test rax, rax
  jz error

  mov rcx, 0FFFFFFF5h
  call GetStdHandle ;eax = handle

  mov rcx, rax
  lea rdx, message
  mov r8, 12 ; length of string
  lea r9, [rsp + 6 * 8]
  mov qword ptr [rsp + 4 * 8], 0

  call WriteConsoleA

  call DisplayMessage

  add rsp, 56
  ret

error:
  mov rcx, -1
  call ExitProcess
main endp

DisplayMessage proc
  sub rsp, 28h
  mov   rcx, 0 
  lea   rdx, message
  lea   r8, caption
  mov   r9d, 0
  call  MessageBoxA
  add   rsp, 28h
  ret
DisplayMessage endp
end

This is the command line that can be used to compile the code above:

ml64.exe /Zi Prog.asm /link /subsystem:windows /defaultlib:kernel32.lib /defaultlib:user32.lib /entry:main

Note that, in my installation of Visual Studio 2010, ml64.exe is in the C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\x86_amd64 folder. Make sure that you use the x64 version of the Visual Studio command prompt, otherwise the link process will silently fail.

Sunday, October 16, 2011

Backing up a Blogger blog

There seems to have been a flurry of activity around 2008 with a lot of tools being written to back up Blogger blogs, but not much since then. After a bit of searching, I found jblogbackup, which seems to be a reliable, command-line based solution. To get this to work, I needed to do the following:

  1. Download the Java RTE and add the bin folder to my path environment variable.
  2. Download jblogbackup.
  3. Set the Feedburner feed for my blog to download more than 25 entries by clicking on "Edit Feed Details…" and setting Original Feed to http://nzbart.blogspot.com/feeds/posts/default?max-results=999. Hopefully 999 entries will last a while.
  4. Run the tool:
    java -jar jblogbackup-onejar-1.1.jar -b BloggerBackup -c -imgthreads 4 -n nzbart

Wednesday, October 12, 2011

Converting a short (8.3) file name into a long file name in PowerShell

It took me a while to find, but converting a short DOS style (8.3) Windows file name into a long file name in PowerShell is easy. For example, my system temp folder was configured with my user folder as a short name, so I used the following to retrieve the long name:

(get-item $env:temp).FullName

Wednesday, October 5, 2011

Demonstrating the effect of CPU pipelining on performance

When writing high performance applications, you may need to understand some of the fundamentals of CPU architecture, such as CPU pipelining. Often it can be most useful to see the effect of pipelining in a simple example.

The full source code for this example is below, but the most important part is the assembly code and results.

The test uses the Windows high performance timer to see how long it takes to execute a function written in Intel x64 assembly.

The simple loop below sets the rax register to a large number (in this case 2.33 billion, since I am running on a 2.33 GHz processor), and repeatedly decreases it until it hits zero. My console application then printed out the number of milliseconds this took to execute. On my machine, it took roughly 1000 ms to execute, even though it is executing a decrement instruction followed by a jump.
.CODE             

runAssemblyCode PROC
  mov rax, 2330 * 1000 * 1000
 start:
  dec rax
  jnz start
  ret 
runAssemblyCode ENDP 
END

To complicate things even more, here I perform more, independent, operations in the same loop. Instead of 1 decrement, I now do 5 on various different registers. The total time taken is only about 2000 milliseconds.

.CODE             

runAssemblyCode PROC
  mov rax, 2330 * 1000 * 1000
 start:
  dec rcx
  dec rdx
  dec r9
  dec r10
  dec rax
  jnz start
  ret 
runAssemblyCode ENDP 
END

As to the finer details of Intel CPU pipelining, I could not explain how to calculate the expected execution time for these examples, but they do demonstrate quite well that a sequence of carefully crafted instructions can execute faster than the clock speed of your CPU would have you believe.

To run this application yourself, simply create a Visual Studio 2010 (or newer) C++ project and create two files - one .ASM file with the assembly above, and one .CPP file with the C++ code below. You will also need to enable the MASM build customisation by right clicking on the project and clicking Build Customisations.

#include <Windows.h>
#include <memory>
#include <iostream>

using namespace std;

extern "C" void runAssemblyCode();

class Timer
{
public:
    Timer()
    {
        QueryPerformanceFrequency(&_ticksPerSecond);
        Reset();
    }

    void Reset()
    {
        QueryPerformanceCounter(&_startedAt);
    }

    long long GetElapsedMilliseconds()
    {
        LARGE_INTEGER now;
        QueryPerformanceCounter(&now);
        return (now.QuadPart - _startedAt.QuadPart) * 1000 / _ticksPerSecond.QuadPart;
    }

private:
    LARGE_INTEGER _startedAt;
    LARGE_INTEGER _ticksPerSecond;
};

int wmain(int argc, wchar_t* argv[])
{
    Timer timer;

    runAssemblyCode();

    auto elapsed = timer.GetElapsedMilliseconds();
    cout << elapsed << endl;

    return 0;
}

Tuesday, October 4, 2011

Problem debugging stored procedures / T-SQL on (local) databases

If you run into the following error while trying to debug T-SQL in SQL Server Management Studio, the problem is that the DNS name (local) cannot be resolved:

Unable to start T-SQL Debugging. Could not connect to computer '(local)'. The requested name is valid, but no data of the requested type was found.

The solution is to connect to localhost rather than (local).

Monday, October 3, 2011

Comparing TFS source branch with destination branch

When a TFS branch is not "enhanced" with visualisations, it can be tricky to determine the version of the source branch.

I recently wanted to find all the changes that had been made in a branch, including some tweaks that had been made when the branch action was initially checked in. The most reliable way to do this is to compare the source branch at the revision that the branch was made with the latest code in the branch. To do this, I wanted to use the following command line:

tf folderdiff "$/SourceBranch;RevisionThatBranchWasTakenFrom" $/DestinationBranch

However, I needed a way to determine RevisionThatBranchWasTakenFrom. Since branching in TFS is essentially copying (the metadata of) each file in the source branch to the destination, there is no single concept of the 'revision' of the source branch at the point when the branch was made (in contrast to Subversion). The best I could come up with was to find the most recent changeset in the source branch before the branch was made. To do this, I used the tf merges command on the destination branch, which lists all the files that have been merged into the destination branch. I have made several assumptions, including that there was a single branch into the destination and no subsequent merges:

tf merges $/SourceBranch /r /showall /format:detailed | % {$_ -match '(\d+) ->' | out-null; $Matches[1]} | sort -Descending | select -first 1

This PowerShell one-liner will return the highest revision number of all the branched files, assuming that nothing untoward happens and it can parse the output of tf merges. In our case, I knew that all the changesets in question had four digits, so the string based sort would work fine.

Now that I had the most recent changeset, I could run the command to get the changes:
tf folderdiff "$/SourceBranch;C12345" $/DestinationBranch