Powershell script with standard cmdlets crashes with System.IO.FileLoadException on some machines

2

We have a script that issues web requests using Invoke-WebRequest and Invoke-RestMethod. It also uses ConvertFrom-Json, ConvertTo-Json, and [System.Net.ServicePointManager]::CertificatePolicy to handle secure connections.

Apart from that, there is one function and one class.

The script works fine on several machines, but crashes with the following message right at the start:

The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
At line:1 char:1
+ .\newNounFamily.ps1
+ ~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], FileLoadException
    + FullyQualifiedErrorId : System.IO.FileLoadException

We tried extracting more info using this command:

$Error[0].Exception.FileName

But the output was empty.

The stack trace is below. It is not saying much:

at System.Reflection.AssemblyName.nInit(RuntimeAssembly& assembly, Boolean forIntrospection, Boolean raiseResolveEvent)
   at System.Reflection.AssemblyName..ctor(String assemblyName)
   at System.Management.Automation.Language.TypeDefiner.DefineTypes(Parser parser, Ast rootAst, TypeDefinitionAst[] typeDefinitions)
   at System.Management.Automation.Language.Compiler.DefinePowerShellTypes(Ast rootForDefiningTypes, TypeDefinitionAst[] typeAsts)
   at System.Management.Automation.Language.Compiler.GenerateTypesAndUsings(ScriptBlockAst rootForDefiningTypesAndUsings, List`1 exprs)
   at System.Management.Automation.Language.Compiler.CompileSingleLambda(ReadOnlyCollection`1 statements, ReadOnlyCollection`1 traps, String funcName, IScriptExtent entryExtent, IScriptExtent exitExtent, ScriptBlockAst rootForDefiningTypesAndUsings)
   at System.Management.Automation.Language.Compiler.CompileNamedBlock(NamedBlockAst namedBlockAst, String funcName, ScriptBlockAst rootForDefiningTypes)
   at System.Management.Automation.Language.Compiler.VisitScriptBlock(ScriptBlockAst scriptBlockAst)
   at System.Management.Automation.Language.Compiler.Compile(CompiledScriptBlockData scriptBlock, Boolean optimize)
   at System.Management.Automation.CompiledScriptBlockData.ReallyCompile(Boolean optimize)
   at System.Management.Automation.CompiledScriptBlockData.CompileOptimized()
   at System.Management.Automation.CompiledScriptBlockData.Compile(Boolean optimized)
   at System.Management.Automation.PSScriptCmdlet..ctor(ScriptBlock scriptBlock, Boolean useNewScope, Boolean fromScriptFile, ExecutionContext context)
   at System.Management.Automation.CommandProcessor.Init(IScriptCommandInfo scriptCommandInfo)
   at System.Management.Automation.CommandDiscovery.GetScriptAsCmdletProcessor(IScriptCommandInfo scriptCommandInfo, ExecutionContext context, Boolean useNewScope, Boolean fromScriptFile, SessionStateInternal sessionState)
   at System.Management.Automation.CommandDiscovery.CreateCommandProcessorForScript(ExternalScriptInfo scriptInfo, ExecutionContext context, Boolean useNewScope, SessionStateInternal sessionState)
   at System.Management.Automation.CommandDiscovery.CreateScriptProcessorForSingleShell(ExternalScriptInfo scriptInfo, ExecutionContext context, Boolean useLocalScope, SessionStateInternal sessionState)
   at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(CommandInfo commandInfo, CommandOrigin commandOrigin, Nullable`1 useLocalScope, SessionStateInternal sessionState)
   at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)
   at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)
   at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, ExecutionContext context)
   at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
   at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

It appears that the Powershell crashes when defining its own types, but the Powershell version and the .NET version seems up to date and perfectly aligned with those on the working machines:

$PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      18362  145

and here:

[environment]::Version

Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      30319  42000

The same machine can run another smaller script, using Invoke-WebRequest and ConvertFrom-Json.

What are we doing wrong, and how do we know what Powershell has issues with?

EDIT: the code parts.

The top:

## =============================================================================
##
##       This script's purpose is to add a new noun family and new lexemes in English and another language, link them, and tag them
##
## =============================================================================
Param(
     [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="LaMP user: ")][String] $user,
     [Parameter(Mandatory = $true, HelpMessage="LaMP password: ")][String] $password,
     [Parameter(HelpMessage="Language code: ")][Int32] $lang,
     [Parameter(HelpMessage="Definition: ")][String] $definition,
     [Parameter(HelpMessage="Wikidata ID: ")][String] $wikidata,
     [Parameter(Mandatory = $true, HelpMessage="English lemmas, delimited by commas")][String[]] $english,
     [Parameter(HelpMessage="Native lemmas, delimited by commas ")][String[]] $native,
     [Parameter(Mandatory = $true, HelpMessage="Family ID ")][Int32] $family,
     [Parameter(Mandatory = $true, HelpMessage="Hypernym ID ")][Int32] $hypernym,
     [Parameter()][Int32] $proper,
     [Parameter()][Int32] $person
   )

The next line:

Function AddLexeme($familyId, $word) {

The first piece after the function:

If (-not ("TrustAllCertsPolicy" -as [type])) {
  Add-Type @"
      using System.Net;
      using System.Security.Cryptography.X509Certificates;
      public class TrustAllCertsPolicy : ICertificatePolicy {
          public bool CheckValidationResult(
              ServicePoint srvPoint, X509Certificate certificate,
              WebRequest request, int certificateProblem) {
              return true;
          }
      }
"@
}

powershell
asked on Stack Overflow Jan 13, 2020 by Vadim Berman • edited Jan 13, 2020 by Vadim Berman

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0