<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>disk Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/tag/disk/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/tag/disk/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Wed, 08 Apr 2020 09:24:57 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.10</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>disk Archives - LucD notes</title>
	<link>https://www.lucd.info/tag/disk/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>Orphaned files and folders &#8211; Spring cleaning</title>
		<link>https://www.lucd.info/2011/04/25/orphaned-files-and-folders-spring-cleaning/</link>
					<comments>https://www.lucd.info/2011/04/25/orphaned-files-and-folders-spring-cleaning/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Mon, 25 Apr 2011 18:53:41 +0000</pubDate>
				<category><![CDATA[datastore]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Virtual Machine]]></category>
		<category><![CDATA[VMDK]]></category>
		<category><![CDATA[disk]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[Folder]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=3217</guid>

					<description><![CDATA[In our PowerCLI book we presented a Delete-Harddisk function in Chapter 7. One [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>In our PowerCLI book we presented a <strong>Delete-Harddisk</strong> function in Chapter 7.</p>
<p>One of our readers asked if that function could be used to remove orphaned VMDK files from one or more datastores. Now unfortunately that is not the case since the function we presented in chapter 7 uses the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.VirtualMachine.html#reconfigure" target="_blank" rel="noopener noreferrer">ReconfigVM_Task</a> method to remove the harddisk.</p>
<p>In the <a href="https://communities.vmware.com/community/vmtn/vsphere/automationtools/powercli?view=discussions" target="_blank" rel="noopener noreferrer">PowerCLI Community</a> there are some thread that provide scripts to report on orphaned VMDK files, but most of these are quite old.</p>
<p>So I decided to write a new script that would report on orphaned folders and VMDK files and that would have an option to remove these folders and files.</p>
<p><span id="more-3217"></span></p>
<h3><span style="background-color: #ffff00;">Update March 19th 2013</span></h3>
<p>The following changes were made to the function</p>
<ul>
<li>Fixed the problem where a VMDK in the root of a datastore, which you shouldn&#8217;t have of course, would erase the complete datastore content (ouch). Thanks Olivier</li>
<li>Added <strong>WhatIf</strong> functionality. You can now check what the <strong>Delete</strong> switch will do before actually removing files and folders.</li>
<li>Added details for lone VMDK. See the properties of the returned objects.</li>
</ul>
<h3><span style="background-color: #ffff00;">Update May 22nd 2011</span></h3>
<p>Two corrections in this new version of the script.</p>
<ul>
<li><a id="commentauthor-987" href="https://www.hypervisor.fr/" rel="external nofollow">NiTRo</a> reported a problem with guests that had only a .vmdk, and not their .vmx, file located on the datastore(s) to be cleaned</li>
<li>The COS became a virtual machine with ESX 4.x and thus has a .vmdk file as well. Although the COS files should be located on a local datastore, the script now ignores COS .vmdk files when it encounters them on the datastore(s) to be cleaned.</li>
</ul>
<h2>The Script</h2>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">function Remove-OrphanedData {
&lt;#
.SYNOPSIS   Remove orphaned folders and VMDK files
.DESCRIPTION   The function searches orphaned folders and VMDK files
   on one or more datastores and reports its findings.
   Optionally the function removes  the orphaned folders   and VMDK files
.NOTES   Author:  Luc Dekens
.PARAMETER Datastore
   One or more datastores.
   The default is to investigate all shared VMFS datastores
.PARAMETER Delete
   A switch that indicates if you want to remove the folders
   and VMDK files
.EXAMPLE
   PS&gt; Remove-OrphanedData -Datastore ds1
.EXAMPLE
  PS&gt; Get-Datastore ds* | Remove-OrphanedData
.EXAMPLE
  PS&gt; Remove-OrphanedData -Datastore $ds -Delete
#&gt;

  [CmdletBinding(SupportsShouldProcess=$true)]

  param(
  [parameter(Mandatory=$true,ValueFromPipeline=$true)]
  [PSObject[]]$Datastore,
  [switch]$Delete
  )

  begin{
    $fldList = @{}
    $hdList = @{}

    $fileMgr = Get-View FileManager
  }

  process{
    foreach($ds in $Datastore){
      if($ds.GetType().Name -eq &quot;String&quot;){
        $ds = Get-Datastore -Name $ds
      }
      if($ds.Type -eq &quot;VMFS&quot; -and $ds.ExtensionData.Summary.MultipleHostAccess){
        Get-VM -Datastore $ds | %{
          $_.Extensiondata.LayoutEx.File | where{&quot;diskDescriptor&quot;,&quot;diskExtent&quot; -contains $_.Type} | %{
            $fldList[$_.Name.Split('/')[0]] = $_.Name
            $hdList[$_.Name] = $_.Name
          }
        }
        Get-Template | where {$_.DatastoreIdList -contains $ds.Id} | %{
          $_.Extensiondata.LayoutEx.File | where{&quot;diskDescriptor&quot;,&quot;diskExtent&quot; -contains $_.Type} | %{
            $fldList[$_.Name.Split('/')[0]] = $_.Name
            $hdList[$_.Name] = $_.Name
          }
        }

        $dc = $ds.Datacenter.Extensiondata

        $flags = New-Object VMware.Vim.FileQueryFlags
        $flags.FileSize = $true
        $flags.FileType = $true

        $disk = New-Object VMware.Vim.VmDiskFileQuery
        $disk.details = New-Object VMware.Vim.VmDiskFileQueryFlags
        $disk.details.capacityKb = $true
        $disk.details.diskExtents = $true
        $disk.details.diskType = $true
        $disk.details.thin = $true

        $searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
        $searchSpec.details = $flags
        $searchSpec.Query += $disk
        $searchSpec.sortFoldersFirst = $true

        $dsBrowser = Get-View $ds.ExtensionData.browser
        $rootPath = &quot;[&quot; + $ds.Name + &quot;]&quot;
        $searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
        foreach($folder in $searchResult){
          if($fldList.ContainsKey($folder.FolderPath.TrimEnd('/'))){
            foreach ($file in $folder.File){
              if(!$hdList.ContainsKey($folder.FolderPath + $file.Path)){
                New-Object PSObject -Property @{
                  Folder = $folder.FolderPath
                  Name = $file.Path
                  Size = $file.FileSize
                  CapacityKB = $file.CapacityKb
                  Thin = $file.Thin
                  Extents = [string]::Join(',',($file.DiskExtents))
                }
                if($Delete){
                  If ($PSCmdlet.ShouldProcess(($folder.FolderPath + &quot; &quot; + $file.Path),&quot;Remove VMDK&quot;)){
                    $dsBrowser.DeleteFile($folder.FolderPath + $file.Path)
                  }
                }
              }
            }
          }
          elseif($folder.File | where {&quot;cos.vmdk&quot;,&quot;esxconsole.vmdk&quot; -notcontains $_.Path}){
            $folder.File | %{
              New-Object PSObject -Property @{
                Folder = $folder.FolderPath
                Name = $_.Path
                Size = $_.FileSize
                CapacityKB = $_.CapacityKB
                Thin = $_.Thin
                Extents = [String]::Join(',',($_.DiskExtents))
              }
            }
            if($Delete){
              if($folder.FolderPath -eq $rootPath){
                $folder.File | %{
                  If ($PSCmdlet.ShouldProcess(($folder.FolderPath + &quot; &quot; + $_.Path),&quot;Remove VMDK&quot;)){
                    $dsBrowser.DeleteFile($folder.FolderPath + $_.Path)
                  }
                }
              }
              else{
                If ($PSCmdlet.ShouldProcess($folder.FolderPath,&quot;Remove Folder&quot;)){
                  $fileMgr.DeleteDatastoreFile($folder.FolderPath,$dc.MoRef)
                }
              }
            }
          }
        }
      }
    }
  }
}</pre><p></p>
<h4>Annotations</h4>
<p><strong>Line 22, 91, 110, 116</strong>: These handle the <strong>WhatIf</strong> switch</p>
<p><strong>Line 26</strong>: The function accepts datastore by name or as a DatastoreImpl object. Hence the PSObject type. There has to be a Datastore argument !</p>
<p><strong>Line 39-41</strong>: Cheap OBN implementation.</p>
<p><strong>Line 42</strong>: The test makes sure only <strong>VMFS</strong> datastores that are <strong>shared</strong> are handled.</p>
<p><strong>Line 43-48</strong>: The function creates a hash table with all the files that are used by the virtual machines on the datastore. The <strong>$fldList</strong> hash table contains the folders and the <strong>$hdList</strong> hash table contains all the disk related files (they have the <strong>diskDescriptor</strong> or the <strong>diskExtent type</strong>).</p>
<p><strong>Line 49-54</strong>: The <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/html/Get-Template.html" target="_blank" rel="noopener noreferrer">Get-Template</a> cmdlet doesn&#8217;t have a <strong>Datastore</strong> parameter, that&#8217;s why this block uses a different method of determining if there are Templates on a specific datastore.</p>
<p><strong>Line 58-75</strong>: The parameters for the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.host.DatastoreBrowser.html#searchSubFolders" target="_blank" rel="noopener noreferrer">SearchDatastoreSubFolders</a> method are prepared and at the end the method is called.</p>
<p><strong>Line 77-105</strong>: The returned results from the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.host.DatastoreBrowser.html#searchSubFolders" target="_blank" rel="noopener noreferrer">SearchDatastoreSubFolders</a> method are analysed.</p>
<p><strong>Line 87,103</strong>: The Extents property will contain all related filenames of the VMDK file. For example, a file called MyVM.vmdk will have an extent called MyVM-flat.vmdk.</p>
<p><strong>Line 89-93</strong>: If the Delete switch is used, the script tests if the WhatIf switch was also specified. If not, the VMDK file is deleted.</p>
<p><strong>Line 109</strong>: This condition handles the case when there is an orphaned VMDK file in the root of the datastore.</p>
<p><strong>Line 110</strong>: For an orphaned VMDK in the root of the datastore, the DeleteFile method is used.</p>
<p><strong>Line 118</strong>: For a folder with orphaned VMDK files, the DeleteDatastoreFile method is used</p>
<h2>Sample runs</h2>
<p>The function will only scan shared VMFS datastores for orphaned folders and files.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Remove-OrphanedData -Datastore DS1</pre><p></p>
<p>This call will display all found folders and files and will not delete them.</p>
<p><a href="https://www.lucd.info/2011/04/25/orphaned-files-and-folders-spring-cleaning/demo2/" rel="attachment wp-att-4386"><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-4386" alt="demo2" src="https://lucd.info/wp-content/uploads/2011/04/demo2.png" width="519" height="257" srcset="https://www.lucd.info/wp-content/uploads/2011/04/demo2.png 519w, https://www.lucd.info/wp-content/uploads/2011/04/demo2-300x148.png 300w" sizes="(max-width: 519px) 100vw, 519px" /></a></p>
<p>When you add the <strong>Delete</strong> switch, the function will return the same information, but the folders and files will be deleted.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Remove-OrphanedData -Datastore DS1 -Delete</pre><p></p>
<p>When you combine the <strong>Delete</strong> switch with the <strong>WhatIf</strong> switch, not files or folders will be deleted. But the function will show what actions will be taken without</p>
<p><a href="https://www.lucd.info/2011/04/25/orphaned-files-and-folders-spring-cleaning/demo1-2/" rel="attachment wp-att-4385"><img decoding="async" class="alignnone size-full wp-image-4385" alt="demo1" src="https://lucd.info/wp-content/uploads/2011/04/demo1.png" width="646" height="145" srcset="https://www.lucd.info/wp-content/uploads/2011/04/demo1.png 646w, https://www.lucd.info/wp-content/uploads/2011/04/demo1-300x67.png 300w" sizes="(max-width: 646px) 100vw, 646px" /></a></p>
<p>You can specify more than one value with the Datastore parameter, the function will investigate all the datastores you specified.<br />
You can pass the datastores by name</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Remove-OrphanedData -Datastore &quot;ds1&quot;,&quot;ds2&quot;</pre><p></p>
<p>or by object</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$ds = Get-Datastore &quot;ds1&quot;
Remove-OrphanedData -Datastore $ds</pre><p></p>
<p>And the function can of course also be used in a pipeline construction</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-Datastore ds* | Remove-OrphanedData -Delete</pre><p></p>
<p><span style="background-color: #ffff00;">Make absolutely sure that you know what you are doing before using the <strong>Delete</strong> switch.</span></p>
<p><span style="background-color: #ffff00;">Use the <strong>WhatIf</strong> switch before actually deleting anything !</span></p>
<p>Enjoy !</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2011/04/25/orphaned-files-and-folders-spring-cleaning/feed/</wfw:commentRss>
			<slash:comments>133</slash:comments>
		
		
			</item>
		<item>
		<title>Finding Thin disks</title>
		<link>https://www.lucd.info/2010/10/04/finding-thin-disks/</link>
					<comments>https://www.lucd.info/2010/10/04/finding-thin-disks/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Mon, 04 Oct 2010 00:53:02 +0000</pubDate>
				<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Thin Provisioning]]></category>
		<category><![CDATA[disk]]></category>
		<category><![CDATA[Thin]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=2672</guid>

					<description><![CDATA[Last Sunday there appeared an interesting thread in the VMTN PowerCLI Community where [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Last Sunday there appeared an interesting <a href="https://communities.vmware.com/thread/287288?tstart=0" target="_blank">thread</a> in the <a href="https://communities.vmware.com/community/vmtn/vsphere/automationtools/powercli?view=discussions" target="_blank">VMTN PowerCLI Community</a> where one the questions was how to find all &#8216;<strong>Thin</strong>&#8216; virtual disks without passing via a virtual machine. The reason for this question was that most of the user&#8217;s virtual machines in Lab Manager are not registered on the vCenter Server.</p>
<p>My first idea was to use &#8216;<em>Get-Datastore | Get-Harddisk</em>&#8216; and then use the Extensiondata property to query the <strong>thinProvisioned</strong> property. Something like Arne did in his <a href="https://ict-freak.nl/2009/10/09/powercli-virtual-machine-disk-vmdk-info/" target="_blank">PowerCLI: Virtual Machine Disk (VMDK) info</a> post. But that, unfortunately, doesn&#8217;t work since the <strong>Extensiondata</strong> property is <strong>$null</strong> in this case.</p>
<p><span id="more-2672"></span>The alternative I came up with in the end, is to use the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.host.DatastoreBrowser.html#searchSubFolders" target="_blank">SearchDatastoreSubFolders_Task</a> method that is available on the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.host.DatastoreBrowser.html" target="_blank">HostDatastoreBrowser</a> object.<br />
With this method you use a <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.host.DatastoreBrowser.SearchSpec.html" target="_blank">HostDatastoreBrowserSearchSpec</a> object to specify which types of files must be searched for.<br />
And since API 4.0, the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.host.DatastoreBrowser.VmDiskQuery.Filter.html" target="_blank">VmDiskFileQueryFilter</a> allows you to specify if you only want &#8216;Thin&#8217; virtual disks to be returned.</p>
<p><span style="background-color: #ffff00;">Note</span> that the following script requires <strong>PowerCLI 4.1</strong> !</p>
<p><span style="background-color: #ffff00;">Update October 4th 2010</span>: added a test to check if the script is running in at least <strong>PowerCLI 4.1</strong></p>
<h2>The script</h2>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">function Find-ThinDisk {
&lt;#
.SYNOPSIS
  Returns all virtual disks that are Thin
.DESCRIPTION
  The function returns all the Thin disks it finds on the datastore(s)
	passed to the function
.NOTES
  Authors:	Luc Dekens
.PARAMETER Datastore
  On or more datastore objects returned by Get-Datastore
.PARAMETER Thin
  If this switch is $true, only Thin virtual disks are returned. If the
switch is set to $false, all non-Thin virtual disks are returned.
.EXAMPLE
  PS&gt; Get-Datastore | Find-ThinDisk
.EXAMPLE
  PS&gt; Find-ThinDisk -Datastore (Get-Datastore -Name &quot;DS*&quot;)
#&gt;

	param(
	[parameter(valuefrompipeline = $true, mandatory = $true,
	HelpMessage = &quot;Enter a datastore&quot;)]
	[VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.DatastoreImpl[]]$Datastore,
	[switch]$Thin = $true
	)

	begin{
		if((Get-PowerCLIVersion).Build -lt 264274){
			Write-Error &quot;The script requires at least PowerCLI 4.1 !&quot;
			exit
		}

		$searchspec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
		$query = New-Object VMware.Vim.VmDiskFileQuery
		$query.Details = New-Object VMware.Vim.VmDiskFileQueryFlags
		$query.Details.capacityKb = $true
		$query.Details.controllerType = $true
		$query.Details.diskExtents = $true
		$query.Details.diskType = $true
		$query.Details.hardwareVersion = $true
		$query.Details.thin = $true
		$query.Filter = New-Object VMware.Vim.VmDiskFileQueryFilter
		$query.Filter.Thin = $Thin
		$searchspec.Query += $query
	}

	process{
		$Datastore | %{
			$dsBrowser = Get-View $_.Extensiondata.Browser
			$datastorepath = &quot;[&quot; + $_.Name + &quot;]&quot;

			$taskMoRef = $dsBrowser.SearchDatastoreSubFolders_Task($datastorePath, $searchSpec)

			$task = Get-View $taskMoRef
			while (&quot;running&quot;,&quot;queued&quot; -contains $task.Info.State){
				$task.UpdateViewData(&quot;Info.State&quot;)
			}
			$task.UpdateViewData(&quot;Info.Result&quot;)
			if($task.Info.Result){
				foreach ($folder in $task.Info.Result){
					if($folder.File){
						foreach($file in $folder.File){
							$record = &quot;&quot; | Select DSName,Path,VmdkName
							$record.DSName = $_.Name
							$record.Path = $folder.FolderPath
							$record.VmdkName = $file.Path
							$record
						}
					}
				}
			}
		}
	}
}</pre><p></p>
<h4>Annotations</h4>
<p><strong>Line 29-32</strong>: The script requires at least PowerCLI 4.1. To avoid errors when running with an older PowerCLI versions, I added this test.</p>
<p><strong>Line 34-45</strong>: The <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.host.DatastoreBrowser.SearchSpec.html" target="_blank">HostDatastoreBrowserSearchSpec</a> object is constructed in the Begin block since it stays constant for all objects that come in through the pipeline.</p>
<p><strong>Line 44</strong>: This is where the value of the -Switch parameter is used.</p>
<p><strong>Line 49</strong>: If the function is not used in a pipeline, the $Datastore variable can be an array. To solve this and to avoid an If-statement to discover this situation, I used the ForEach-Object cmdlet.</p>
<p><strong>Line 60,62</strong>: The ForEach statement will execute the code block at least once, even if the collection is $null. These 2 If-statements avoid this situation. A $null value in a condition is interpreted as $false.</p>
<h2>Samples</h2>
<p>The Find-ThinDisk function can be called in several ways.</p>
<p>As a standalone cmdlet</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Find-ThinDisk -Datastore (Get-Datastore DS*)</pre><p></p>
<p>And it can be used in a pipeline as follows</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-Datastore -Name &quot;DS*&quot; | Find-ThinDisk</pre><p></p>
<p>In both invocation methods the result will be the same and will look something like this.</p>
<p><a href="https://lucd.info/wp-content/uploads/2010/10/FindThin.png"><img decoding="async" class="alignnone size-full wp-image-2677" title="FindThin" src="https://lucd.info/wp-content/uploads/2010/10/FindThin.png" alt="" width="742" height="151" srcset="https://www.lucd.info/wp-content/uploads/2010/10/FindThin.png 928w, https://www.lucd.info/wp-content/uploads/2010/10/FindThin-300x61.png 300w" sizes="(max-width: 742px) 100vw, 742px" /></a></p>
<p>A handy feature is the <strong>-Thin</strong> parameter. This switch is by default $true and hence the function will return only Thin virtual disks. But you can set the switch to $false and then the function will return all non-Thin disks.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">Get-Datastore -Name DS1 | Find-ThinDisk -Thin:$false</pre><p></p>
<p>Needless to say that the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.host.DatastoreBrowser.SearchSpec.html" target="_blank">HostDatastoreBrowserSearchSpec</a> object contains many other interesting properties that can be used to launch other queries against a datastore.</p>
<p>Let me know if there is a specific query you are looking for ?</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2010/10/04/finding-thin-disks/feed/</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
			</item>
		<item>
		<title>yadr &#8211; A vdisk reporter</title>
		<link>https://www.lucd.info/2010/03/23/yadr-a-vdisk-reporter/</link>
					<comments>https://www.lucd.info/2010/03/23/yadr-a-vdisk-reporter/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Tue, 23 Mar 2010 19:35:14 +0000</pubDate>
				<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[RDM]]></category>
		<category><![CDATA[report]]></category>
		<category><![CDATA[Thin Provisioning]]></category>
		<category><![CDATA[vSphere]]></category>
		<category><![CDATA[disk]]></category>
		<category><![CDATA[virtual]]></category>
		<guid isPermaLink="false">http://lucd.info/?p=1935</guid>

					<description><![CDATA[I know there are already numerous scripts to report on virtual hard disks [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>I know there are already numerous scripts to report on virtual hard disks and most of them without a doubt much better then what I came up with for this post.</p>
<p>The reason I started with this script was a question in the <a href="https://communities.vmware.com/community/vmtn/vsphere/automationtools/powercli?view=overview" target="_blank">PowerCLI</a> Community from <strong>Alan</strong> in his <a href="https://communities.vmware.com/message/1491897#1491897" target="_blank">Thin Provisioned Disks</a> post. He wanted to know if you could get the <strong>provisioned</strong> and the <strong>allocated</strong> disk size for a thin provisioned virtual disk.</p>
<p><span id="more-1935"></span>From past experience I knew that the answer was to be found in the <strong>layoutEx</strong> property of the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.VirtualMachine.html" target="_blank">VirtualMachine</a> object.</p>
<p>But while I was writing the script I saw opportunities to expand the script to make the produced report the &#8220;mother of all virtual disk reports&#8221;. Or at least an attempt to include much more information in the produced report <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2>The layoutEx property</h2>
<p>The layoutEx property, that came with <a href="https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/index.html" target="_blank">API 4.0</a>, contains a wealth of information concerning the virtual disks connected to your guests.</p>
<p>Since you can find back the mapping to all individual files that constitute a virtual disk,  it is the ideal property to show the actual storage used by each virtual disk and the snapshots.</p>
<p>The following graphic shows how the three major properties in the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.FileLayoutEx.html" target="_blank">VirtualMachineFileLayoutEx</a> object are organised. The values in the graphic are from a virtual machine, with one virtual hard disk, and two snapshots.</p>
<p><a href="https://lucd.info/wp-content/uploads/2010/03/layoutEx2.png"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-1950" title="layoutEx" src="https://lucd.info/wp-content/uploads/2010/03/layoutEx2.png" alt="" width="680" height="308" srcset="https://www.lucd.info/wp-content/uploads/2010/03/layoutEx2.png 945w, https://www.lucd.info/wp-content/uploads/2010/03/layoutEx2-300x135.png 300w" sizes="auto, (max-width: 680px) 100vw, 680px" /></a></p>
<h2>Handling of snapshots</h2>
<p id="firstHeading">I assume that most of you know what happens in the background with a virtual disk when you take a snapshot of a virtual machine. The following graphic is small <a href="https://en.wikipedia.org/wiki/Aide-m%C3%A9moire" target="_blank">aide-mémoire</a> for understanding the basics.</p>
<p><a href="https://lucd.info/wp-content/uploads/2010/03/snapshot-layout.png"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-1944" title="snapshot-layout" src="https://lucd.info/wp-content/uploads/2010/03/snapshot-layout.png" alt="" width="690" height="390" srcset="https://www.lucd.info/wp-content/uploads/2010/03/snapshot-layout.png 1077w, https://www.lucd.info/wp-content/uploads/2010/03/snapshot-layout-300x169.png 300w, https://www.lucd.info/wp-content/uploads/2010/03/snapshot-layout-1024x579.png 1024w" sizes="auto, (max-width: 690px) 100vw, 690px" /></a></p>
<p>If you look closely at the Snapshot table (first graphic) you will notice that an entry points to the files, or  chain of files, before the snapshot was taken.</p>
<p>For example, Snapshot 2  results in the FileKey sequence 0, 1, 2 and 3. While the actual new  files that are created for Snapshot 2, are those with FileKey 4 and 5.</p>
<p>Since I wanted to know how much datastore space Snapshot 2 was  taking, I decided to let the script look for the files linked to  Snapshot 2. In this case that would be the files with FileKey 4 and 5.</p>
<h2>The script</h2>
<p>There were a few problems to be solved:</p>
<ol>
<li>Not all virtual machines have the same number of snapshots. If the data needs to be exported to a CSV file this poses a problem with the Export-Csv cmdlet. The solution I took:
<ul>
<li>For all guests determine the number of snapshots, if any, they have and take the maximum of these numbers.</li>
<li>Define a new object, I called it <strong>DiskInfo</strong> in the script, and define a sufficient number of snapshot-related properties (Name and Size) so that the guest with the highest number of snapshots can be represented.</li>
<li>Define the new object in <strong>C#</strong> and use the <strong>Add-Type</strong> cmdlet to create the new type.</li>
</ul>
</li>
<li>Like I mentioned earlier, I wanted to display the actual datastore space a specific snapshot takes. For that I had to find the FileKey values that are<strong> not</strong> in the <strong>Snapshot</strong> table. My reasoning for this was that I wanted to know how much datastore space would become free when a specific snapshot is removed.</li>
</ol>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$DiskInfoDef = @&quot;
public struct DiskInfo {
	public string VMname;
	public string Label;
	public string Path;
	public bool Thin;
	public bool RDM;
	public string Used;
	public string CapacityMB;
	public string AllocatedMB;
&quot;@

$vms = Get-View -ViewType VirtualMachine
$maxSnapshot = ($vms | %{$_.LayoutEx.Snapshot.Count} | Measure-Object -Maximum).Maximum
1..$maxSnapshot | %{
	$DiskInfoDef += (&quot;`n`tpublic string Snap&quot; + $_ + &quot;MB;&quot;)
	$DiskInfoDef += (&quot;`n`tpublic string Snap&quot; + $_ + &quot;Name;&quot;)
}
$DiskInfoDef += &quot;`n}&quot;

Add-Type -Language CsharpVersion3 -TypeDefinition $DiskInfoDef

$snapHash = @{}

filter Get-SnapHash{
	$snapHash[$_.Snapshot.Value] = $_.Name
	if($_.ChildSnapshotList){
		$_.ChildSnapShotList | Get-SnapHash
	}
}

$vms | %{
	$vm = $_

	if($vm.Snapshot){
		$vm.Snapshot.RootSnapshotList | Get-SnapHash
	}

	$_.Config.Hardware.Device | where {$_.DeviceInfo.Label -like &quot;Hard disk *&quot;} | %{
		$hd = $_
		$vm.LayoutEx.Disk | where {$_.Key -eq $hd.Key} | %{
			$diskFiles = $_.Chain | %{$_.FileKey} | %{$_}
		}

		$diskAllocated = 0
		$vmdkName = $vm.LayoutEx.File[$diskFiles[0]].Name

		$vm.LayoutEx.File | where {$diskFiles -contains $_.Key} | %{
			$diskAllocated += $_.Size
		}

		$used = &quot;na&quot;
		if($hd.Backing.ThinProvisioned){
			$used = (&quot;{0:P1}&quot; -f ($diskAllocated / 1KB/$hd.CapacityInKB))
		}

		$diskInfo = New-Object DiskInfo
		$diskInfo.VMname = $vm.Name
		$diskInfo.Label = $hd.DeviceInfo.Label
		$diskInfo.Path = $vmdkName
		$diskInfo.Thin = $hd.Backing.ThinProvisioned
		$diskInfo.RDM = $hd.Backing.GetType().Name.Contains(&quot;RawDisk&quot;)
		$diskInfo.Used = $used
		$diskInfo.CapacityMB = (&quot;{0:N1}&quot; -f ($hd.CapacityInKB/1KB))
		$diskInfo.AllocatedMB = (&quot;{0:N1}&quot; -f ($diskAllocated / 1MB))
		$snapNr = 1
		if($vm.Snapshot){
			$vm.LayoutEx.Snapshot | %{
				$_.Disk | where {$_.Key -eq $hd.Key} | %{
					$prevFiles = $_.Chain | %{$_.FileKey} | %{$_}
				}
				$vm.LayoutEx.Disk | where {$_.Key -eq $hd.Key} | %{
					foreach($chain in $_.Chain){
						if($prevFiles -notcontains $chain.FileKey[0] -and $prevFiles -notcontains $chain.FileKey[1]){
							break
						}
					}
				}
				$snapFiles = $chain.FileKey | %{$_}
				$snapSize = &quot;&quot;
				$snapName = &quot;&quot;
				if($snapFiles){
					$snapAllocated = 0
					$vm.LayoutEx.File | where {$snapFiles -contains $_.Key} | %{
						$snapAllocated += $_.Size
					}
					$snapSize = (&quot;{0:N1}&quot; -f ($snapAllocated / 1MB))
					$snapName = $snapHash[$_.Key.Value]
				}
				$diskInfo.(&quot;Snap&quot; + $snapNr + &quot;MB&quot;) = $snapSize
				$diskInfo.(&quot;Snap&quot; + $snapNr + &quot;Name&quot;) = $snapName
				$snapNr++
			}
		}
		$diskInfo
	}
} | Export-Csv &quot;C:\Yadr-report.csv&quot; -UseCulture -NoTypeInformation</pre><p></p>
<h4>Annotations</h4>
<p><strong>Line 1-11</strong>: The &#8220;static&#8221; part of the C# code that defines the DiskInfo type.</p>
<p><strong>Line 13</strong>: Get all the virtual machines. I used the Get-View cmdlet for speed and also because the script only needs properties from the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.VirtualMachine.html" target="_blank">VirtualMachine</a> object.</p>
<p><strong>Line 14</strong>: Determine the highest number of snapshot present on any guest.</p>
<p><strong>Line 15-18</strong>: The &#8220;dynamic&#8221; part of the C# code. For each snapshot this adds two properties, Snap<em>i</em>Name and Snap<em>i</em>MB,</p>
<p><strong>Line 19</strong>: The &#8220;newline&#8221; character is not really. Just gives a better view when you dump the content of the $DiskInfoDef variable.</p>
<p><strong>Line 21</strong>: The new type is created. Note that when you change anything in the definition, you will most probably have to restart your PS environment, otherwise you will get a message that the type already exists. There is, afaik, no way to get rid of a type definition in the .Net environment.</p>
<p><strong>Line 23-30</strong>: This filter will recursively run through the snapshot tree and will create a hash table. The table will later be used to find the snapshot name.</p>
<p><strong>Line 32-98</strong>: The main loop.</p>
<p><strong>Line 35-37</strong>: Create a hash table from the snapshot tree.</p>
<p><strong>Line 39</strong>: Loop through all the virtual disks connected to this virtual machine.</p>
<p><strong>Line 41-43</strong>: Get all the FileKeys for this specific virtual disk</p>
<p><strong>Line 46</strong>: Get the name of the first disk file.</p>
<p><strong>Line 48-50</strong>: Summarise the size of each file that comprises the virtual hard disk.</p>
<p><strong>Line 52-55</strong>: Check if the virtual hard disk is Thin Provisioned. If it is, calculate the percentage of storage that is actually allocated on the datastore.</p>
<p><strong>Line 57</strong>: Create a new object based on the DiskInfo type.</p>
<p><strong>Line 58-65</strong>: Start filling in the values for the properties of the DiskInfo object.</p>
<p><strong>Line 66-94</strong>: Handle the snapshots (if present) for this virtual hard disk.</p>
<p><strong>Line 67</strong>: Start by checking if there is at least one snapshot for this virtual hard disk.</p>
<p><strong>Line 68</strong>: Loop through all the snapshots.</p>
<p><strong>Line 69-71</strong>: Get the FileKey for all the files for this virtual hard disk before the snapshot was taken. Store these FileKey values in the $prevFiles array.</p>
<p><strong>Line 72-78</strong>: Find the first pair of FileKeys in the chain that is not in the $prevFiles array. These are the files that constitute the snapshot.</p>
<p><strong>Line 79</strong>: Store the pair of FileKeys in the array $snapFiles.</p>
<p><strong>Line 82-89</strong>: Calculate the total size of the snapshot files and get the name of the snapshot from the hash table created in the beginning of the main loop.</p>
<p><strong>Line 90-91</strong>: Store the values in the correct properties in the DiskInfo object.</p>
<p><strong>Line 97</strong>: Export the array to a CSV file.</p>
<h2>Result</h2>
<p>The following screenshot shows a sample run of the script.</p>
<p><a href="https://lucd.info/wp-content/uploads/2010/03/yadr-sample1.png"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-1965" title="yadr-sample" src="https://lucd.info/wp-content/uploads/2010/03/yadr-sample1.png" alt="" width="860" height="307" srcset="https://www.lucd.info/wp-content/uploads/2010/03/yadr-sample1.png 1229w, https://www.lucd.info/wp-content/uploads/2010/03/yadr-sample1-300x106.png 300w, https://www.lucd.info/wp-content/uploads/2010/03/yadr-sample1-1024x364.png 1024w" sizes="auto, (max-width: 860px) 100vw, 860px" /></a></p>
<p>There are some interesting results in this sample run.</p>
<ul>
<li>The maximum snapshots any guest has is two.</li>
<li>Server1 (row 2-5) has three virtual hard disks. The first one is thin provisioned and uses currently 47.7% of it&#8217;s capacity.</li>
<li>The other two hard disks of Server1 are RDM disks. Notice that the AllocatedMB column says &#8220;zero&#8221; because there is not really a point in talking about allocated space for RDM disks.</li>
<li>The guest in row 8 has two snapshots.</li>
</ul>
<p>And last but not least, I suspect there is a <span style="color: #ff0000;"><strong>bug</strong></span> in the <a href="https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.FileLayoutEx.html" target="_blank">VirtualMachineFileLayoutEx</a> object, more specifically in the <strong>Size</strong> property of <strong>some</strong> entries in the <strong>Files</strong> array.</p>
<p>Have a look at row 20, this disk is <strong>not</strong> a thin provisioned disk but the <strong>AllocatedMB</strong> value is <span style="text-decoration: underline;">less</span> then the <strong>CapacityMB</strong> value. I would have expected the same value in those two columns.</p>
<p>When I investigate from the console with a simple &#8220;<strong>ls -l</strong>&#8221; I see the correct value.</p>
<p>For one reason or another, there are some (very few) virtual disks that have an incorrect size values in the Files table. I couldn&#8217;t find out in which cases this seems to happen.</p>
<p><span style="font-size: x-small;">PS: <strong>yadr</strong> stands for <em>yet another disk reporter</em></span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2010/03/23/yadr-a-vdisk-reporter/feed/</wfw:commentRss>
			<slash:comments>54</slash:comments>
		
		
			</item>
	</channel>
</rss>
