VMware Cloud Community
SunDev
Contributor
Contributor

Script Works in Powershell ISE..but not the Powershell Command Prompt

Hello,

I'm running the get-dvswitchnetwork and set-dvswitchnetwork functions from http://poshcode.org/1895.

The script works quite well when I run it from Powershell ISE - I can change the dvSwitch that a VM guest uses without a problem or just get the information. Now, when I run the same script from the powershell command prompt, it fails with an error that states "the network doesn't exist on the host." So, I took a step back and changed the script so it just runs the "get-dvSwitchNetwork" portion and I noticed that when I run it from the command line, the "NetworkName" field is empty, but when I run it thru the ISE, the "NetworkName" is populated.

Any idea why the script can get the dvSwitch info (in this case, I connect to my VC server) when the script runs in the ISE, but then fails to get the dvSwitch info from my VC server when I run the script from the command prompt?

Thank you,

Z

0 Kudos
11 Replies
LucD
Leadership
Leadership

The first point that comes to me would be to check your profiles.

As you probably know the ISE uses a different profile from the command line.

See for example How to Use Profiles in Windows PowerShell ISE.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
SunDev
Contributor
Contributor

It looks like I don't have profiles defined. $profile lists the default file names, but they don't exist in the file system.

0 Kudos
LucD
Leadership
Leadership

If you have no profiles then where and how do you load for example the PowerCLI snapin ?

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
SunDev
Contributor
Contributor

I have two lines in the script that load the PowerCLI snap-in:

Add-PSSnapin VMware.VimAutomation.Core

Initialize-VIToolkitEnvironment.ps1

0 Kudos
LucD
Leadership
Leadership

Ok, just tried it myself and it worked in both environments.

If I understand it correctly you packaged the function and the call of the function in a .ps1 file and then run the .ps1 file from the PS prompt ?

If yes, could you perhaps attach the script you're using ?

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
SunDev
Contributor
Contributor

Here it is - you are correct, everything is in one .ps1 file.

I pull two values from a couple of text files - one is for the dvSwitch that I want to change to and the other for the server name. Other than that, it's just the script from that website.

For fun, I installed PowerCLI on a Windows 2008 R2 machine and ran the script from the command line - same thing. NetworkName is blank...

Thanks for taking a look at this...

0 Kudos
LucD
Leadership
Leadership

Sorry for the delay, was watching a "great" game of football.

I think the explanation is rather simple.

The moment PS gets to your line "Get-NetworkAdapter...", the function definitions for Get-dvSwitchNetworkadapter and Set-dvSwitchNetworkadapter are not known to PS yet.

And more importantly, the alias definitions for Get-NetworkAdapter and Set-NetworkAdapter aren't executed yet.

As a consequence PS will fetch the original PowerCLI version of these cmdlets.

The solution is to place the lines you added at the end of the script.

That way PS knows about the functions and the aliases when it reaches your lines of code.

The reason that this works in the ISE is because there the functions and aliases are remembered between runs of the script.

Remember PS executes your scripts sequentially. So place your definitions before you call or use them.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
SunDev
Contributor
Contributor

LucD -

Thanks for helping me thru this. I'm just getting started with powershell... I'll make the changes and go from there!

I hope the vuvuzelas didn't drive you crazy during that match...

Z

0 Kudos
LucD
Leadership
Leadership

No, the UK pros drove me crazy Smiley Wink

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
SunDev
Contributor
Contributor

Hi LucD,

One more question regarding the script, which now runs with your assistance. Smiley Happy

I get a module error even though the script runs properly. It states:

Export-ModuleMember : The Export-ModuleMember cmdlet can only be called from inside a module.

This is the second to last line in the script:

Export-ModuleMember -Function Get-dvSwitchNetworkAdapter, Set-dvSwitchNetworkAdapter -Alias Get-NetworkAdapter,Set-NetworkAdapter

Is there a way to prevent this error or is it ok to ignore it since the script seems to work perfectly?

Many thanks,

Z

0 Kudos
LucD
Leadership
Leadership

You can safely comment out the Export-ModuleMember line.

Since the code doesn't go into a module, it serves no purpose (like the error message is telling us).

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos