{"id":308,"date":"2021-04-11T04:55:19","date_gmt":"2021-04-11T04:55:19","guid":{"rendered":"https:\/\/thecloudwiki.com\/?p=308"},"modified":"2021-06-20T10:39:43","modified_gmt":"2021-06-20T10:39:43","slug":"powercli-useful-single-liners","status":"publish","type":"post","link":"https:\/\/thecloudwiki.com\/?p=308","title":{"rendered":"PowerCLI &#8211; Useful single liners"},"content":{"rendered":"\n<p class=\"has-extra-small-font-size wp-block-paragraph\" style=\"line-height:0\"><strong>Vm&#8217;s and space details <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-Cluster &lt;ClusterName&gt;|get-vm|select Name,@{Name=\"ProvisionedSpace\";E={&#91;Math]::Round(($_.ProvisionedSpaceGB),2)}},@{Name=\"UsedSpace\";E={&#91;Math]::Round(($_.UsedSpaceGB),2)}}<\/code><\/pre>\n\n\n\n<p class=\"has-extra-small-font-size wp-block-paragraph\"><strong>Find Thick provisioning Vm&#8217;s in a datastore<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-Datastore  | Get-VM | Get-HardDisk | Where {$_.storageformat -eq \"Thick\" } | Select Parent, Name, CapacityGB, storageformat | FT -AutoSize<\/code><\/pre>\n\n\n\n<p class=\"has-extra-small-font-size wp-block-paragraph\"><strong>Find VM&#8217;s migration failed VM&#8217;s <\/strong>(in last 5 days)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-5 ) | Where {$_.FullFormattedMessage -like \"Cannot migrate*\"} |select CreatedTime, Net, FullFormattedMessage |export-csv e:\\cannot.csv<\/code><\/pre>\n\n\n\n<p class=\"has-extra-small-font-size wp-block-paragraph\"><strong>Find the Specific events across multiple hosts in a vCenter<\/strong> <br>(Below example we are checking for Down state)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-cluster|get-vmhost|Get-VIEvent -MaxSamples (&#91;int]::MaxValue)|Where {$_.FullFormattedMessage -match \"Down state\"} |select @{Name='Host';E={$_.Host.name}},@{N='Createdtime'; E={$_.CreatedTime}},@{N='Description';E={$_.FullF\normattedMessage}} |Export-Csv -Path c:\/temp\/Downstate.csv -NoTypeInformation -UseCulture<\/code><\/pre>\n\n\n\n<p class=\"has-extra-small-font-size wp-block-paragraph\"><strong>Find a Specific OS VM&#8217;s count <\/strong><br>(Below case we are looking for Windows 7 vm&#8217;s)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-cluster|select Name,@{N=\"Windoes7-VMCount\" ;E={(((get-cluster $_|get-vm).Extensiondata.guest.guestfullname -like \"*Windows 7*\")|Measure).count}}<\/code><\/pre>\n\n\n\n<p class=\"has-extra-small-font-size wp-block-paragraph\"><strong>Find OS details of VM&#8217;s<\/strong> &#8211; faster output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-Cluster &lt;clustername&gt;|Get-VM | Sort | Get-View -Property @(\"Name\", \"Config.GuestFullName\", \"Guest.GuestFullName\") |?{$_.Config.GuestFullName -match \"Server\"}| Select -Property Name, @{N=\"Configured OS\";E={$_.Config.GuestFullName}\n},  @{N=\"Running OS\";E={$_.Guest.GuestFullName}},@{N=\"cluster\";E={(get-vm ($_.name)|get-cluster).name}} |Format-Table -AutoSize<\/code><\/pre>\n\n\n\n<p class=\"has-extra-small-font-size wp-block-paragraph\"><strong>Find a VM IP address<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-VM|Select Name, @{N=\"IP Address\";E={@($_.guest.IPAddress&#91;0])}}<\/code><\/pre>\n\n\n\n<p class=\"has-extra-small-font-size wp-block-paragraph\"><strong>Find a vm product version<\/strong> (if specified)<br>(Below case we are trying to find Horizon UAG VM&#8217;s version)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-vm *UAG*|?{$_.powerstate -eq \"poweredon\"}|select name,@{N=\"UAG Version\";E={$_.ExtensionData.summary.config.product.version}}<\/code><\/pre>\n\n\n\n<p class=\"has-extra-small-font-size wp-block-paragraph\"><strong>Find GPU Profile of the VM&#8217;s <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-cluster |get-vm|select Name,NumCPU,MemoryGB,Powerstate,@{N=\"GPU Profile\";E={($_.ExtensionData.Config.hardware.Device.backing.vgpu)}},@{N=\"CapacityGB\";E={(get-vm $_|get-harddisk).CapacityGB}},@{N=\"storageformat\n\";E={(get-vm $_|get-harddisk).Storageformat}},@{N=\"HDD\/Disk Type\";E={(get-vm $_|get-harddisk).Persistence}},@{N=\"Host\";E={(get-vm $_|get-vmhost).name}}|ft -AutoSize\n<\/code><\/pre>\n\n\n\n<p class=\"has-extra-small-font-size wp-block-paragraph\"><strong>Find Esxi hosts Names with VMK&#8217;s in a specific segment<\/strong><br>(Below case we are looking for Esxi hosts having 172.1.1 Subnet in VMK&#8217;s)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-VMHost -state Connected|Get-VMHostNetworkAdapter -VMKernel |select @{N=\"Cluster\";E={($_.vmhost|get-cluster).name}},vmhost,Name,IP|?{$_.IP -match \"172.1.1\"} |ft -AutoSize<\/code><\/pre>\n\n\n\n<p class=\"has-extra-small-font-size wp-block-paragraph\"><strong>Find VM details &#8211; Who\/When provisioned and etc<\/strong><br>(Below case we are looking for VM name contains test)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-vm *test*|select Name,@{N=\"Cluster\";E={(get-vm $_|get-cluster).name}},Folder,NumCpu,MemoryGB,Powerstate,@{N=\"Hostname\";E={$_.ExtensionData.Guest.Hostname}},@{N=\"IPaddress\";E={$_.ExtensionData.Guest.IpAddress}},,@{N=\"Networkadapter\";E={(get-vm $_|Get-NetworkAdapter).networkname}},@{N=\"GuestFullName\";E={$_.ExtensionData.Guest.GuestFullName}},@{N=\"Boottime\";E={$_.ExtensionData.Summary.Runtime.Boottime}},@{N=\"CreatedTime\";E={(get-vm $_|Get-VIEvent  -MaxSamples(&#91;int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like \"clone*\"}).CreatedTime}},@{N=\"CreatedUser\";E={(get-vm $_|Get-VIEvent  -MaxSamples(&#91;int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like \"clone*\"}).Username}},@{N=\"Event\";E={(get-vm $_|Get-VIEvent  -MaxSamples(&#91;int]::MaxValue) | Where-Object {$_.FullFormattedMessage -like \"clone*\"}).FullFormattedMessage}}|export-csv C:\\temp\\testvminfo.csv<\/code><\/pre>\n\n\n\n<p class=\"has-extra-small-font-size wp-block-paragraph\"><strong>Find NSX logical Router Cluster, Datastore, CPU &amp; Memory reservations<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-Nsxlogicalrouter|select Name,@{N=\"Cluster\";E={$_.appliances.appliance.resourcePoolName}},@{N=\"Datastore\";E={$_.appliances.appliance.datastoreName}},@{N=\"CPU Reservation\";E={$_.appliances.appliance.cpuReservation.reservati\non}},@{N=\"Memory Reservation\";E={$_.appliances.appliance.memoryReservation.reservation}}|Sort-Object -Property Name|ft -AutoSize<\/code><\/pre>\n\n\n\n<p class=\"has-extra-small-font-size wp-block-paragraph\"><strong>Find NSX Edge Cluster, Datastore, CPU &amp; Memory reservations<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-NsxEdge|select @{N=\"VM-Name\";E={$_.appliances.appliance.vmName}},@{N=\"Cluster\";E={$_.appliances.appliance.resourcePoolName}},@{N=\"Datastore\";E={$_.appliances.appliance.datastoreName}},@{N=\"CPU Reservation\";E={$_.appliance\ns.appliance.cpuReservation.reservation}},@{N=\"Memory Reservation\";E_.appliances.appliance.memoryReservation.reservation}}|Sort-Object -Property Name|ft -AutoSize<\/code><\/pre>\n\n\n\n<p class=\"has-extra-small-font-size wp-block-paragraph\"><strong>VM&#8217;s Folder details &#8211; order by power state<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-Cluster |get-vm|select Name,powerstate,@{N=\"Folder\";E={$_.folder.name}},@{N=\"Root Folder\";E={$_.folder.parent}}|Sort-Object -Property powerstate|ft -AutoSize<\/code><\/pre>\n\n\n\n<p class=\"has-extra-small-font-size wp-block-paragraph\"><strong>Snapshot faster output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-View -ViewType virtualmachine -Filter @{\"Runtime.PowerState\"=\"poweredOn\"; \"snapshot\" = \"\"} -Property Name|% {get-vm -id $_.MoRef | get-snapshot}|select VM,Name,Powerstate,Description,Quiesced,@{N=\"Size\";E={\"{0:N2} GB\" -f\n($_.SizeGB)}},Created|ft -AutoSize\n<\/code><\/pre>\n\n\n\n<p class=\"has-extra-small-font-size wp-block-paragraph\"><strong>Find Disk consolidation required VM&#8217;s<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(Get-View -ViewType virtualmachine -Property Name, Runtime |where-object {$_.runtime.consolidationneeded -eq \"True\"}).Name<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vm&#8217;s and space details Find Thick provisioning Vm&#8217;s in a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1282,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"default","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[23],"tags":[5],"class_list":["post-308","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-powershell"],"uagb_featured_image_src":{"full":["https:\/\/thecloudwiki.com\/wp-content\/uploads\/2021\/04\/powercli-singleliners-feature-image-e1624185559529.png",400,247,false],"thumbnail":["https:\/\/thecloudwiki.com\/wp-content\/uploads\/2021\/04\/powercli-singleliners-feature-image-150x150.png",150,150,true],"medium":["https:\/\/thecloudwiki.com\/wp-content\/uploads\/2021\/04\/powercli-singleliners-feature-image-300x185.png",300,185,true],"medium_large":["https:\/\/thecloudwiki.com\/wp-content\/uploads\/2021\/04\/powercli-singleliners-feature-image-768x474.png",768,474,true],"large":["https:\/\/thecloudwiki.com\/wp-content\/uploads\/2021\/04\/powercli-singleliners-feature-image-1024x632.png",1024,632,true],"1536x1536":["https:\/\/thecloudwiki.com\/wp-content\/uploads\/2021\/04\/powercli-singleliners-feature-image-e1624185559529.png",400,247,false],"2048x2048":["https:\/\/thecloudwiki.com\/wp-content\/uploads\/2021\/04\/powercli-singleliners-feature-image-e1624185559529.png",400,247,false]},"uagb_author_info":{"display_name":"Sandeep","author_link":"https:\/\/thecloudwiki.com\/?author=1"},"uagb_comment_info":0,"uagb_excerpt":"Vm&#8217;s and space details Find Thick provisioning Vm&#8217;s in a [&hellip;]","_links":{"self":[{"href":"https:\/\/thecloudwiki.com\/index.php?rest_route=\/wp\/v2\/posts\/308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecloudwiki.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thecloudwiki.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thecloudwiki.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thecloudwiki.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=308"}],"version-history":[{"count":7,"href":"https:\/\/thecloudwiki.com\/index.php?rest_route=\/wp\/v2\/posts\/308\/revisions"}],"predecessor-version":[{"id":1283,"href":"https:\/\/thecloudwiki.com\/index.php?rest_route=\/wp\/v2\/posts\/308\/revisions\/1283"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thecloudwiki.com\/index.php?rest_route=\/wp\/v2\/media\/1282"}],"wp:attachment":[{"href":"https:\/\/thecloudwiki.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thecloudwiki.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thecloudwiki.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}