VMware Cloud Community
carvaled
Enthusiast
Enthusiast

PowerCLI 6.5.x - Import-Module when executed via an C# and ASP.net fails with command was found in the module but the module could not be loaded

So this one is interesting...

I have a self-service portal which I built for users to pull metrics from vRops, It runs on W2K12, C# / ASP.net and executes a PowerShell script which goes off and talks to the vRops API and vCenter for Event history...

The portal has been working just fine using PowerCLI 6.3 R1, I recently upgraded to PowerCLI 6.5.1 and had customers complain that the Events where no longer showing up in the report... When I execute the script via PowerShell manually or with a Scheduled task it works just fine and events are imported... but when the ASP page runs the script it fails!

So after lots of playing around I discovered that Import-Module appears not to work correctly when executed by my webpage... where as Add-PSSnapin still works just fine. The portal runs as a Domain User which is also an administrator of the server...

I suspect it's permission related as I found this post that sounded like a similar issue...

Has anyone else experienced this before?

Connect-VIServer : The 'Connect-VIServer' command was found in the module 'VMware.VimAutomation.Core', but the module could not be loaded. For more information, run 'Import-Module VMware.VimAutomation.Core'.

At E:\vRops\vRopsScript.ps1:506 char:1

+ Connect-VIServer -server $VC -User $vRopsUser -Password $vRopsPasswor ...

+ ~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (Connect-VIServer:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

Could not load file or assembly 'file:///E:\vROPS\Modules\VMware.VimAutomation.Common\6.5.1.5335010\Common.Logging.dll' or one of its dependencies. The system cannot find the file specified.

Import-Module : Could not load file or assembly

'file:///E:\vROPS\Modules\VMware.VimAutomation.Common\6.5.1.5335010\Common.Logging.dll' or one of its dependencies. The system cannot find the file specified.

At E:\vRops\vRopsScript.ps1:501 char:1

+ Import-Module VMware.VimAutomation.Core -Force

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [Import-Module], FileNotFoundException

    + FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand

Could not load file or assembly 'file:///E:\vROPS\Modules\VMware.VimAutomation.Common\6.5.1.5335010\Common.Logging.dll' or one of its dependencies. The system cannot find the file specified.

Reply
0 Kudos
14 Replies
LucD
Leadership
Leadership

You mention Add-PSSnapin, are you sure you installed PowerCLI 6.5.1, and not 6.5R1?

They are not the same, the former only has modules, so Add-PSSnapin should return an error.

You could also check by doing a Get-PSSnapin -Registered, this should not return anything for 6.5.1.

I assume you installed the PowerCLI modules under E:\vROPS\Modules?

In that case, did you update the $env:PSModulePath variable to include that folder?

Not 100% sure if that would matter in your setup though.

For further debugging it would be important to know under which account your PowerShell script is executed.

Perhaps active a transcript log at the beginning of your script, and perhaps add the Verbose switch on the cmdlets.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
carvaled
Enthusiast
Enthusiast

Hello LucD,

wow thanks for getting back to me so quickly.

Yes I am sure I installed it correctly as my script works with PowerCLI 6.5.1 and earlier versions (The code checks if it should use modules or load snapins depending on the version of PowerCLI)

When I execute it in PowerShell, PowerShell ISE or a windows Scheduled task with PowerCLI 6.5.1 everything works just fine (This is all on the same server where the portal runs, all methods are executing the same script). Its only when its executed via C# and ASP.net on a box with PowerCLI 6.5.1 that it does not work.

Yes I have the modules under E:\vROPS\Modules, I thought it was a permission issue but it also throws the same error when the module is installed to the default path. Yes I also checked $env:PSModulePath and everything is correct.

Regarding permissions: The script is exeucted by IIS, the application pool is running as a domain user in the local Administrators

Its the same user account which runs the script for scheduled tasks and works just fine.

The issue really appears to be in C# and ASP.net calling the PowerCLI cmdlets when using C# PowerShell.Create();

Thanks

vMan

Reply
0 Kudos
LucD
Leadership
Leadership

Just curious, did you already try the Import-Module with the full path of the module?

Like (the path of course needs to be adapted to where you installed the module).

Import-Module -Name "C:\Program Files\WindowsPowerShell\Modules\VMware.VimAutomation.Core"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
carvaled
Enthusiast
Enthusiast

Hello LucD,

Yes I did also try to hardcode it:

Import-Module -Name "E:\vROPS\Modules\VMware.VimAutomation.Core\6.5.1.5374329\VMware.VimAutomation.Core"

I spent a while on this issue and it's really strange / specific to the asp.net / C# execution method when using import-module instead of Add-PSSnapin.

I should also mention that I have this issue on my server (IIS) and my workstation (VS 2012)

Cheers

vMan

Reply
0 Kudos
carvaled
Enthusiast
Enthusiast

Another additional bit of info:

The same script which performs all the data collection from vRops and vCenter then exports the data into several worksheets using module Export-Excel without any issues.

Cheers

vMAN

Reply
0 Kudos
LucD
Leadership
Leadership

And you did suppress all Warnings before loading the module?

There is this CEIP message when you load the Core module.

But then again, it would require running the Set-PowerCLIConfiguration cmdlet before importing the module (chicken & egg).

Unless you can do this, with the correct account, interactively beforehand.

Just guessing btw :smileygrin:


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
carvaled
Enthusiast
Enthusiast

Hello LucD,

Yes I did try, I ran PowerShell as the service account using "Run as different user" and set:

Can you see anything wrong based on the lines below?

Set-PowerCLIConfiguration -Scope AllUsers -ParticipateInCEIP $false -InvalidCertificateAction Ignore -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue -DisplayDeprecationWarnings $false

Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false -InvalidCertificateAction Ignore -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue -DisplayDeprecationWarnings $false

CHeers

vMan

Reply
0 Kudos
grasshopper
Virtuoso
Virtuoso

Not sure if it's related since I don't do C, but at some point in PowerShell (~4.0) we had to start using the full assembly name [System.Management.Automation.PowerShell]::Create() instead of just PowerShell::Create().

Reply
0 Kudos
carvaled
Enthusiast
Enthusiast

Thanks for the info, I will check it out but the actual script launches from c#/ASP.net just fine and 90% of the functionality works... its just that it fails to load the vmware core module which I need for the vCenter events as vRops doesnt expose it with the suite-api

Cheers

vMan

Reply
0 Kudos
grasshopper
Virtuoso
Virtuoso

It looks like you are targeting VMware.VimAutomation.Core directly.  Do your results change (for the better) when targeting VMware.PowerCLI?  The only downside is it loads all PowerCLI modules, but it's dependable.

As for your PowerCLI configuration that looks fine.  Please note that the

'-ErrorAction', 'WarningAction' and 'InformationAction' are not needed.  This only affects your interaction with the Set-PowerCLIConfiguration cmdlet while you're doing the settings (i.e. do you want to see if an error happened while setting the setting).  These are part of the 'free' cmdlets you get when you add [CmdletBinding()] to the top of a script (as VMware does).

EDIT:

Remove redundant question

Add link to Don Jones Toolmaking (3 part series on youtube)

Reply
0 Kudos
grasshopper
Virtuoso
Virtuoso

Also, there is a -Global switch available on the Import-Module cmdlet that may help.

Reply
0 Kudos
carvaled
Enthusiast
Enthusiast

Hey, sorry been MIA with heaps of work... Thanks I will check out the details you posted.

For the moment I just rolled back to an earlier version of PowerCLI until I find the solution... When I get time.

Reply
0 Kudos
carvaled
Enthusiast
Enthusiast

nah didnt help.. same  problem... here is the transcript for the import section...

**********************

Windows PowerShell transcript start

Start time: 20170928225431

Username: vMANNET\ServiceAccount123

RunAs User: VMANNET\ServiceAccount123

Machine: vMANPSHOST (Microsoft Windows NT 6.3.9600.0)

Host Application: c:\windows\system32\inetsrv\w3wp.exe -ap vmanmetrics.ch -v v4.0 -l webengine4.dll -a \\.\pipe\iisipm822ef59c-f946-4fd0-af4f-23a0c7d46c3b -h G:\inetpub\temp\appPools\vmanmetrics.ch\vmanmetrics.ch.config -w  -m 0 -t 20 -ta 0

Process ID: 5668

PSVersion: 5.1.14409.1005

PSEdition: Desktop

PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1005

BuildVersion: 10.0.14409.1005

CLRVersion: 4.0.30319.42000

WSManStackVersion: 3.0

PSRemotingProtocolVersion: 2.3

SerializationVersion: 1.1.0.1

**********************

PS>TerminatingError(Import-Module): "Could not load file or assembly 'file:///C:\Program Files\WindowsPowerShell\Modules\VMware.VimAutomation.Common\6.5.1.5335010\Common.Logging.dll' or one of its dependencies. The system cannot find the file specified."

>> TerminatingError(Import-Module): "Could not load file or assembly 'file:///C:\Program Files\WindowsPowerShell\Modules\VMware.VimAutomation.Common\6.5.1.5335010\Common.Logging.dll' or one of its dependencies. The system cannot find the file specified."

Import-Module : Could not load file or assembly 'file:///C:\Program Files\WindowsPowerShell\Modules\VMware.VimAutomation.Common\6.5.1.5335010\Common.Logging.dll' or one of its

dependencies. The system cannot find the file specified.

At G:\Scripts\vRops\vRopsMetricGet.ps1:594 char:1

+ Get-Module –ListAvailable VMware.PowerCLI | Import-Module -Global

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [Import-Module], FileNotFoundException

    + FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand

PS>TerminatingError(Import-Module): "Could not load file or assembly 'file:///C:\Program Files\WindowsPowerShell\Modules\VMware.VimAutomation.Common\6.5.1.5335010\Common.Logging.dll' or one of its dependencies. The system cannot find the file specified."

>> TerminatingError(Import-Module): "Could not load file or assembly 'file:///C:\Program Files\WindowsPowerShell\Modules\VMware.VimAutomation.Common\6.5.1.5335010\Common.Logging.dll' or one of its dependencies. The system cannot find the file specified."

>> TerminatingError(Import-Module): "Could not load file or assembly 'file:///C:\Program Files\WindowsPowerShell\Modules\VMware.VimAutomation.Common\6.5.1.5335010\Common.Logging.dll' or one of its dependencies. The system cannot find the file specified."

**********************

Windows PowerShell transcript end

End time: 20170928225432

**********************

Reply
0 Kudos
carvaled
Enthusiast
Enthusiast

thanks, i tried but that also didn't help...

Reply
0 Kudos