{"id":335,"date":"2014-06-18T16:07:35","date_gmt":"2014-06-18T23:07:35","guid":{"rendered":"http:\/\/www.ossintegrators.com\/blog\/?p=335"},"modified":"2014-06-18T17:24:44","modified_gmt":"2014-06-19T00:24:44","slug":"getting-started-with-vmware-powercli","status":"publish","type":"post","link":"http:\/\/www.ossintegrators.com\/blog\/getting-started-with-vmware-powercli\/","title":{"rendered":"Getting Started with VMWare PowerCLI"},"content":{"rendered":"<div style=\"padding-bottom:20px; padding-top:10px;\" class=\"hupso-share-buttons\"><!-- Hupso Share Buttons - http:\/\/www.hupso.com\/share\/ --><a class=\"hupso_toolbar\" href=\"http:\/\/www.hupso.com\/share\/\"><img decoding=\"async\" src=\"http:\/\/static.hupso.com\/share\/buttons\/share-small.png\" border=\"0\" style=\"padding-top:5px; float:left;\" alt=\"Share\"\/><\/a><script type=\"text\/javascript\">var hupso_services_t=new Array(\"Twitter\",\"Facebook\",\"Google Plus\",\"Linkedin\",\"Digg\",\"Reddit\");var hupso_toolbar_size_t=\"small\";var hupso_counters_lang = \"en_US\";var hupso_url_t=\"\";var hupso_title_t=\"Getting Started with VMWare PowerCLI\";<\/script><script type=\"text\/javascript\" src=\"http:\/\/static.hupso.com\/share\/js\/share_toolbar.js\"><\/script><!-- Hupso Share Buttons --><\/div><p>I&#8217;ve recently been spending some time &#8220;cleaning up&#8221; our VMWare infrastructure and making it more functional \u2013 both for demos and for internal lab use. To that end, I&#8217;ve been trying to automate\/simplify as many things as possible. One task that I&#8217;d been wanting to simplify is snapshot creation\/deletion of multiple VMs at once since this is something we do all the time. Imagine you have a set of VMs that are used for a single lab instance of a product. Before you make any major changes \u2013 for example patching or upgrading the product \u2013 you want to create a clean snapshot so that you can revert if something goes wrong. Unfortunately, this is something you can only do one VM-at-a-time in a vanilla-vCenter environment. However, it&#8217;s dead simple using Powershell and PowerCLI!<\/p>\n<p><!--more--><\/p>\n<h1>Enable Remotesigning in Powershell<\/h1>\n<p>In order to avoid popup warnings and errors later, you&#8217;ll want to enable remote signing in PowerShell. Open a PowerShell command window and type the following: &#8220;<span style=\"font-family: Courier New;\">Set-ExecutionPolicy RemoteSigned<\/span>&#8220;. I don&#8217;t really want to go into why this needs to be done, but if you want more info just type &#8220;<span style=\"font-family: Courier New;\">Get-Help About_Signing | More<\/span>&#8221; in PowerShell (or use The Google).<\/p>\n<h1>Download and Test PowerCLI<\/h1>\n<p>I&#8217;m using the latest version of PowerCLI \u2013 <a href=\"https:\/\/my.vmware.com\/web\/vmware\/details?downloadGroup=PCLI550&amp;productId=352\">5.5<\/a>. After installation, go ahead and run the &#8220;VMWare vSphere PowerCLI&#8221; program and type the following two commands:<\/p>\n<ol>\n<li><span style=\"font-family: Courier New;\">Connect-VIServer \u2013Server &lt;vSphere Server Name&gt;.<\/span> I&#8217;m assuming that your using Windows authentication &#8211; if not, then you&#8217;ll need to supply a username\/password.<\/li>\n<li><span style=\"font-family: Courier New;\">Get-VM<br \/>\n<\/span><\/li>\n<\/ol>\n<p>Ideally, you should see a list of all the VMs in your vCenter system.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"PowerCLI First Run\" src=\"http:\/\/www.ossintegrators.com\/blog\/wp-content\/uploads\/2014\/06\/061914_0006_GettingStar1.png\" width=\"997\" height=\"765\" \/><\/p>\n<p>If you want to get rid of that annoying &#8220;certificate is not valid&#8221; message, then simply run the command &#8220;<span style=\"color: cadetblue;\"><span style=\"font-family: Courier New; font-size: 10pt;\"><strong>Set-PowerCLIConfiguration\u00a0<\/strong><span style=\"color: black;\"><span style=\"color: cadetblue;\"><em>-InvalidCertificateAction\u00a0<\/em><span style=\"color: black;\"><span style=\"color: maroon;\">Ignore<\/span><\/span><\/span><\/span><\/span>&#8220;.<\/span><\/p>\n<h1>Powershell IDEs<\/h1>\n<p>Now that you have both PowerShell and PowerCLI working, it&#8217;s time to test out an IDE. You&#8217;ll want an IDE for a number of reasons: syntax highlighting, in-line documentation, and debugging are just a few. While there are quite a few options, let&#8217;s just look at two free ones: PowerShell ISE from Microsoft and PowerGUI from Dell.<\/p>\n<h1>PowerShell ISE<\/h1>\n<p>The latest PowerShell ISE is included in the Windows Management Framework 4.0, available <a href=\"http:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=40855\">here<\/a>. Once installed, type &#8220;Add-PSSnapin Vmware*&#8221; in the PowerShell window to load the PowerCLI components. Doing so allows all the IDE enhancements like IntelliSense to work (see below).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"PowerShell ISE Intellisense\" src=\"http:\/\/www.ossintegrators.com\/blog\/wp-content\/uploads\/2014\/06\/061914_0006_GettingStar2.png\" width=\"338\" height=\"196\" \/><\/p>\n<h2>PowerGUI<\/h2>\n<p>Another free IDE option is PowerGUI, available <a href=\"http:\/\/www.quest.com\/powergui-freeware\/\">here<\/a>. After install, run it and choose &#8220;File-&gt;PowerShell Libraries&#8221; and check the VMWare.VimAutomation.Core module to enable Intellisense and inline help for PowerCLI:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"PowerGUI Module Options\" src=\"http:\/\/www.ossintegrators.com\/blog\/wp-content\/uploads\/2014\/06\/061914_0006_GettingStar3.png\" width=\"508\" height=\"396\" \/><\/p>\n<p>IMO, PowerGUI does many things better than PowerShell ISE:<\/p>\n<ul>\n<li>I like the debugger better \u2013 it feels closer to the Visual Studio &#8220;experience&#8221;.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"PowerGUI Debugger\" src=\"http:\/\/www.ossintegrators.com\/blog\/wp-content\/uploads\/2014\/06\/061914_0006_GettingStar4.png\" width=\"456\" height=\"304\" \/><\/p>\n<ul>\n<li>The Variables window is nice \u2013 it gives you quick access to all variables and their values in a script. It mostly eliminates having to step through your script to see what values a variable contains.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"PowerGUI Variables Window\" src=\"http:\/\/www.ossintegrators.com\/blog\/wp-content\/uploads\/2014\/06\/061914_0006_GettingStar5.png\" width=\"377\" height=\"193\" \/><\/p>\n<ul>\n<li>I like the inline help implementation \u2013 it makes learning\/exploring much easier (just hover over a cmdlet for help)!<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"PowerGUI Intellisense and Inline Help\" src=\"http:\/\/www.ossintegrators.com\/blog\/wp-content\/uploads\/2014\/06\/061914_0006_GettingStar6.png\" width=\"1029\" height=\"187\" \/><\/p>\n<ul>\n<li>It includes the PowerGUI Admin Console \u2013 a separate program that makes it easy to explore the namespaces and values of a PowerShell SnapIns.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"PowerGUI Administrator Console\" src=\"http:\/\/www.ossintegrators.com\/blog\/wp-content\/uploads\/2014\/06\/061914_0006_GettingStar7.png\" width=\"591\" height=\"502\" \/><\/p>\n<p>A nifty feature of the Admin console is that right-clicking on any item in the tree and selecting &#8220;Properties&#8221; brings up a window showing the PowerShell script used to generate the output! This can be a real time saver.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"PowerGUI Administrator Properties\" src=\"http:\/\/www.ossintegrators.com\/blog\/wp-content\/uploads\/2014\/06\/061914_0006_GettingStar8.png\" width=\"1201\" height=\"626\" \/><\/p>\n<h1>Simple Snapshot Script<\/h1>\n<p>Now we&#8217;re ready to write a script that will allow us to snapshot multiple VMs at once, in this case all members of a vApp. The high-level flow we need is:<\/p>\n<ul>\n<li>Prompt for a snapshot name<\/li>\n<li>Connect to the vCenter Server<\/li>\n<li>Stop the vApp<\/li>\n<li>Get all the VMs in the vApp and snapshot them<\/li>\n<li>Start the vApp<\/li>\n<\/ul>\n<p>The completed script looks like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"PowerCLI Snapshot Script\" src=\"http:\/\/www.ossintegrators.com\/blog\/wp-content\/uploads\/2014\/06\/061914_0006_GettingStar9.png\" width=\"532\" height=\"260\" \/><span style=\"color: green; font-family: Courier New; font-size: 10pt;\"><br \/>\n<\/span><\/p>\n<p>Explanation:<\/p>\n<ul>\n<li>Line 4: this is the &#8220;Powershell Way&#8221; for getting user input \u2013 Parameters. Using Parameters allows the script to be called from other scripts. See <a href=\"http:\/\/technet.microsoft.com\/en-us\/magazine\/jj554301.aspx\">here<\/a> for a nice overview.<\/li>\n<li>Line 10: gets the vApp object that we want to snap.<\/li>\n<li>Line 11: add all the VMs in the vApp to a variable so we can actually snapshot them later.<\/li>\n<li>Lines 13-15: do the actual work!<\/li>\n<\/ul>\n<h1>Conclusion<\/h1>\n<p>I hope this little walkthrough was useful \u2013 I know I will be using PowerShell and PowerCLI a lot more in the future (I only wish I had looked into it sooner!).<\/p>\n","protected":false},"excerpt":{"rendered":"<div style=\"padding-bottom:20px; padding-top:10px;\" class=\"hupso-share-buttons\"><!-- Hupso Share Buttons - http:\/\/www.hupso.com\/share\/ --><a class=\"hupso_toolbar\" href=\"http:\/\/www.hupso.com\/share\/\"><img src=\"http:\/\/static.hupso.com\/share\/buttons\/share-small.png\" border=\"0\" style=\"padding-top:5px; float:left;\" alt=\"Share\"\/><\/a><script type=\"text\/javascript\">var hupso_services_t=new Array(\"Twitter\",\"Facebook\",\"Google Plus\",\"Linkedin\",\"Digg\",\"Reddit\");var hupso_toolbar_size_t=\"small\";var hupso_counters_lang = \"en_US\";var hupso_url_t=\"\";var hupso_title_t=\"Getting Started with VMWare PowerCLI\";<\/script><script type=\"text\/javascript\" src=\"http:\/\/static.hupso.com\/share\/js\/share_toolbar.js\"><\/script><!-- Hupso Share Buttons --><\/div><p>I&#8217;ve recently been spending some time &#8220;cleaning up&#8221; our VMWare infrastructure and making it more functional \u2013 both for demos and for internal lab use. To that end, I&#8217;ve been trying to automate\/simplify as many things as possible. One task that I&#8217;d been wanting to simplify is snapshot creation\/deletion of multiple VMs at once since [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[39],"tags":[54,41,42,40,43,55],"class_list":["post-335","post","type-post","status-publish","format-standard","hentry","category-vmware","tag-netcool","tag-powercli","tag-powergui","tag-powershell","tag-powershell-ise","tag-vmware"],"_links":{"self":[{"href":"http:\/\/www.ossintegrators.com\/blog\/wp-json\/wp\/v2\/posts\/335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ossintegrators.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ossintegrators.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ossintegrators.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ossintegrators.com\/blog\/wp-json\/wp\/v2\/comments?post=335"}],"version-history":[{"count":16,"href":"http:\/\/www.ossintegrators.com\/blog\/wp-json\/wp\/v2\/posts\/335\/revisions"}],"predecessor-version":[{"id":384,"href":"http:\/\/www.ossintegrators.com\/blog\/wp-json\/wp\/v2\/posts\/335\/revisions\/384"}],"wp:attachment":[{"href":"http:\/\/www.ossintegrators.com\/blog\/wp-json\/wp\/v2\/media?parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ossintegrators.com\/blog\/wp-json\/wp\/v2\/categories?post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ossintegrators.com\/blog\/wp-json\/wp\/v2\/tags?post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}