VMware Cloud Community
tickermcse76
Contributor
Contributor

PowerShell scripts do not run correctly when using PowerCLI shortcut

When using the PowerCLI installed shortcut (Initialize-PowerCLIEnvironment.ps1) I noticed that some of my standard PowerShell scripts do not work correctly.  This is PowerCLI version 5.8 and PowerShell 3.0.

When running the PS script directly from PowerShell it returns the correct result.  When running the same PS script after launching the PowerCLI shortcut, the script runs, but does not return the correct results.  Now if I run standard PowerShell, load the PowerCLI module (Add-PSSnapin VMware.VimAutomation.Core), and then run the PS script, it returns the correct value.  In all cases $PSVersionTable returns the same values.

Can anyone explain what might cause this behavior and how to correct?

4 Replies
jpsider
Expert
Expert

It's very possible its a permission issue.  Sometimes the shortcut does not begin the powershell process with the permissions to perform the correct work.  You can set shortcuts to run as administrator.  I would check who you are running the cmd window as in each of the ways you are trying to run.

0 Kudos
LucD
Leadership
Leadership

Are you getting any errors ?

Or is the result of the script just incorrect ?

Can you try setting verbose with

$VerbosePreference="Continue"

Perhaps this will give some more indications of where the problem might be.


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

0 Kudos
tickermcse76
Contributor
Contributor

The script runs without error, just the results that are returned are different (and incorrect) when run from the PowerCLI shortcut vs the standard PowerShell shortcut.  I discovered this issue because I have a native PowerShell script that has been working for some time and has not been changed; and needed to feed the output to a new PowerCLI script to take actions in vCenter.  The PowerCLI script was not providing the expected behavior and running just the native Powershell script from the PowerCLI shortcut initiated session immediately showed why (the PowerCLI script was acting on an incorrect set of data).

I'll follow the suggestions posted to see if I can track it down; thanks.

0 Kudos
LucD
Leadership
Leadership

It could help if you tell us which kind of differences you see in the results.

And the code you use to get the results


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