PowerShell : Searching for Controls Without a Fully-qualified Type Name

Today’s post is a quick tip. I’m working with some older code where a lot of the ascx files don’t have a fully qualified type name in the Inherits attribute of the Control directive. I wanted to find them without having to open dozens of files and manually search for offenders:

function Find-Controls-Without-PublicKeyToken([string]$directory) {
    $files = dir $directory -Recurse -Filter *.ascx
    $files | where { No-Publickeytoken($_) } | foreach { $_.FullName }
}

This is a pretty straightforward function, it finds all the user control files in a given directory or any of its subdirectories, filters the list using No-Publickeytoken and prints out the paths to the offending files. And here’s No-Publickeytoken:

function No-Publickeytoken($file) {
    $firstLine = Get-Content $_.FullName -TotalCount 1
    ($firstLine -match “Inherits”) -and `
        (-not ($firstLine -match “PublicKeyToken”))
}

This function gets the first line of the file passed to it and then returns true if the line contains the word “Inherits” but doesn’t contain the word “PublicKeyToken”.

Hope this is useful to someone, it certainly saved me a bunch of repetitive manual work.

Ref : SharePoint Blues

Advertisements

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