DLPipkin
Contributor
Contributor

powershell vm cli not running as a CGI

I am running pwsh (PowerShell v6.1.0-preview.2) on Ubuntu 16.04.

   VMware PowerCLI 10.1.0 build 8346946

   VMware Cis Core PowerCLI Component PowerCLI Component 10.1 build 8377811

   VMware VimAutomation VICore Commands PowerCLI Component PowerCLI Component 10.1 build 8344055

When I run my powerCLI script from a shell it runs perfectly, but when I run it as a CGI the vmware module aborts.

apache2/error.log says:

AH01215: Connect-VIServer : The type initializer for 'VMware.VimAutomation.ViCore.Util10.SettingsManager' threw an exception.: /var/www/html/vm.ps1

AH01215: At /var/www/html/vm.ps1:62 char:7: /var/www/html/vm.ps1

AH01215: + if(Connect-VIServer -Server $vmSphere -user $vmSphereUser -passwor ...: /var/www/html/vm.ps1

AH01215: +       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~: /var/www/html/vm.ps1

AH01215: + CategoryInfo          : NotSpecified: (:) [Connect-VIServer], TypeInitializationException: /var/www/html/vm.ps1

AH01215: + FullyQualifiedErrorId : System.TypeInitializationException,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer: /var/www/html/vm.ps1

AH01215:  : /var/www/html/vm.ps1

syslog contains the errors in the attached errors.txt file.

0 Kudos
9 Replies
LucD
Leadership
Leadership

PowerShell v6.1.0-preview.2 is not supported in PowerCLI 10.1.0 (see Compatibility Matrix).

Do you see the same with PowerShell Core 6.0.2?


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

0 Kudos
DLPipkin
Contributor
Contributor

Same results with powershell 6.0..2

0 Kudos
LucD
Leadership
Leadership

Does your script import the PowerCLI modules explicitly?

Or do you rely on auto-loading?

Does the PSModulePath for PS6Core contain the folders where the PowerCLI modules are installed?


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

0 Kudos
DLPipkin
Contributor
Contributor

it is in the /var/www/html directory --

I have added and removed the install-module and import-module -- this version does not generate any errors.

Here is the start of the script down to where it dies.

#!/usr/bin/pwsh

# set-psdebug -Trace 1

Param([string]$CourseID,

      [string]$StudentID,

      [string]$LabName,

      [string]$OPERATION

)

if ($Env:QUERY_STRING){

$QS = $Env:QUERY_STRING.split("&")

$CourseID = ($QS  -match "Course=").split("=")[1]

$StudentID = ($QS  -match "Student=").split("=")[1]

$LabName = ($QS  -match "Lab=").split("=")[1]

$OPERATION = ($QS  -match "Operation=").split("=")[1]

}

write-output "Content-type: text/html"

write-output ""

write-output "<HTML>"

write-output "<HEAD>"

write-output "<TITLE>$OPERATION $StudentID $LabName</TITLE>"

write-output "</HEAD>"

Write-Output "<link rel='stylesheet' type='text/css' href='BRITE.css' />"

write-output "<!--"

write-output "StudentID = $StudentID"

write-output "CourseID = $CourseID"

write-output "LabName = $LabName"

write-output "Operation = $Operation"

Install-Module -Name VMware.PowerCLI -Scope CurrentUser

Import-Module "VMware.PowerCLI"

# Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true  -confirm:$false

Set-PowerCLIConfiguration -Scope All -InvalidCertificateAction Ignore -confirm:$false

write-output "-->"

# Constants

$vmSphere = "172.29.220.100"

$vmSphereUser = "Secret@localos"

$vmSpherePass = "S3cret!"

$Domain = 'TULSATECH\'

$TMP = "TEMPLATE"

write-output '<BODY>'

#Computed Constants

$DSID = ($Domain + $StudentID)

$SLN = ($StudentID + '-' + $LabName)

Connect-VIServer -Server $vmSphere -user $vmSphereUser -password $vmSpherePass -ErrorAction continue )

0 Kudos
LucD
Leadership
Leadership

Could you check if the PowerCLI modules actually were imported?

By doing a

Get-Module VMware* | Select Name,Version


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

0 Kudos
DLPipkin
Contributor
Contributor

sorry -- the install-module is actually commented out on my current version.

got this output

Name Version 
----
------- 
VMware.VimAutomation.Sdk 10.1.0.8342078
0 Kudos
LucD
Leadership
Leadership

It was the Import-Module I'm interested in.

From your output it looks as if not all modules are loaded.
And that the module auto load feature seems to be not calling in the correct modules.

That would explain the missing type error you are getting.

I would try to explicitely import all the (required) modules.


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

0 Kudos
DLPipkin
Contributor
Contributor

I have added the import-modules and it now matches the interactive version

Name                          Version

----                          -------

VMware.Vim                    6.7.0.8343295

VMware.VimAutomation.Cis.Core 10.1.0.8377811

VMware.VimAutomation.Common   10.1.0.8342134

VMware.VimAutomation.Core     10.1.0.8344055

VMware.VimAutomation.Sdk      10.1.0.8342078

However, I believe i have been chasing a ghost.

I think my real problem is with the certificates and that the

Set-PowerCLIConfiguration -Scope User  -InvalidCertificateAction ignore -confirm:$false

action is failing and consequently the Connect-Viserver fails.

I am running esx 5.5 (we are a school with no budget for upgrades) and the invalid certs have not previously been a problem.

Still not sure why the Set-PowerCLIConfiguration works in the interactive shell but not in the CGI script?

Set-PowerCLIConfiguration : The type initializer for 'VMware.VimAutomation.ViCore.Util10.SettingsManager' threw an exception.

At /var/www/html/vm.ps1:49 char:2

+  Set-PowerCLIConfiguration -Scope User  -InvalidCertificateAction ign ...

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

+ CategoryInfo          : NotSpecified: (:) [Set-PowerCLIConfiguration], TypeInitializationException

+ FullyQualifiedErrorId : System.TypeInitializationException,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetVIToolkitConfiguration

0 Kudos
Sudarshan_Bhart
Enthusiast
Enthusiast

Did anyone find out a solution to this problem?

0 Kudos