Friday, September 11, 2015

PowerShell Tip - Manage .NET Assemblies in your session

The more I use PowerShell, the more I become a an automation fan!!!
PowerShell provides me the ability to automate almost everything I encounter in my daily work. Most of the time the cmdlets are available as part of the snapins, but sometimes you need to use the functionalities in a .NET assembly that should be loaded into the session and utilize the functionality provided by the classes in these assemblies. To properly manage the assemblies I’ve created a module, which you can also use for your work.

function Initialize-Assembly
{
       param
       (
              [Parameter(Mandatory=$true, Position=0, ParameterSetName="Single")]
              [ValidateNotNullOrEmpty()]
              [string] $Name,

              [Parameter(Mandatory=$true, Position=0, ParameterSetName="Array")]
              [ValidateNotNullOrEmpty()]
              [string[]] $Names

       )
      
       switch($PSCmdlet.ParameterSetName)
       {
              "Single"
              {
                     [void][System.Reflection.Assembly]::LoadWithPartialName($Name)
                     break
              }
              "Array"
              {
                     $Names |% { [void][System.Reflection.Assembly]::LoadWithPartialName($_) }
                     break
              }
       }
}

function Test-AssemblyLoaded
{
       param
       (
              [Parameter(Mandatory=$true, Position=0, ParameterSetName="Single")]
              [ValidateNotNullOrEmpty()]
              [string] $Name,

              [Parameter(Mandatory=$true, Position=0, ParameterSetName="Array")]
              [ValidateNotNullOrEmpty()]
              [string[]] $Names

       )

       switch($PSCmdlet.ParameterSetName)
       {
              "Single"
              {
                     (Get-AssemblyLoaded |? {$_ -match $Name}) -ne $null
                     break
              }
              "Array"
              {
                     $result = $true
                     $assemblies = Get-AssemblyLoaded
                     $Names |% {
                           $assembly = $_
                           if(($assemblies |? {$_ -match $assembly}) -eq $null)
                           {
                                  $result = $false
                           }
                     }

                     $result
                     break
              }
       }
}

function Get-AssemblyLoaded
{
       [System.AppDomain]::CurrentDomain.GetAssemblies()
}

Export-ModuleMember *Assembly*



PS E:\> Get-AssemblyLoaded | select fullname

FullName                                                                                                               
--------                                                                                                                
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089                                            
powershell_ise, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35                                      
System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089                                
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089                                              
System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a                                      
System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35                        
System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089                                         
Microsoft.PowerShell.ISECommon, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35                      
Microsoft.PowerShell.GPowerShell, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35                    








PS E:\> Initialize-Assembly -Name "Microsoft.TeamFoundation.Client"

PS E:\> Test-AssemblyLoaded  -Name "Microsoft.TeamFoundation.Client"
True

PS E:\> 



No comments: