{"id":786,"date":"2013-06-26T22:58:40","date_gmt":"2013-06-26T20:58:40","guid":{"rendered":"http:\/\/michlstechblog.info\/blog\/?p=786"},"modified":"2018-02-23T08:29:50","modified_gmt":"2018-02-23T07:29:50","slug":"vmware-a-incomplete-list-with-some-powercli-scripts-for-vsphere","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/vmware-a-incomplete-list-with-some-powercli-scripts-for-vsphere\/","title":{"rendered":"VMware: A (incomplete) list with some PowerCLI Scripts for vSphere"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_786 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_786')){$('.twoclick_social_bookmarks_post_786').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"VMware%3A%20A%20%28incomplete%29%20list%20with%20some%20PowerCLI%20Scripts%20for%20vSphere\",\"the_excerpt\":\"Hi%2C%0D%0A%0D%0Afor%20VMware%20vSphere%20a%20very%20powerfull%20Scripting%20engine%20is%20available%20which%20is%20called%20PowerCLI.%20PowerCLI%20based%20on%2C%20the%20also%20very%20powerfull%2C%20Microsoft%20powershell.%0D%0A%0D%0AThe%20Basics%0D%0A%0D%0AGet%20the%20PowerCLI%20package%20from%20the%20VMware%20website%20and%20install%20it%20on%20your%20Windows%20Computer%20or%20on%20the%20VMware%20vCenter%20Server.%20Ok%2C%20lets%20start.%0D%0A%0D%0AOpen%C2%A0%20PowerCLI%20Command%20prompt%20by%20clicking%20%22VMware%20vSphere%20PowerCLI%20%2832-Bi%20...\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/vmware-a-incomplete-list-with-some-powercli-scripts-for-vsphere\\\/\",\"post_id\":786,\"post_title_referrer_track\":\"VMware%3A+A+%28incomplete%29+list+with+some+PowerCLI+Scripts+for+vSphere\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hi,<\/p>\n<p>for VMware vSphere a very powerfull Scripting engine is available which is called PowerCLI. PowerCLI based on, the also very powerfull, Microsoft powershell.<\/p>\n<p><strong>The Basics<\/strong><\/p>\n<p>Get the PowerCLI package from the <a title=\"VMware PowerCLI \" href=\"http:\/\/www.vmware.com\/support\/developer\/PowerCLI\/index.html\" target=\"_blank\">VMware website<\/a> and install it on your Windows Computer or on the VMware vCenter Server. Ok, lets start.<\/p>\n<p>Open\u00a0 PowerCLI Command prompt by clicking &#8220;VMware vSphere PowerCLI (32-Bit)&#8221; Icon. Note: The 64Bit Version(both the 32Bit and 64Bit versions were installed) did support all commands. For example when you clone a computer by the New-VM Command-Let and specifiy the OSCustomizationSpec parameter the follwing error was thrown.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nNew-VM -VM &quot;ComputerTemplate&quot; -Location &quot;Windows Clients&quot; -ResourcePool Resources -OSCustomizationSpec &quot;Windows XP Client&quot;\u00a0 -Datastore esxdatastore0 -Name &quot;COMPUTER1&quot;\r\nNew-VM : 26.06.2013 23:37:42\u00a0\u00a0\u00a0 New-VM\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)<\/pre>\n<p>However start the 32Bit Version :-). First you have to connect to your vCenter. If your Windows Account, with which you logged on, have the appropriate rights to connect to vCenter simply call<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Connect-VIServer NameOfYourvCenter.domain.com<\/pre>\n<p>If logon successeds 2 global object variables are set to which all following commands refers to.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nglobal:DefaultVIServer\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 NameOfYourvCenter.domain.com\r\nglobal:DefaultVIServers\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {NameOfYourvCenter.domain.com}<\/pre>\n<p>Alternatively you can assign the vCenter Object to a variable and append the -Server &lt;VIServer[]&gt; parameter to each command. This is usefull if you have multiple instances of vCenter.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$oSecondVCenter=Connect-VIServer NameOfYourvCenter.domain.com\r\nSet-Vm -VM &quot;OldName&quot; -name &quot;New-Name&quot; -Server $oSecondVCenter\r\n<\/pre>\n<p>By default, all command are executed synchronous. This means the command prompt returned not until the tasks is finished. To start jobs asynchronus append the -RunAsync parameter to each command line.<\/p>\n<p>Lets start with some example scripts<br \/>\n<!--more--><\/p>\n<p><strong>VM Tools<\/strong><\/p>\n<p>Update VMware tools for a single host without rebooting a virtual machine<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nUpdate-Tools &quot;Windows 8 Client&quot; -NoReboot\r\n<\/pre>\n<p>Combine this with an other command-let demonstrates the power of PowerCLI and Powershell.<br \/>\nThe following &#8220;one liner&#8221; updates the vmware tools on all Windows Guest which are powered on and have already installed an on old version of the vmware tools.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">get-vm|where-object{$_.Guest.State -eq &quot;Running&quot; -and $_.ExtensionData.Guest.ToolsStatus -eq &quot;toolsOld&quot; -and $_.ExtensionData.Guest.GuestFamily -eq &quot;windowsGuest&quot;}|Update-Tools -NoReboot<\/pre>\n<p><strong>Host Storage<\/strong><\/p>\n<p>Get all Storage LUNs of an ESX Host<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Get-ScsiLun -VMHost ESXHost.domain.com -CanonicalName *<\/pre>\n<p>Get Information of a specific LUN, store in a variable<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">$oStorLUN=Get-ScsiLun -CanonicalName naa.600000e00d1100003245789294528900 -VmHost ESXHost.domain.com<\/pre>\n<p>To Show the properties pipe it to Format-list<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Get-ScsiLun -CanonicalName naa.600000e00d1100003245789294528900 -VmHost ESXHost.domain.com|Format-List<\/pre>\n<p>Set the MultiPath Policy to Round Robin<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Set-ScsiLun -SCSILun $oStorLUN -MultipathPolicy RoundRobin<\/pre>\n<p>And another one liner, set the MultipathPolicy for all LUNs of Type &#8220;disk&#8221;, with current policy &#8220;MostRecentlyUsed&#8221; and which are not local to MultipathPolicy RoundRobin<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Get-ScsiLun -CanonicalName * -VmHost ESXHost.domain.com|where-Object{$_.MultipathPolicy -eq &quot;MostRecentlyUsed&quot; -and $_.LunType -eq &quot;disk&quot; -and $_.IsLocal -eq $false}|Set-ScsiLun -MultipathPolicy RoundRobin<\/pre>\n<p>All pathes for a LUN<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Get-ScsiLunPath -ScsiLun $oStorLUN<\/pre>\n<p>Get all FibreChannel WWN<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nGet-VMhosthba|Where-Object{$_.Type -eq  &quot;FibreChannel&quot; -and $_.Status -eq &quot;online&quot;}|Select-Object device,NodeWorldWideName,PortWorldWideName,VMHost|%{New-Object PSObject -Property @{device=$_.device;NodeWorldWideName=&quot;{0:X0}&quot; -f $_.NodeWorldWideName;PortWorldWideName=&quot;{0:X0}&quot; -f $_.PortWorldWideName;VMHost=$_.VMHost;}}\r\n<\/pre>\n<p>ReScan HBAs <\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nGet-VMHost | Get-VMHostStorage -RescanAllHba\r\n<\/pre>\n<p>Copy the local file d:\\temp\\test.txt to folder C:\\TEMP of the virtual machine(VMware tools requiered) named VMComputer<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Copy-VMGuestFile -Source &quot;d:\\temp\\test.txt&quot; -Destination &quot;C:\\TEMP&quot; -VM &quot;VMComputer&quot; -LocalToGuest<\/pre>\n<p>To reverse the operation use GuestToLocal instead of LocalToGuest<\/p>\n<p><strong>Guests Network<\/strong>(VMware tools on the virtual machine requiered)<br \/>\nGet network configuration of all interfaces at virtual machine VMComputer<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Get-VMGuestNetworkInterface -VM &quot;VMComputer&quot;<\/pre>\n<p><strong>Network<\/strong><br \/>\nGet Status of Host Firewall<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Get-VMHostFirewallException -VMHost ESXHost.domain.com<\/pre>\n<p>Get a specific Firewall Exception<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">Get-VMHostFirewallException -VMHost ESXHost.domain.com -Name SSH-Client<\/pre>\n<p>To be continued&#8230;.early:-)<\/p>\n<p>Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, for VMware vSphere a very powerfull Scripting engine is available which is called PowerCLI. PowerCLI based on, the also very powerfull, Microsoft powershell. The Basics Get the PowerCLI package from the VMware website and install it on your Windows Computer or on the VMware vCenter Server. Ok, lets start. Open\u00a0 PowerCLI Command prompt by &hellip; <a href=\"https:\/\/michlstechblog.info\/blog\/vmware-a-incomplete-list-with-some-powercli-scripts-for-vsphere\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">VMware: A (incomplete) list with some PowerCLI Scripts for vSphere<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[220,217,801,800,104,215,216,218,14,219,20],"class_list":["post-786","post","type-post","status-publish","format-standard","hentry","category-vmware","tag-application","tag-example","tag-fc-wwn","tag-fibrechannel","tag-not","tag-powercli","tag-scripts","tag-valid","tag-vmware-2","tag-win32","tag-windows-2"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=786"}],"version-history":[{"count":36,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/786\/revisions"}],"predecessor-version":[{"id":5239,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/786\/revisions\/5239"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}