{"id":11771,"date":"2011-12-16T00:01:00","date_gmt":"2011-12-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/12\/16\/use-powershell-to-simplify-access-to-data-through-powercli\/"},"modified":"2011-12-16T00:01:00","modified_gmt":"2011-12-16T00:01:00","slug":"use-powershell-to-simplify-access-to-data-through-powercli","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-simplify-access-to-data-through-powercli\/","title":{"rendered":"Use PowerShell to Simplify Access to Data Through PowerCLI"},"content":{"rendered":"<p><b>Summary<\/b>: Two new features in VMware&#8217;s PowerCL make it easy for Windows PowerShell to gather virtualization configuration information.<\/p>\n<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today we have a guest blog written by Josh Atwell.<\/p>\n<p>. Take it away Josh&hellip;<\/p>\n<p style=\"margin: 0in 0in 0pt\"><span style=\"font-family: 'Calibri','sans-serif';font-size: 11pt\">Hi.&nbsp; I&#8217;m Josh Atwell and I&#8217;m a SystemsAdministrator focused on scripting and automation in large virtualenvironments.&nbsp; I am huge lover of Windows PowerShell, VMware&#8217;s PowerCLI, and all the fun ways they allow me to do more with less.&nbsp; You can regularly find me blogging or tweeting about Powershell and PowerCLI.<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt\"><span style=\"font-family: 'Calibri','sans-serif';font-size: 11pt\">Blog:&nbsp; <a href=\"http:\/\/www.vtesseract.com\/\" target=\"_blank\">http:\/\/www.vtesseract.com<\/a><\/span><\/p>\n<p style=\"margin: 0in 0in 0pt\"><span style=\"font-family: 'Calibri','sans-serif';font-size: 11pt\">Twitter: <a href=\"https:\/\/twitter.com\/Josh_Atwell\" target=\"_blank\">https:\/\/twitter.com\/Josh_Atwell<\/a><\/span><\/p>\n<p style=\"margin: 0in 0in 0pt\"><span style=\"font-family: 'Calibri','sans-serif';font-size: 11pt\">&nbsp;<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt\"><span style=\"font-family: 'Calibri','sans-serif';font-size: 11pt\">New to PowerCLI? I have a post specifically for those interested in getting started with PowerCLI.&nbsp; I update it<br \/>whenever new items come to my attention so I hope you find it useful!<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt\"><span style=\"font-family: 'Calibri','sans-serif';font-size: 11pt\"><a href=\"http:\/\/www.vtesseract.com\/post\/10685628144\/resources-for-getting-started-with-powercli-automation\" target=\"_blank\">http:\/\/www.vtesseract.com\/post\/10685628144\/resources-for-getting-started-with-powercli-automation<\/a><\/span><\/p>\n<\/p>\n<p>When I was approached by the Scripting Guys to guest blog this holiday season, I JUMPED at the opportunity. My exclamations scared the cat and led to a funny look from the Mrs. Why so excited? Because I was being given an opportunity to share with more people my favorite use of Windows PowerShell: VMware&#8217;s PowerCLI snap-in.<\/p>\n<\/p>\n<h2>Using the VMware PowerCLI snap-in<\/h2>\n<\/p>\n<p>The growing use of datacenter virtualization in organizations means more servers, more configurations, and more reporting for administrators to manage. Fortunately for all of us, Microsoft and VMware have worked hard to increase Windows PowerShell functionality in these environments. I would say they&#8217;ve done an amazing job; but unfortunately, gathering information in large environments can be very time consuming, even with Windows PowerShell. I&#8217;d like to share two of the best additions to VMware&#8217;s PowerCLI and how to use them to get information from the SDK quickly and efficiently. The two additions are <b>ExtensionData <\/b>and <b>New-VIProperty<\/b>.&nbsp;<\/p>\n<\/p>\n<h2>Using the Get-View cmdlet<\/h2>\n<\/p>\n<p>Gathering information with PowerCLI is generally straightforward, but there are times where the cmdlets don&#8217;t provide all of the information about an object that you may need. VMware addressed this problem with the <b>Get-View<\/b> cmdlet. The <b>Get-View<\/b> cmdlet allows you to retrieve .NET view objects based on the criteria that you provide for the object type and based on filters. With some practice, you can use <b>Get-View<\/b> to retrieve data from the VMware SDK very quickly. These calls are essentially raw data grabs, and they lack the Windows PowerShell processing provided by traditional cmdlets like <b>Get-VM<\/b>.<\/p>\n<\/p>\n<p>Let&#8217;s give it a shot and search for information about my virtual machine &#8220;w2k8-std-32-2&#8221;. We already know the Virtual Hardware version of the virtual machine can be grabbed quickly through vCenter Server; but for demonstration purposes, let&#8217;s assume that it has to be grabbed as part of a Windows PowerShell script. I&#8217;m hoping it&#8217;s on Virtual Hardware version&nbsp;7 because I need to take advantage of some specific features for that hardware version. Note that in the code shown here, the <i>Filter <\/i>parameter requires a <i>hash table <\/i>for the input.&nbsp;<\/p>\n<\/p>\n<p style=\"padding-left: 30px\">Get-View -ViewType VirtualMachine -Filter @{&#8220;Name&#8221; = &#8220;w2k8-std-32-2&#8221;}<\/p>\n<\/p>\n<p>This initial call returns and is significantly different than what is provided by a simple <b>Get-VM<\/b> call. This is shown in the image that follows.<\/p>\n<\/p>\n<p>&nbsp;<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0882.hsg-12-16-11-1.jpg\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0882.hsg-12-16-11-1.jpg\" \/><\/a><\/p>\n<\/p>\n<p>At first glance, the information here is pretty cryptic, but notice that you now have visibility into a variety of different properties such as the virtual machine&rsquo;s <b>Config<\/b> information. We&#8217;ll set our <b>Get-View<\/b> call to a variable and dig into <b>Config<\/b> to see what we can find there. I use the code shown here to examine the <b>Config<\/b> property.<\/p>\n<\/p>\n<p style=\"padding-left: 30px\">$vmview = Get-View -ViewType VirtualMachine -Filter @{&#8220;Name&#8221; = &#8220;w2k8-std-32-2&#8221;}<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">$vmview.Config<\/p>\n<\/p>\n<p>The command and output associated with that command are shown in the following image.<\/p>\n<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3301.hsg-12-16-11-2.jpg\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3301.hsg-12-16-11-2.jpg\" \/><\/a><\/p>\n<p>I have condensed the information provided because it is quite extensive, but you can see right away that Version = vmx-07! Fantastic! Although it&#8217;s not as simply put as in vCenter server, it wasn&#8217;t too difficult. Using <b>Get-View<\/b> made it a very quick call and return.<\/p>\n<\/p>\n<p><strong>Get-View<\/strong> calls can start to get quite complex and difficult if you are not familiar with the VMware SDK. VMware addressed this in a big way with their 4.1 release of PowerCLI.&nbsp;<\/p>\n<\/p>\n<h2>Introducing ExtensionData!<\/h2>\n<\/p>\n<p>Taking a look into the virtual machine object, you see a new property called <b>ExtensionData<\/b>. <b>ExtensionData<\/b> was released with PowerCLI 4.1, and it provided Windows PowerShell folks a new and easier mechanism for getting into the vCenter SDK. Did I mention that it&#8217;s easy to use? It&#8217;s SUPER easy to use! In the image that follows, I use the <b>Get-View <\/b>cmdlet and pipe the output to the <b>Get-Member <\/b>cmdlet to display the <b>ExtensionData<\/b> property.<\/p>\n<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3323.hsg-12-16-11-3.jpg\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3323.hsg-12-16-11-3.jpg\" \/><\/a><\/p>\n<p>Let&#8217;s look at using <b>ExtensionData<\/b> to find the Virtual Hardware version that we just found with <b>Get-View<\/b>. We&#8217;ll begin by grabbing the <b>VirtualMachine<\/b> object for the virtual machine. You may ask why we would want to use <b>Get-VM<\/b> at all when we could simply use <b>Get-View<\/b>. That is an excellent question, and it is often discussed, but slightly beyond the scope of this post.<\/p>\n<\/p>\n<p>Back to work&hellip;<\/p>\n<\/p>\n<p>In the code that follows, I illustrate using the <b>Get-VM <\/b>cmdlet to retrieve the <b>Config<\/b><i> <\/i>property from the object stored in the <b>ExtensionData<\/b> property.&nbsp;<\/p>\n<\/p>\n<p style=\"padding-left: 30px\">$vm = Get-VM &#8220;w2k8-std-32-2&#8221;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">$vm.ExtensionData.Config<\/p>\n<\/p>\n<p>The image that is shown here displays the information that is contained in the <b>Config<\/b><i> <\/i>property.<\/p>\n<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4682.hsg-12-16-11-4.jpg\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4682.hsg-12-16-11-4.jpg\" \/><\/a><\/p>\n<\/p>\n<p>Well, that was easy enough, and now you have direct access to that info and more. The information provided with <b>ExtensionData<\/b> is exactly the same as what is provided with the <b>Get-View<\/b> command. However, we now have considerable flexibility for when and where we decide to grab this data.<\/p>\n<\/p>\n<p>We can take this one step further and generate a quick one-liner to report on the Virtual Hardware version for all virtual machines in my little lab setup. In the code that follows, notice that we use <b>ExtensionData<\/b> right in line without skipping a beat.&nbsp;<\/p>\n<\/p>\n<p style=\"padding-left: 30px\">Get-VM | Select Name,@{Name=&#8221;HardwareVersion&#8221;;Expression={$_.ExtensionData.Config.Version}}<\/p>\n<\/p>\n<p>&nbsp;The command and data returned by that command are shown here.<\/p>\n<\/p>\n<p>&nbsp;<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1263.hsg-12-16-11-5.jpg\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1263.hsg-12-16-11-5.jpg\" \/><\/a><\/p>\n<\/p>\n<h2>Step it Up with New-VIProperty<\/h2>\n<\/p>\n<p>PowerCLI also provides one more little feature that was introduced in version 4.1 that makes a report like this even easier in larger scripts. Because we know the hardware type is part of the virtual machine, let&#8217;s make it a direct property of the object with <b>New-VIProperty<\/b>! Here we&#8217;re using a value from the <b>ExtensionData<\/b> property for that <b>VirtualMachine<\/b> object. This will allow us to simplify our query. The simplified query is shown here.&nbsp;<\/p>\n<\/p>\n<p style=\"padding-left: 30px\">New-VIProperty -Name HardwareVersion -ObjectType VirtualMachine -ValueFromExtensionProperty &#8216;Config.Version&#8217;<\/p>\n<p style=\"padding-left: 30px\">\n<p style=\"padding-left: 30px\">Get-VM | Select Name, HardwareVersion<\/p>\n<\/p>\n<p>The command and the output associated by the command are shown here.<\/p>\n<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4530.hsg-12-16-11-6.jpg\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4530.hsg-12-16-11-6.jpg\" \/><\/a>&nbsp;<\/p>\n<\/p>\n<p>Keep in mind that this new property for the <b>VirtualMachine<\/b> object is only available while the session is active. If you plan to add new properties by using <b>New-VIProperty<\/b>, make sure that you include their creation in your scripts. If you don&#8217;t, you may be left wondering why you don&#8217;t have any data for that property. Doh! This lack of data is shown in the image that follows.<\/p>\n<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3225.hsg-12-16-11-7.jpg\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3225.hsg-12-16-11-7.jpg\" \/><\/a>&nbsp;<\/p>\n<\/p>\n<p>This was a simple example of these very PowerShell-ful features from VMware&#8217;s PowerCLI. I definitely recommend taking some time to explore with <b>Get-View<\/b> and the <b>ExtensionData<\/b> property in your environment. You&#8217;ll find a wealth of information about many objects in the inventory, including VMhosts, virtual machines, resource pools, folders, and more. When you find those valuable tidbits, you can speed up and simplify your scripts even more with the <b>New-ViProperty<\/b> cmdlet. That&#8217;s all for now! Good luck and good scripting!<\/p>\n<\/p>\n<p>~Josh<\/p>\n<\/p>\n<p>Thank you, Josh, for taking the time to share your knowledge with us. Join us tomorrow when I will talk about customizing the Windows PowerShell console. It is a cool blog that you will not want to miss.<\/p>\n<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Two new features in VMware&#8217;s PowerCL make it easy for Windows PowerShell to gather virtualization configuration information. Microsoft Scripting Guy, Ed Wilson, is here. Today we have a guest blog written by Josh Atwell. . Take it away Josh&hellip; Hi.&nbsp; I&#8217;m Josh Atwell and I&#8217;m a SystemsAdministrator focused on scripting and automation in large [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,299,300,3,130,45],"class_list":["post-11771","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-josh-atwell","tag-powercli","tag-scripting-guy","tag-servers","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Two new features in VMware&#8217;s PowerCL make it easy for Windows PowerShell to gather virtualization configuration information. Microsoft Scripting Guy, Ed Wilson, is here. Today we have a guest blog written by Josh Atwell. . Take it away Josh&hellip; Hi.&nbsp; I&#8217;m Josh Atwell and I&#8217;m a SystemsAdministrator focused on scripting and automation in large [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=11771"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11771\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=11771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}