Pre-exec script powershell does not load modules

  • KM03106602
  • 28-Feb-2018
  • 28-Feb-2018

Archived Content: This information is no longer maintained and is provided "as is" for your convenience.

Summary

When run powershell script as a pre-exec script on the backup server, the import-module part does not work, which results in vmware commands that are not understood by Data Protector.

Error

C:\Windows\system32>powershell -file "C:\Program Files\OmniBack\bin\stopvm.ps1" 
Import-Module : The required module 'VMware.VimAutomation.Common' is not loaded. Load the module or remove the module from 'RequiredModules' in the file 'C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Modules\VMware.VimAutomation.Cis.Core\VMware.VimAutomation.Cis.Core.psd1'.
At C:\Program Files\OmniBack\bin\stopvm.ps1:2 char:1

Cause

We need to create a backup of a virtual machine running on vmware ESXi farm.
For this to work, we need to stop the VM before the backup.
 
So to do this, we have installed vmware powerCli tools on the backup server.
This so we can run vmware commands against our ESXi farm.
 
As a pre-exec script we have created a startvm.bat file in the omniback\bin folder.
The script runs the following command: powershell -file "script.ps1".
 
The powershell itself runs fine, but the script needs to import the vmware modules, as we need to use them.
 
So we run the following commands in the powershell script:
get-module -name VMware* -ListAvailable
Import-Module
 
Then we have some vmware PS commands to connect to ESXI and stop the VM.
 
Via the command line on the server itself, the import works and the following vmware commands work.
But when we run the script as a pre-exec script on the backup server, the import-module part does not work, which results in vmware commands that are not understood.
 
So the question is, how to import additional modules in the powershell which will be understood by Data Protector.

Fix

  • The command get-module should be run by putting the full path to each module.
  • The order of commands for including modules matters.
  • A powershell script can't run directly as pre-exec, so we have to run a batch file and then launch the powershell script.