title: Reflection Assembly Errors in Powershell
body: <p>I have some Powershell code that takes the Apps running on an IIS server and finds the versions of .NET they are running. </p>
  
  <pre><code>$Apps = Get-WebApplication
  foreach($App in $Apps) {
      $binLocation = "$($App.physicalPath)\bin"
  
      # get all dlls in bin folder
      $dllFolder = Get-Item -Path $binLocation
      $dlls = $dllFolder.GetFiles("*.dll")
  
      # analyze dll .net version
      $set = New-Object System.Collections.Generic.HashSet[String]
      $dlls | ForEach-Object {
          $set.Add([Reflection.Assembly]::ReflectionOnlyLoadFrom("$binLocation\$($_.Name)").ImageRuntimeVersion) | Out-Null
      }
  
      # print all dll .NET version
      $App
          $set
  }
  </code></pre>
  
  <p>However when I run this on my server I get 2 types of error;</p>
  
  <pre><code>Exception calling "ReflectionOnlyLoadFrom" with "1" argument(s): "API restriction: The
  assembly 'file:///D:\inetpub\wwwroot\OABS_ECRM\bin\WebGrease.dll' has already loaded from a 
  different location. It cannot be loaded from a new location within the same appdomain."
  At line:13 char:74
  +         $set.Add([Reflection.Assembly]::ReflectionOnlyLoadFrom ("$binLocation\$($_.Name ...
  +                                                                                ~~~~~~~
  + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  + FullyQualifiedErrorId : FileLoadException
  </code></pre>
  
  <p>and,</p>
  
  <pre><code>Exception calling "ReflectionOnlyLoadFrom" with "1" argument(s): "Could not load file or 
  assembly 'file:///D:\inetpub\wwwroot\Tablet\bin\epengine.dll' or one of its dependencies. 
  The module was expected to contain an assembly manifest."
  At line:13 char:74
  +         $set.Add([Reflection.Assembly]::ReflectionOnlyLoadFrom("$binLocation\$($_.Name ...
  +                                                                                ~~~~~~~
  + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  + FullyQualifiedErrorId : BadImageFormatException
  </code></pre>
  
  <p>I seem to get the .NET versions running on each app from the script but I was wondering what caused these errors and if they can be cleared. I need to solve this in powershell.</p>