Label Cloud

Friday, August 25, 2006

First time scripting with Powershell

Powershell (used to be Monad shell) is the great new command line shell from Microsoft. Now in RC1 with RC2 soon to come. Not only is it written in .NET, but .NET framework is at the core of the Powershell functionality. The shell is extremely scriptable, and even thought the language is not 100% C#, it gives you ability to tap into most of the .NET features.

Some things that you were only dreaming about doing in the old command prompt become extremely easy.

I wrote a simple script to let me check which assemblies are compiled with debug.

function IsDebug() {
process
{
trap {continue;}
$asmb = $null;
$asmb = [System.Reflection.Assembly]::ReflectionOnlyLoadFrom($_.FullName);
if ($asmb)
{
$Attribs = [System.Reflection.CustomAttributeData]::GetCustomAttributes($asmb);
foreach ($attrib in $Attribs)
{
if ($attrib.ToString().StartsWith("[System.Diagnostics.DebuggableAttribute") -AND
-NOT $attrib.ToString().StartsWith("[System.Diagnostics.DebuggableAttribute((Boolean)False") )
      {
       $_.Name + " Is DEBUG " + $attrib.ToString();
      }
     }
    }
  }
}

After the script is loaded, all I have to do is pipe a dir commandlet to IsDebug function like

dir | IsDebug($_)


Share/Save/Bookmark

No comments: