Matthias Güntert
Matthias Güntert

Reputation: 4638

How to use the #Requires keyword for DLLs?

How can I use the #Requires keyword to check for a specific dll? This is the code I have got, which requires the file Microsoft.Search.Interop.dll to be in the same folder as the script.

# Load DLL containing classes & interfaces
Add-Type -path "Microsoft.Search.Interop.dll"

# Provides methods for controlling the Search service. This 
# interface manages settings and objects that affect the search engine 
# across catalogs. 
#
# https://msdn.microsoft.com/en-us/library/bb231485(v=vs.85).aspx
$sm = New-Object Microsoft.Search.Interop.CSearchManagerClass

# Retrieves a catalog by name and creates a new ISearchCatalogManager 
# object for that catalog.
$catalog = $sm.GetCatalog("SystemIndex")

# Resets the underlying catalog by rebuilding the databases 
# and performing a full indexing. 
#
# https://msdn.microsoft.com/en-us/library/bb266414(v=vs.85).aspx
$catalog.Reset()

Upvotes: 0

Views: 74

Answers (1)

TechSpud
TechSpud

Reputation: 3518

A simple function to test for existence of an assembly should suffice, if all you want is to check for existence of a specific assembly in the filesystem (i.e. in the same folder as the script) or in the GAC.

The function will accept an assembly name with or without a path. It tests for an assembly in the script folder (if no path element). Then if searchGAC param specified, tests for existence in the GAC. If it finds the assembly anywhere, it will load it, otherwise, will throw an exception.

# requires function
function Get-Assembly ([string]$dllName, [switch]$searchGAC) {
  if ($dllName.Split('\\').Count -eq 1) {
    $dllName = Join-Path $PSScriptRoot $dllName
  }
  if (Test-Path $dllName -PathType Leaf) {
    Add-Type -path $dllName
  } else {
    $found = $false
  }
  if (!$found -and $searchGAC) {
    $dllName = ($dllName -split '\\')[-1]
    $assembly = [appdomain]::CurrentDomain.GetAssemblies() | Select -ExpandProperty Modules | where {$_.Name -match $dllName}
    if ($assembly) {
      [void][System.Reflection.Assembly]::Load($assembly.Assembly.FullName)
      $found = $true
    }
  }
  if (!$found) {
    throw [System.IO.FileNotFoundException] "$dllName not found."
  }
}
Set-Alias -Name Requires -Value Get-Assembly -Force


# your code from here, with the requires added in
# Load DLL containing classes & interfaces
Requires -dllName 'Microsoft.Search.Interop.dll' -searchGAC

# Provides methods for controlling the Search service. This 
# interface manages settings and objects that affect the search engine 
# across catalogs. 
#
# https://msdn.microsoft.com/en-us/library/bb231485(v=vs.85).aspx
$sm = New-Object Microsoft.Search.Interop.CSearchManagerClass

# Retrieves a catalog by name and creates a new ISearchCatalogManager 
# object for that catalog.
$catalog = $sm.GetCatalog("SystemIndex")

# Resets the underlying catalog by rebuilding the databases 
# and performing a full indexing. 
#
# https://msdn.microsoft.com/en-us/library/bb266414(v=vs.85).aspx
$catalog.Reset()

Upvotes: 1

Related Questions