Search code examples
powerclipowershell-coresnmptrapd

TypeInitializationException Importing Modules in Powershell Under SNMPTRAPD


So, here's my setup: I've got a Dell UPS which I want to catch a particular SNMP Trap from. To do so I set up SNMPTRAPD to run as a service on Debian 9. I have two "Traphandle" statements in the configuration file which upon detecting particular trap OIDs launch a Powershell script (code to follow). One of the first things these scripts do is import VMware PowerCLI modules, this fails with the TypeInitializationException.

I've tried looking up similar issues for TypeInitializationExceptions on Google and on the Powershell Core Github, but these have been for different modules in different situations, or the problem solutions have been kinda over my head. I've tried updating .Net Core to the latest preview release (SDK 3.0.100-preview7-012821), to no avail. This issue does NOT occur when running the script outside of the SNMPTRAPD service, the module imports perfectly fine.

Here is what I've pared my code down to:

#!/usr/bin/pwsh

$env:PSModulePath += ":/root/.local/share/powershell/Modules:/usr/local/share/powershell/Modules:/opt/microsoft/powershell/6/Modules"


Import-Module VMWare.Vim
exit 0

I have to put the environment variable assignment in there to solve a different error, where Powershell can't find the module directory. Here is the SNMPTRAPD.conf file, if that needs to be shown as well:

snmpTrapAddr 192.168.1.69:162
ignoreAuthFailure yes
doNotFork no
AuthCommunity log,execute,net public
traphandle .1.3.6.1.4.1.674.10902.2.140.0.47 /root/Desktop/pwshtest
traphandle .1.3.6.1.4.1.674.10902.2.140.0.48 /root/Desktop/killerpwsh

pwshtest is what is running with this.

I would expect the powershell script as is to import the module and exit with no errors, but instead, this is the error I get.

Import-Module : The type initializer for 'VMware.VimAutomation.ViCore.Util10.SettingsManager' threw an exception.
Jul 29 16:11:54 debian-upsmon snmptrapd[2184]: At line:6 char:1
Jul 29 16:11:54 debian-upsmon snmptrapd[2184]: + Import-Module VMware.Vim
Jul 29 16:11:54 debian-upsmon snmptrapd[2184]: + ~~~~~~~~~~~~~~~~~~~~~~~~
Jul 29 16:11:54 debian-upsmon snmptrapd[2184]: + CategoryInfo          : OperationStopped: (:) [Import-Module], TypeInitializationException
Jul 29 16:11:54 debian-upsmon snmptrapd[2184]: + FullyQualifiedErrorId : System.TypeInitializationException,Microsoft.PowerShell.Commands.ImportModuleCommand

I've tried to get the inner exception, but, it seems to be Null. Maybe I'm doing something wrong there. Thanks in advance


Solution

  • The problem here is that it needs a user folder to write some settings xml somewhere. It searches for the current users home directory and I bet this user has none defined.

    You can do the following:

    In your powershell script add this before importing the module:

    $env:HOME = "/home/myuser"

    and create a home directory for this user:

    mkdir myuser

    chown myuser:myuser myuser

    chmod 700 myuser

    then I suggest to enter a interactive pwsh session and set:

    pwsh

    Set-PowerCLIConfiguration -Scope AllUsers -ParticipateInCeip $false

    and just to be complete: I used this modules path for all users: /usr/local/share/powershell/Modules you have to create the folder if it does not exist yet.

    I tested this on Centos7 but I guess it should work on other distris as well.