VMware Cloud Community
Zsoldier
Expert
Expert
Jump to solution

Import-Module via scheduled task results in different $env:modulepath

Just upgraded to PowerCLI 6.0 R3

Has anyone run into this?  I was updating my scheduled task scripts to start importing the module rather than adding the snapins, but for some reason when task scheduler runs the scripts, it basically errors out w/ unable to find valid module.

When the task scheduler job as the service account runs it, $env:modulepath looks like this:

C:\Windows\system32\WindowsPowerShell\v1.0\Modules\;D:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\;C:\Dell_PSCmdlets;C:\Program Files (x86)\AWS Tools\PowerShell\;C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement


When I run it interactively as the service account, I see that the module is listed properly in the modulepath environment variable.

C:\Users\svcLDAPVMware\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules\;D:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\;C:\Dell_PSCmdlets;C:\Program Files (x86)\AWS Tools\PowerShell\;C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement;D:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Modules

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos
1 Solution

Accepted Solutions
Zsoldier
Expert
Expert
Jump to solution

I figured it out.  The task scheduler service doesn't actually update the module path variable until it's restarted.  So anything launched by it will only have whatever is populated in the system modulepath @ time of start.  Installing PowerCLI 6.0R2 AFTER the fact, populates all new sessions, just not anything that was actively running at the time.

I had to reboot the VM for the task scheduler to pick up the new addition to the system modulepath.

This was specifically related to Windows 2008 R2, not sure if newer revisions are affected as well.  Anyway, hope this is useful to anyone that might run into the same situation.

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier

View solution in original post

0 Kudos
3 Replies
Zsoldier
Expert
Expert
Jump to solution

I figured it out.  The task scheduler service doesn't actually update the module path variable until it's restarted.  So anything launched by it will only have whatever is populated in the system modulepath @ time of start.  Installing PowerCLI 6.0R2 AFTER the fact, populates all new sessions, just not anything that was actively running at the time.

I had to reboot the VM for the task scheduler to pick up the new addition to the system modulepath.

This was specifically related to Windows 2008 R2, not sure if newer revisions are affected as well.  Anyway, hope this is useful to anyone that might run into the same situation.

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos
LucD
Leadership
Leadership
Jump to solution

This is still there in later Windows versions.

The content is coming from the wininit.exe, which spawns services.exe, which spawns svchost.exe.

It's a fundamental Windows OS concept.

Nice catch btw, I got tripped by this on several occasions Smiley Happy


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

0 Kudos
Zsoldier
Expert
Expert
Jump to solution

Thanks for confirming LucD.  Glad I wasn't the only one.  PowerCLI installer should probably recommend a reboot if that's the case.  alanrenouf

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos