VMware Cloud Community
MrGallus23
Contributor
Contributor

Jenkins and Powercli scripts

Hi All,

Complete Newbie with Jenkins here but...

I'm trying to use Jenkins to run VM automation scripts but I can seem to get the Powercli module loaded so that i can connect to vcenter in the scripts.

When I load PowerShell interactively PowerCLI loads fine and I can exceute commands fine.

I've seen a few posts about environment variables and I've added the Path and PSModulePath variables to the global properties section in  the configure systems, but that doesnt change anything.

Also tried runnnig the Jenkins Windows service as my account but the results are always the same. As shown below...

connect-viserver : The term 'connect-viserver' is not recognized as the name

of a cmdlet, function, script file, or operable program. Check the spelling of

the name, or if a path was included, verify that the path is correct and try

again.

and

import-module : The specified module 'VMware.PowerCLI' was not loaded because

no valid module file was found in any module directory.

At C:\Windows\TEMP\jenkins3243661025460378840.ps1:3 char:1

+ import-module VMware.PowerCLI

Any help would be appreciated here

Mike.

Reply
0 Kudos
13 Replies
jpsider
Expert
Expert

Jenkins is a pita because of the way the accounts and services are run. Where is the module installed? Is it in any of the folders named in $env:psmodulepath for the user account Jenkins runs under?

Reply
0 Kudos
MrGallus23
Contributor
Contributor

The module is installed in the normal location..C:\Windows\System32\WindowsPowerShell\v1.0\Modules

When I run the Jenkins service under my account I still get the same issues but inetractively from the Powershell prompt PowerCli is fine.

Looking at the built tho it still says its running as System

Started by user admin

Running as SYSTEM

Building in workspace C:\Program Files (x86)\Jenkins\workspace\ps-test1

[ps-test1] $ powershell.exe -NonInteractive -ExecutionPolicy Bypass -File C:\Users\DA_HOW~1\AppData\Local\Temp\jenkins4121092801461908652.ps1

import-module : The specified module 'VMware.PowerCLI' was not loaded because no valid module file was found in any

module directory.

At C:\Users\da_howardm\AppData\Local\Temp\jenkins4121092801461908652.ps1:3 char:1

+ import-module VMware.PowerCLI

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

  + CategoryInfo : ResourceUnavailable: (VMware.PowerCLI:String) [Import-Module], FileNotFoundException

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

connect-viserver : The term 'connect-viserver' is not recognized as the name of a cmdlet, function, script file, or

operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try

again.

Reply
0 Kudos
jpsider
Expert
Expert

What operating system is this? What if you were to specify the import-module to the full path to the Module

Reply
0 Kudos
MrGallus23
Contributor
Contributor

its Win 2016..doesnt seem to make a difference if i put the full path in... I assumed that powershell autoload would deal with the modules...

Reply
0 Kudos
jpsider
Expert
Expert

Interesting, on my 2016 boxes, when I install a module it goes into: C:\Program Files\WindowsPowerShell\Modules During a Run, can you print the output of: get-module -listAvailable

Reply
0 Kudos
JustinB404
Contributor
Contributor

So using jenkins in a way its not intended. I have been down this rabbit hole.... How are you calling your scripts? Are you just putting your script into the powershell command block or are you calling it like so? "powershell -File .\VMware\Tools\CDrom\Get-CDrom.ps1"

Reply
0 Kudos
LucD
Leadership
Leadership

Have a look at Module not loaded when using Jenkins


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

Reply
0 Kudos
MrGallus23
Contributor
Contributor

Ok so that showed a different location.. it showed C:\Program Files (x86)\WindowsPowerShell\Modules

I moved the modules and now that works within Jenkins, but interactively it doesn't' work now...

I also notice I have Powershell 7 installed on here. it must be part of the template....

Reply
0 Kudos
MrGallus23
Contributor
Contributor

I'd just been pasting som test POwercli commands straight into Jenkins just to see if everything is working...

So the approach is to  get Jenkins to trigger a .ps1 script rather than adding commands here...?

Reply
0 Kudos
MrGallus23
Contributor
Contributor

Hi LucD,

Yes I'd found that but it didnt seem to make any difference however I wasn't entirely sure i'd added it correctly as I'm completely nw to Jenkins..

I added the below...

pastedImage_0.png

Reply
0 Kudos
JustinB404
Contributor
Contributor

That is what worked reliably for me. I also was using GIT so it would pull the latest version of that script from the repo.

Reply
0 Kudos
LucD
Leadership
Leadership

Why don't you automate the installation of the PowerCLI modules as well?

In your build, first check if the modules are there, if not install them.

To be honest, I haven't tested/tried this with PSv7 (which looks for modules in yet another location)


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

Reply
0 Kudos
MrGallus23
Contributor
Contributor

Well with the modules in C:\Program Files (x86)\WindowsPowerShell\Modules running a test ps.1 with Powecli commands works fine.

It runs using the older PowerShell 5.1.14393.3471 and not the v7 thats also installed. I think thats fine for me...

I dont sem to need the import first commands are available straight away...

Thanks very much for your help Guys

Mike

Reply
0 Kudos