VMware Cloud Community
maikeru1028
Contributor
Contributor
Jump to solution

Why does everyone say that "Add-PSSnapin VMware.VimAutomation.Core" is necessary?

Every thread I read says you need to put "Add-PSSnapin VMware.VimAutomation.Core" at the top of any script or commands if you want to run them from PowerShell (and not PowerCLI).

However, when I add that to my script, PowerShell (2.0) will continue on and run the script, but it will throw me a nice big red warning (I guess it's not an error because the script ultimately runs), saying that "VMware.VimAutomation.Core" is already included and thus can't be added again.

I never did anything to configure or change PowerShell, ever.  In fact, this is only like day 2 of using PowerShell.  So it's in its straight-out-of-the-box condition.  Yet every forum I go to just goes on and on about how this is necessary and stuff.  Just wondering, what gives?  What's the story/explanation here?  Do I just have some kind of freak mutant version of PowerShell?    

Reply
0 Kudos
1 Solution

Accepted Solutions
sflanders
Commander
Commander
Jump to solution

Adding it just ensures it is loaded in case it is not. Certain applications (e.g. PowerGUI) allow you to load the snap-ins by default removing the need for adding it to every script. If you do not add it to your scripts and it is not loaded and you attempt to run PowerCLI commands the operations will fail as the commands will not be know. People typically consider it a best practive to include the snap-in at the beginning of scripts so it is easier to transport.

Hope this helps! === If you find this information useful, please award points for "correct" or "helpful". ===

View solution in original post

Reply
0 Kudos
5 Replies
sflanders
Commander
Commander
Jump to solution

Adding it just ensures it is loaded in case it is not. Certain applications (e.g. PowerGUI) allow you to load the snap-ins by default removing the need for adding it to every script. If you do not add it to your scripts and it is not loaded and you attempt to run PowerCLI commands the operations will fail as the commands will not be know. People typically consider it a best practive to include the snap-in at the beginning of scripts so it is easier to transport.

Hope this helps! === If you find this information useful, please award points for "correct" or "helpful". ===
Reply
0 Kudos
Dave_Mishchenko
Immortal
Immortal
Jump to solution

Perhaps its been added to your PS profile - C:\Documents and Settings\\Documents\ WindowsPowerShell\Microsoft.PowerShell_profile.ps1.

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

To prevent the Add-PSSnapin cmdlet to throw a warning message you can add "2> $null" to the line. This writes the message to the null device. Which is the same as throwing it away. So:

Add-PSSnapin VMware.VimAutomation.Core 2> $null

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
LucD
Leadership
Leadership
Jump to solution

It's probably already added in one of your profiles.

You can add it conditionally like this

if(!(Get-PSSnapin | where {$_.Name -eq "VMware.VimAutomation.Core"})){
    Add-PSSnapin "VMware.VimAutomation.Core"}


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

avlieshout
VMware Employee
VMware Employee
Jump to solution

Nice one Luc.

Never thought of adding it conditionally.

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
Reply
0 Kudos