Search code examples
.netpowershellpowershell-remoting

Could not load file or assembly in powershell remote session


I get this error when I try to run an assembly from an powershell remote session

Could not load file or assembly 'System.Xml.Linq, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find
the file specified.

I started the remote session by

Enter-PsSession -ComputerName myComputer -ConfigurationName Microsoft.Powershell
-Credential domain\user

But when I run it on the same machine as the same user it has no problems.

Update:

I want to do something like

if (Test-Path $AssemblyPath) {
    $fileStream = ([System.IO.FileInfo] (Get-Item $AssemblyPath)).OpenRead()
    $assemblyBytes = New-Object byte[] $fileStream.Length
    $fileStream.Read($assemblyBytes, 0, $fileStream.Length) | Out-Null
    $fileStream.Close()
    $assemblyLoaded = [System.Reflection.Assembly]::Load($assemblyBytes) | Out-Null

    $myObject = New-Object namespaceInMyAssembly.ClassInMyAssembly
    $myObject.doMyAction()
}

Solution

  • I found the problem: My Powershell was configured for .Net 4 in powershell.exe.config but not in wsmprovhost.exe.config. So I couldn't load .Net 4 assemblies from my remote session. See also this question.