<?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>PeetersOnline &#187; snapshots</title>
	<atom:link href="http://www.peetersonline.nl/index.php/tag/snapshots/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.peetersonline.nl</link>
	<description>Using Powershell for (virtually) everything!</description>
	<lastBuildDate>Thu, 27 May 2010 08:01:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Set VMware Snapshot Location with Powershell</title>
		<link>http://www.peetersonline.nl/index.php/vmware/set-vmware-snapshot-location-with-powershell/</link>
		<comments>http://www.peetersonline.nl/index.php/vmware/set-vmware-snapshot-location-with-powershell/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 13:09:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PowerCLI 4.0]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[VI Toolkit 1.5]]></category>
		<category><![CDATA[VMware]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[snapshots]]></category>

		<guid isPermaLink="false">http://www.peetersonline.nl/?p=411</guid>
		<description><![CDATA[Snapshots are m*th3rfcukers. If you&#8217;re not careful, they will mass-murder your vms. Yet they allow you to time-travel! You want to use them, but how to prevent a massacre? Here&#8217;s how: relocate the delta files. When you create a snapshot, the current state of the vm is preserved by leaving the disk files alone. All changes [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Snapshots are m*th3rfcukers. If you&#8217;re not careful, they will mass-murder your vms. Yet they allow you to time-travel! You want to use them, but how to prevent a massacre? Here&#8217;s how: relocate the delta files.</p>
<p>When you create a snapshot, the current state of the vm is preserved by leaving the disk files alone. All changes since the moment of creating the snapshot are written to delta files. The delta files are stored in the vm&#8217;s working directory. The working directory is &#8211; by default &#8211; the location where the vmx and other config files reside. If that datastore runs out of free space &#8211; especially if it also contains disk files &#8211; you&#8217;re in a bit of a kerfuffle. Vms not booting or being frozen as if they stared into Medusa&#8217;s reptoid eyes.</p>
<p>So you can do two things: reserve overhead in your datastores and stay afraid some overactive snapshot might destroy your environment, or set the working directory of your vms to some big-ass datastore you don&#8217;t use for anything else and let the snapshots enjoy themselves. If they fill up the datastore, they only kill all vms with snapshots, not the rest.</p>
<p>But how, you ask? You can edit the vmx files of you vms manually - which requires your vms to be powered down &#8211; and add the line: workingDir = &#8220;/vmfs/volumes/&lt;insanely long guid you need to somehow find&gt;/&#8221;</p>
<p>Or, you run my script and change the working location on the fly:</p>
<p><span id="more-411"></span></p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Description:</span>
<span style="color: #008000;"># Set's the Working Location for a VM. This location will hold the vswap and snapshot delta files.</span>
&nbsp;
<span style="color: #008000;"># Usage:</span>
<span style="color: #008000;"># First, connect to a virtual server server (Connect-VIServer)</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;"># Set snapshot location for one vm (folder must already exist on datastore):</span>
<span style="color: #008000;"># Set-VMSnapshotLocation -vm VM001 -Location &quot;[BigAssDatastore] Snapshots&quot;</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;"># For all vms at once (no folder specified will put all in the root of the datastore):</span>
<span style="color: #008000;"># Get-VM | ForEach-Object {Set-VMSnapshotLocation -vm $_ -Location &quot;[BigAssDatastore]&quot;}</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;"># Reset the location to the original location (with the vmx file):</span>
<span style="color: #008000;"># Set-VMSnapshotLocation -vm VM001 -Reset</span>
&nbsp;
<span style="color: #0000FF;">Function</span> Set<span style="color: pink;">-</span>VMSnapshotLocation
	<span style="color: #000000;">&#123;</span>
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$vm</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Location</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">switch</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Reset</span><span style="color: pink;">=</span><span style="color: #800080;">$False</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #008000;">#Region Input Validation</span>
	<span style="color: #008000;"># Check vm parameter</span>
	<span style="color: #0000FF;">If</span> <span style="color: #000000;">&#40;</span><span style="color: pink;">!</span><span style="color: #800080;">$vm</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">Throw</span> <span style="color: #800000;">&quot;No VM specified.&quot;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #008000;"># Check Location parameter.</span>
	<span style="color: #008000;"># Can be empty when using Reset, can be &quot;[datastore]&quot; or can be &quot;[datastore] folder&quot; (folder must exist).</span>
	<span style="color: #0000FF;">If</span> <span style="color: #000000;">&#40;</span><span style="color: pink;">!</span><span style="color: #800080;">$Reset</span> <span style="color: #FF0000;">-and</span> <span style="color: #800080;">$Location</span> <span style="color: #FF0000;">-notmatch</span> <span style="color: #800000;">&quot;\[\S+\](\s\w+)?&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">Throw</span> <span style="color: #800000;">&quot;Invalid location specified. Syntax: [Datastore] Folder&quot;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #008000;"># Specifying Reset will set Location to empty, which causes the original location to be used (same location as vmx file).</span>
	<span style="color: #0000FF;">If</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Reset</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$Location</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #008000;"># Make sure to get a vm object to work with.</span>
	<span style="color: #008000;"># This allows both using a vm name when targeting one vm (Set-VMSnapshotLocation -vm VM001 -Reset);</span>
	<span style="color: #008000;"># AND piping a collection of vm's (Get-VM | ForEach (Set-VMSnapshotLocation -vm $_ -Location &quot;[BigAssDatastore] Snapshots&quot; ).</span>
	<span style="color: #0000FF;">If</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$vm</span>.GetType<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.Name <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;String&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$oVM</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>VM <span style="color: #800080;">$vm</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">ElseIf</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$vm</span>.GetType<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.Name <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;VirtualMachineImpl&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$oVM</span> <span style="color: pink;">=</span> <span style="color: #800080;">$vm</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">Else</span>
		<span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">Throw</span> <span style="color: #800000;">&quot;Parameter vm is of an invalid type.&quot;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #008000;">#EndRegion InputValidation</span>
&nbsp;
	<span style="color: #008000;">#Region Main</span>
	<span style="color: #008000;"># Get advanced properties of vm</span>
	<span style="color: #800080;">$vmView</span> <span style="color: pink;">=</span> <span style="color: #800080;">$oVM</span> <span style="color: pink;">|</span> Get<span style="color: pink;">-</span>View
	<span style="color: #008000;"># Create object containing new properties</span>
	<span style="color: #800080;">$vmConfigSpec</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> VMware.Vim.VirtualMachineConfigSpec
	<span style="color: #800080;">$vmConfigSpec</span>.Files <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> VMware.Vim.VirtualMachineFileInfo
	<span style="color: #800080;">$vmConfigSpec</span>.Files.SnapshotDirectory <span style="color: pink;">=</span> <span style="color: #800080;">$Location</span>
	<span style="color: #008000;"># Reconfigure VM using new properties</span>
	<span style="color: #800080;">$vmView</span>.ReconfigVM<span style="color: #000000;">&#40;</span><span style="color: #800080;">$vmConfigSpec</span><span style="color: #000000;">&#41;</span>
	<span style="color: #008000;">#EndRegion Main</span>
	<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You can download it here: <a href="http://www.peetersonline.nl/wp-content/Set-VMSnapshotLocation.txt">Set-VMSnapshotLocation (rename to .ps1)</a></p>
<p>I tried hard as I could, but I could not get my vm to die. Even running the script, creating a snapshot, running the script to move the working location again and deleting the snapshot did not prove a problem. However, I urge you to be careful with it. I&#8217;m not responsable for anything you do with it.</p>
<p>Tiny glitch: the VI Client shows the wrong datastore location when a vm has a snapshot in a modified workingdir.</p>
<p>Enjoy.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.peetersonline.nl/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.peetersonline.nl/index.php/vmware/set-vmware-snapshot-location-with-powershell/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Add Snapshot Information to the VI Client using Powershell</title>
		<link>http://www.peetersonline.nl/index.php/vmware/add-snapshot-information-to-the-vi-client-using-powershell/</link>
		<comments>http://www.peetersonline.nl/index.php/vmware/add-snapshot-information-to-the-vi-client-using-powershell/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 10:02:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[VMware]]></category>
		<category><![CDATA[Custom Fields]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[snapshots]]></category>
		<category><![CDATA[VI Toolkit]]></category>

		<guid isPermaLink="false">http://www.peetersonline.nl/?p=147</guid>
		<description><![CDATA[Although I spend quite some time in the Powershell Command Line Interface, the main tool for managing the Virtual Infrastructure remains the VI Client. So wouldn&#8217;t it be great if we could somehow show the results of our Powershell VI Toolkit scripts inside the VI Client? Well, we can! Let&#8217;s take a closer look at [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Although I spend quite some time in the <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target='_blank'>Powershell</a> Command Line Interface, the main tool for managing the Virtual Infrastructure remains the VI Client. So wouldn&#8217;t it be great if we could somehow show the results of our <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target='_blank'>Powershell</a> <a href="http://www.vmware.com/sdk/vitk_win/index.html" target='_blank'>VI Toolkit</a> scripts <em>inside</em> the VI Client?<br />
Well, we can! Let&#8217;s take a closer look at <em>Custom Fields / Custom Attributes</em>.<br />
If you select either a VMHost (ESX Server) or a VM in the VI Client and open the Summary tab, you will see the Annotations section in the bottom left. When you click Edit, I&#8217;m sure you have used the Notes section to enter Descriptions. But have you ever used the Attributes section? Here you can manually add and remove custom attributes and their values. Go ahead and create one. Then select a cluster or datacenter and click the Hosts or Virtual Machines tab. You will notice you can display your custom attribute in this table view, just like all the other properties of your VMs / Hosts. Pretty sweet!<br />
But we don&#8217;t want to add and update those fields manually, now do we? Of course not, we&#8217;ve got <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target='_blank'>Powershell</a>! Let&#8217;s see how we can automate this.<br />
We start by connecting to the Custom Fields Manager:</p>
<blockquote><p>$VCServerName = &#8220;MYVCSERVER&#8221;<br />
$VC = Connect-VIServer $VCServerName<br />
$SI = Get-View ServiceInstance<br />
$CFM = Get-View $SI.Content.CustomFieldsManager</p></blockquote>
<p>Take a look at the $CFM.Field property. It contains an array of available fields. Next step: add our own custom field.</p>
<blockquote><p># Variables<br />
$CustomFieldName = &#8220;Snapshots&#8221;<br />
$ManagedObjectType = &#8220;VirtualMachine&#8221;<br />
# Check if the custom field already exists<br />
$myCustomField = $CFM.Field | Where {$_.Name -eq $CustomFieldName}<br />
If (!$myCustomField)<br />
	{<br />
	# Create Custom Field<br />
	$FieldCopy = $CFM.Field[0]<br />
	$CFM.AddCustomFieldDef($CustomFieldName, $ManagedObjectType, $FieldCopy.FieldDefPrivileges, $FieldCopy.FieldInstancePrivileges)<br />
	}</p></blockquote>
<p>Final step: Fill the custom field with some relevant information:</p>
<blockquote><p># Fill Custom Fields<br />
$VMs = Get-VM<br />
$VMViews = $VMs | Get-View<br />
ForEach ($VMView in $VMViews)<br />
	{<br />
	$SnapshotCount = ($VMView.Snapshot.RootSnapshotList | Measure-Object).Count<br />
	If ($SnapShotCount)<br />
		{<br />
		$VMView.setCustomValue($CustomFieldName,$SnapShotCount)<br />
		}<br />
	}</p></blockquote>
<p>Now take a look at the VI Client:<br />
<img src="http://www.peetersonline.nl/wp-content/snapshots.jpg" alt="snapshots" title="snapshots" width="228" height="179" class="alignnone size-full wp-image-148" /><br />
More examples coming soon!<br />
Hugo</p>
<p>PS: Don&#8217;t forget to schedule the script to run at an interval, so the values are kept up-to-date!</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.peetersonline.nl/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.peetersonline.nl/index.php/vmware/add-snapshot-information-to-the-vi-client-using-powershell/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Powershell Oneliner #4</title>
		<link>http://www.peetersonline.nl/index.php/vmware/powershell-oneliner-4/</link>
		<comments>http://www.peetersonline.nl/index.php/vmware/powershell-oneliner-4/#comments</comments>
		<pubDate>Wed, 06 Aug 2008 07:16:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oneliners]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[VMware]]></category>
		<category><![CDATA[Oneliner]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[snapshots]]></category>
		<category><![CDATA[VI Toolkit]]></category>

		<guid isPermaLink="false">http://www.peetersonline.nl/index.php/vmware/powershell-oneliner-4/</guid>
		<description><![CDATA[Yesterday, alanrenouf asked the following question on the VMware Community VI Toolkit forums: Is there a way (preferably a one-liner) to get a list of vm&#8217;s and the number of snapshots per vm? Here&#8217;s a script that will get that info: $VC = Connect-VIServer $VCServerName $vms = Get-VM $myCol = @() ForEach ($vm in $vms) [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Yesterday, alanrenouf asked the following question on the <a href="http://www.vmware.com/" target='_blank'>VMware</a> Community <a href="http://www.vmware.com/sdk/vitk_win/index.html" target='_blank'>VI Toolkit</a> forums:</p>
<blockquote><p><em>Is there a way (preferably a one-liner) to get a list of vm&#8217;s and the number of snapshots per vm?</em></p></blockquote>
<p>Here&#8217;s a script that will get that info:</p>
<p><font color="#000080"><code>$VC = Connect-VIServer $VCServerName<br />
$vms = Get-VM<br />
$myCol = @()<br />
ForEach ($vm in $vms)<br />
{<br />
  $snapshots = Get-Snapshot -VM $vm<br />
  $myObj = "" | Select-Object VM, NumSnapshots<br />
  $myObj.VM = $vm.name<br />
  $myObj.NumSnapshots = ($snapshots | measure-object).count<br />
  $myCol += $myObj<br />
}<br />
$myCol | Where-Object{$_.NumSnapshots -gt 0} | Sort-Object VM | Format-Table -AutoSize</code><br />
</font></p>
<p>And here&#8217;s a one-liner that does the same thing:<code> </code></p>
<p><code><font color="#000080">Get-VM |<br />
  Where{(Get-SnapShot -VM $_ | Measure-Object).Count -gt 0} |<br />
  Format-Table Name, `<br />
  @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}}</font></code></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.peetersonline.nl/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.peetersonline.nl/index.php/vmware/powershell-oneliner-4/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
