Label Cloud

Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Monday, August 28, 2006

More Powershell tricks

Two other usefull scriptlets for the powershell

First function filters for only CLR files
Second returns CLR version that the assembly was compiled against.

function IsClr() {
process {
trap {continue;}
$asmb = $null;
$asmb = [System.Reflection.Assembly]::ReflectionOnlyLoadFrom($_.FullName);
if ($asmb)
{ $_ }
}
}

function ClrVersion
{
param ([System.IO.FileInfo] $fileInfo)
$asmb = [System.Reflection.Assembly]::ReflectionOnlyLoadFrom($fileInfo.FullName);
$refAssemblies = $asmb.GetReferencedAssemblies();
foreach ($refAssembly in $refAssemblies)
{
if ($refAssembly.Name -eq "mscorlib")
{
$refAssembly.Version
break;
}
}
}

Use Examples

dir | Clr

dir | IsClr | foreach-object{$V = ClrVersion($_); $_.Name + " " + $V.ToString()}


Share/Save/Bookmark

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
Directory of Computers/Tech Blogs