<?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>ben.neise.co.uk &#187; VI Toolkit</title>
	<atom:link href="http://ben.neise.co.uk/index.php/tag/vi-toolkit/feed/" rel="self" type="application/rss+xml" />
	<link>http://ben.neise.co.uk</link>
	<description></description>
	<lastBuildDate>Wed, 27 Jul 2011 10:19:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Creating a Shortcut to a PowerShell Script</title>
		<link>http://ben.neise.co.uk/index.php/2011/07/creating-a-shortcut-to-a-powershell-script/</link>
		<comments>http://ben.neise.co.uk/index.php/2011/07/creating-a-shortcut-to-a-powershell-script/#comments</comments>
		<pubDate>Wed, 27 Jul 2011 10:09:40 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Powershell Script]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[VI Toolkit]]></category>
		<category><![CDATA[VMware]]></category>

		<guid isPermaLink="false">http://ben.neise.co.uk/?p=163</guid>
		<description><![CDATA[You've probably noticed that when you double-click on a PowerShell script, it is opened for editing rather than being run. This is useful from a security standpoint, and while administrators have no problem opening up the shell and running the script, you've probably made something useful that you want to share with users, and users always need a bit more hand-holding.]]></description>
			<content:encoded><![CDATA[<p>You&#8217;ve probably noticed that when you double-click on a PowerShell script, it is opened for editing rather than being run. This is useful from a security standpoint, and while administrators have no problem opening up the shell and running the script, you&#8217;ve probably made something useful that you want to share with users, and users always need a bit more hand-holding.</p>
<div id="attachment_166" class="wp-caption alignnone" style="width: 228px"><img class="size-medium wp-image-166" title="PowerShell shortcut properties" src="http://ben.neise.co.uk/wp-content/uploads/2009/05/psshortcut-218x300.png" alt="PowerShell shortcut properties" width="218" height="300" /><p class="wp-caption-text">PowerShell shortcut properties</p></div>
<p>Assuming that your PowerShell installation is in the default location, you need to append the path to your script in the shortcut path like so:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">%windir%\System32\WindowsPowerShell\v1.0\powershell.exe C:\Scripts\MyUsefulScript.ps1</pre></div></div>

<p>If you are wanting to run a PowerCLI (VI Toolkit) script, you also need to add the following line:-</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Add-PSSnapin</span> VMware.VimAutomation.Core</pre></div></div>

<p>&#8230;to your script, as the VMware Cmdlets are not loaded by default unless you run it from the VMware Toolkit Shortcut.</p>
]]></content:encoded>
			<wfw:commentRss>http://ben.neise.co.uk/index.php/2011/07/creating-a-shortcut-to-a-powershell-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VMware Developer CodeCentral</title>
		<link>http://ben.neise.co.uk/index.php/2009/06/vmware-developer-codecentral/</link>
		<comments>http://ben.neise.co.uk/index.php/2009/06/vmware-developer-codecentral/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 08:50:47 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Powershell Script]]></category>
		<category><![CDATA[VMware]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[VI Toolkit]]></category>
		<category><![CDATA[vSphere]]></category>

		<guid isPermaLink="false">http://ben.neise.co.uk/?p=200</guid>
		<description><![CDATA[VMware have launched a new community site for code examples, and there’s a section on VMware vSphere PowerCLI (formerly VI Toolkit). They seem to be picking the best scripts from the regular contributors to the forums, so it’s a good place to have a look for examples. They’re rated by users, and tend to be [...]]]></description>
			<content:encoded><![CDATA[<p>VMware have launched a <a href="http://communities.vmware.com/community/developer/codecentral">new community site for code examples</a>, and there’s a section on <a href="http://communities.vmware.com/community/developer/codecentral/vsphere_powercli">VMware vSphere PowerCLI</a> (formerly VI Toolkit).</p>
<p>They seem to be picking the best scripts from the regular contributors to the forums, so it’s a good place to have a look for examples. They’re rated by users, and tend to be slightly better documented than the ones posted in the forums (which tend to be appended and amended as the thread progresses).</p>
]]></content:encoded>
			<wfw:commentRss>http://ben.neise.co.uk/index.php/2009/06/vmware-developer-codecentral/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VMware PowerCLI 4.0 released</title>
		<link>http://ben.neise.co.uk/index.php/2009/05/vmware-powercli-40-released/</link>
		<comments>http://ben.neise.co.uk/index.php/2009/05/vmware-powercli-40-released/#comments</comments>
		<pubDate>Tue, 26 May 2009 13:21:14 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[VMware]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[VI Toolkit]]></category>

		<guid isPermaLink="false">http://ben.neise.co.uk/?p=178</guid>
		<description><![CDATA[VMware PowerCLI 4.0 has been released as the successor to VI Toolkit 1.5.]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-235" title="powerCliLogo" src="http://ben.neise.co.uk/wp-content/uploads/2009/05/powerCliLogo.png" alt="powerCliLogo" width="128" height="128" />I came back from a week&#8217;s holiday this morning to find that VMware PowerCLI 4.0 has been released as the successor to VI Toolkit 1.5. The jump from 1.5 to 4.0 is for <a href="http://blogs.vmware.com/vipowershell/2009/05/powercli-is-official-whats-new.html">version number consolidation, rather than being than indicitive of major changes</a>.</p>
<p>They have however fixed one of the bugs that&#8217;s been annoying me, which is the <a href="http://communities.vmware.com/thread/194369?tstart=0">inability to change drives to non-persistent</a>, so I&#8217;ll need to revisit some of my old scripts from v1.0 and check that they still work.</p>
<p>You can download it from the <a href="http://communities.vmware.com/community/developer/windows_toolkit">community page</a>. I&#8217;ll follow up with more information when I get a chance to investigate it fully.</p>
]]></content:encoded>
			<wfw:commentRss>http://ben.neise.co.uk/index.php/2009/05/vmware-powercli-40-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add Drive Persistence State to the VI Client using Powershell</title>
		<link>http://ben.neise.co.uk/index.php/2009/05/add-drive-persistence-state-to-the-vi-client-using-powershell/</link>
		<comments>http://ben.neise.co.uk/index.php/2009/05/add-drive-persistence-state-to-the-vi-client-using-powershell/#comments</comments>
		<pubDate>Tue, 05 May 2009 12:02:40 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Powershell Script]]></category>
		<category><![CDATA[VMware]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[VI Toolkit]]></category>

		<guid isPermaLink="false">http://ben.neise.co.uk/?p=131</guid>
		<description><![CDATA[Using PowerShell and the VI Toolkit to add an easily visible custom attribute to VIC to show whether a guests hard drives are persistent or non persistent.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve further adapted <a href="http://www.peetersonline.nl/index.php/vmware/add-snapshot-information-to-the-vi-client-using-powershell/">Hugo&#8217;s script</a> to add a custom attribute which shows the drive persistence state(s) when the script was run.</p>
<p>The script also adds drive state information for templates as well as VM objects.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Add drive persistence as a custom attribute for VMs and Templates</span>
&nbsp;
<span style="color: #800080;">$VCServerName</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;MyVCServer&quot;</span>
<span style="color: #800080;">$VC</span> <span style="color: pink;">=</span> Connect<span style="color: pink;">-</span>VIServer <span style="color: #800080;">$VCServerName</span>
<span style="color: #800080;">$SI</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>View ServiceInstance
<span style="color: #800080;">$CFM</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>View <span style="color: #800080;">$SI</span>.Content.CustomFieldsManager
&nbsp;
<span style="color: #008000;"># Variables</span>
<span style="color: #800080;">$CustomFieldName</span> <span style="color: pink;">=</span> “HD Persistence”
<span style="color: #800080;">$ManagedObjectType</span> <span style="color: pink;">=</span> “VirtualMachine”
&nbsp;
<span style="color: #008000;"># Check if the custom field already exists</span>
<span style="color: #800080;">$myCustomField</span> <span style="color: pink;">=</span> <span style="color: #800080;">$CFM</span>.Field <span style="color: pink;">|</span> <span style="color: #0000FF;">Where</span> <span style="color: #000000;">&#123;</span><span style="color: #000080;">$_</span>.Name <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$CustomFieldName</span><span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">If</span> <span style="color: #000000;">&#40;</span><span style="color: pink;">!</span><span style="color: #800080;">$myCustomField</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># Create Custom Field</span>
	<span style="color: #800080;">$FieldCopy</span> <span style="color: pink;">=</span> <span style="color: #800080;">$CFM</span>.Field<span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span>
	<span style="color: #800080;">$CFM</span>.AddCustomFieldDef<span style="color: #000000;">&#40;</span><span style="color: #800080;">$CustomFieldName</span><span style="color: pink;">,</span> <span style="color: #800080;">$ManagedObjectType</span><span style="color: pink;">,</span> <span style="color: #800080;">$FieldCopy</span>.FieldDefPrivileges<span style="color: pink;">,</span> <span style="color: #800080;">$FieldCopy</span>.FieldInstancePrivileges<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Get the machine objects</span>
<span style="color: #800080;">$objVMs</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span>Get<span style="color: pink;">-</span>VM<span style="color: #000000;">&#41;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#40;</span>Get<span style="color: pink;">-</span>Template<span style="color: #000000;">&#41;</span>
<span style="color: #008000;"># Loop through each of the machine objects</span>
<span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$objVM</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$objVMs</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$strPersistence</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span>
	<span style="color: #800080;">$objHardDisks</span> <span style="color: pink;">=</span> <span style="color: #800080;">$objVM</span> <span style="color: pink;">|</span> Get<span style="color: pink;">-</span>HardDisk
	<span style="color: #008000;"># Count the number of hard drives</span>
	<span style="color: #800080;">$intHardDisks</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$objHardDisks</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Measure-Object</span><span style="color: #000000;">&#41;</span>.count
	<span style="color: #008000;"># Loop through each of the hard disks</span>
	<span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$objHardDisk</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$objHardDisks</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #008000;"># Replace default persisstence states with initials for brevity</span>
		<span style="color: #0000FF;">Switch</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$objHardDisk</span>.Persistence<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			Persistent <span style="color: #000000;">&#123;</span>
				<span style="color: #800080;">$strPersistenceInitial</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;P&quot;</span>
			<span style="color: #000000;">&#125;</span>
			IndependentPersistent <span style="color: #000000;">&#123;</span>
				<span style="color: #800080;">$strPersistenceInitial</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;IP&quot;</span>
			<span style="color: #000000;">&#125;</span>
			IndependentNonPersistent <span style="color: #000000;">&#123;</span>
				<span style="color: #800080;">$strPersistenceInitial</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;INP&quot;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #008000;"># Concatenate the initial onto the persistence string</span>
		<span style="color: #800080;">$strPersistence</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$strPersistence&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$strPersistenceInitial</span>
		<span style="color: #008000;"># If there are more hard drives to add</span>
		<span style="color: #0000FF;">If</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$intHardDisks</span> <span style="color: #FF0000;">-gt</span> <span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #008000;"># Append a comma and a space (there may be a more elegant way of doing this)</span>
			<span style="color: #800080;">$strPersistence</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$strPersistence&quot;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;, &quot;</span>
			<span style="color: #008000;"># Count down the number of hard drives</span>
			<span style="color: #800080;">$intHardDisks</span> <span style="color: pink;">-=</span> <span style="color: #804000;">1</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #008000;"># Add the $strPersistence to custom attribute $CustomFieldName (HD Persistence)</span>
	<span style="color: #0000FF;">If</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$strPersistence</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$VMView</span> <span style="color: pink;">=</span> <span style="color: #800080;">$objVM</span> <span style="color: pink;">|</span> Get<span style="color: pink;">-</span>View
		<span style="color: #800080;">$VMView</span>.setCustomValue<span style="color: #000000;">&#40;</span><span style="color: #800080;">$CustomFieldName</span><span style="color: pink;">,</span><span style="color: #800080;">$strPersistence</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># End of script</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ben.neise.co.uk/index.php/2009/05/add-drive-persistence-state-to-the-vi-client-using-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add Disk Size Information to the VI Client using Powershell</title>
		<link>http://ben.neise.co.uk/index.php/2009/04/add-disk-size-information-to-the-vi-client-using-powershell/</link>
		<comments>http://ben.neise.co.uk/index.php/2009/04/add-disk-size-information-to-the-vi-client-using-powershell/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 11:41:47 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Powershell Script]]></category>
		<category><![CDATA[VMware]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[VI Toolkit]]></category>

		<guid isPermaLink="false">http://ben.neise.co.uk/?p=123</guid>
		<description><![CDATA[Using PowerShell and the VI Toolkit to add the size of hard drives as a custom attribute to VIC.]]></description>
			<content:encoded><![CDATA[<p>This is based on Hugo Peeters&#8217; script to <a href="http://www.peetersonline.nl/index.php/vmware/add-snapshot-information-to-the-vi-client-using-powershell/">Add Snapshot Information to the VI Client using Powershell</a>.</p>
<p>Our users occasionally need larger machines created for packaging big applications.  After increasing the size, we used to append the VM Object name (e.g, &#8220;PACKVM01 &#8211; 10GB&#8221;), but this caused a mismatch between the virtual machine object name in VIC and the DNS host name. Also, it looked untidy!</p>
<p>We needed a new way for VIC users to be able to tell which were the larger machines, so I modified Hugo&#8217;s script to add disk size as a custom attribute.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Add disk size as a custom attribute</span>
&nbsp;
<span style="color: #800080;">$VCServerName</span> <span style="color: pink;">=</span> “MYVCSERVER”
<span style="color: #800080;">$VC</span> <span style="color: pink;">=</span> Connect<span style="color: pink;">-</span>VIServer <span style="color: #800080;">$VCServerName</span>
<span style="color: #800080;">$SI</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>View ServiceInstance
<span style="color: #800080;">$CFM</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>View <span style="color: #800080;">$SI</span>.Content.CustomFieldsManager
&nbsp;
<span style="color: #008000;"># Variables</span>
<span style="color: #800080;">$CustomFieldName</span> <span style="color: pink;">=</span> “HD Size <span style="color: #000000;">&#40;</span>GB<span style="color: #000000;">&#41;</span>”
<span style="color: #800080;">$ManagedObjectType</span> <span style="color: pink;">=</span> “VirtualMachine”
&nbsp;
<span style="color: #008000;"># Check if the custom field already exists</span>
<span style="color: #800080;">$myCustomField</span> <span style="color: pink;">=</span> <span style="color: #800080;">$CFM</span>.Field <span style="color: pink;">|</span> <span style="color: #0000FF;">Where</span> <span style="color: #000000;">&#123;</span><span style="color: #000080;">$_</span>.Name <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$CustomFieldName</span><span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">If</span> <span style="color: #000000;">&#40;</span><span style="color: pink;">!</span><span style="color: #800080;">$myCustomField</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># Create Custom Field</span>
	<span style="color: #800080;">$FieldCopy</span> <span style="color: pink;">=</span> <span style="color: #800080;">$CFM</span>.Field<span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span>
	<span style="color: #800080;">$CFM</span>.AddCustomFieldDef<span style="color: #000000;">&#40;</span><span style="color: #800080;">$CustomFieldName</span><span style="color: pink;">,</span> <span style="color: #800080;">$ManagedObjectType</span><span style="color: pink;">,</span> <span style="color: #800080;">$FieldCopy</span>.FieldDefPrivileges<span style="color: pink;">,</span> <span style="color: #800080;">$FieldCopy</span>.FieldInstancePrivileges<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #800080;">$objVMs</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>VM
<span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$objVM</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$objVMs</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$objTotalDiskSize</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>
	<span style="color: #008000;"># Sum the total size of all disks attached to the VM</span>
	<span style="color: #0000FF;">ForEach</span>	<span style="color: #000000;">&#40;</span><span style="color: #800080;">$objHardDisk</span> <span style="color: #0000FF;">in</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$objVM</span> <span style="color: pink;">|</span> Get<span style="color: pink;">-</span>HardDisk<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$objTotalDiskSize</span> <span style="color: pink;">+=</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$objHardDisk</span>.CapacityKB<span style="color: pink;">/</span><span style="color: #804000;">1024</span><span style="color: pink;">/</span><span style="color: #804000;">1024</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">If</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$objTotalDiskSize</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #008000;"># Round the size to one decimal place</span>
		<span style="color: #800080;">$objHDSize</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;{0:N1}&quot;</span> <span style="color: #FF0000;">-f</span> <span style="color: #800080;">$objTotalDiskSize</span>
		<span style="color: #800080;">$VMView</span> <span style="color: pink;">=</span> <span style="color: #800080;">$objVM</span> <span style="color: pink;">|</span> Get<span style="color: pink;">-</span>View
		<span style="color: #800080;">$VMView</span>.setCustomValue<span style="color: #000000;">&#40;</span><span style="color: #800080;">$CustomFieldName</span><span style="color: pink;">,</span><span style="color: #800080;">$objHDSize</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ben.neise.co.uk/index.php/2009/04/add-disk-size-information-to-the-vi-client-using-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Virtualization EcoShell</title>
		<link>http://ben.neise.co.uk/index.php/2009/04/virtualization_ecoshell/</link>
		<comments>http://ben.neise.co.uk/index.php/2009/04/virtualization_ecoshell/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 14:22:05 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Powershell Script]]></category>
		<category><![CDATA[VMware]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[VI Toolkit]]></category>

		<guid isPermaLink="false">http://ben.neise.co.uk/?p=90</guid>
		<description><![CDATA[I&#8217;ve started looking at the recently released beta of Vizioncore&#8217;s Virtualization EcoShell, which is a VMware tailored version of PowerGUI. Both are GUI front-ends for (among other things) VMware&#8217;s Powershell based VI Toolkit, which I&#8217;ve talked about before. I never got the full benefit from PowerGui, as by the time I&#8217;d realised there was more [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve started looking at the recently released beta of <a href="http://thevesi.org/index.jspa">Vizioncore&#8217;s Virtualization EcoShell</a>, which is a VMware tailored version of <a href="http://www.powergui.org/index.jspa">PowerGUI</a>.</p>
<p>Both are GUI front-ends for (among other things) <a href="http://www.vmware.com/sdk/vitk_win/index.html?ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:en-GB:official&amp;client=firefox-a">VMware&#8217;s Powershell based VI Toolkit</a>, <a href="http://ben.neise.co.uk/index.php/2009/03/using-vmwares-vi-toolkit/">which I&#8217;ve talked about before</a>.</p>
<p>I never got the full benefit from PowerGui, as by the time I&#8217;d realised there was more to it than the (very good) script editor, I&#8217;d already developed a lot of the scripts I needed &#8211; scripts which PowerGui would have been able to generate for me a lot more quickly (c&#8217;est la vie).</p>
<p>EcoShell has the ability to export some <a href="http://www.virtu-al.net/wp-content/uploads/2009/04/virtualcenterreport.jpg">nice looking reports</a> (although the <a href="http://ict-freak.nl/2009/01/27/vmware-vdiagram-script-by-alan-renout/">Visio based vDiagram</a> functionality still doesn&#8217;t work for me).</p>
<p>There&#8217;s more information on EcoShell over at <a href="http://www.virtu-al.net/2009/04/15/the-turtle-has-landed-the-ves-is-here/">Virtua-Al</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ben.neise.co.uk/index.php/2009/04/virtualization_ecoshell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hal Rottenberg&#8217;s book available for pre-order</title>
		<link>http://ben.neise.co.uk/index.php/2009/04/hal-rottenbergs-book-available-for-pre-order/</link>
		<comments>http://ben.neise.co.uk/index.php/2009/04/hal-rottenbergs-book-available-for-pre-order/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 08:55:59 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[VMware]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[VI Toolkit]]></category>

		<guid isPermaLink="false">http://ben.neise.co.uk/?p=87</guid>
		<description><![CDATA[Hal Rottenberg&#8217;s book Managing VMWare Infrastructure with Windows PowerShell is now available for pre-order. I&#8217;m a big fan of Hal&#8216;s; he&#8217;s helped me out a few times on the VMware VI Toolkit forums. Sadly shipping to the UK is over $76, meaning that I&#8217;ll have to either wait for a digital copy, or see if [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.scriptingoutpost.com/p-129-managing-vmware-infrastructure-with-windows-powershell-tfm.aspx">Hal Rottenberg&#8217;s book </a><span class="ProductNameText"><a href="http://www.scriptingoutpost.com/p-129-managing-vmware-infrastructure-with-windows-powershell-tfm.aspx">Managing VMWare Infrastructure with Windows PowerShell</a> is now available for pre-order.</span></p>
<p><span class="ProductNameText">I&#8217;m a big fan of <a href="http://communities.vmware.com//people/halr9000;jsessionid=EB377B6915DC753DC2F843ACDD4D162E">Hal</a>&#8216;s; he&#8217;s helped me out a few times on the <a href="http://communities.vmware.com/community/developer/windows_toolkit">VMware VI Toolkit forums</a>. Sadly shipping to the UK is over $76, meaning that I&#8217;ll have to either wait for a digital copy, or see if a local publisher/distributer takes it up.<br />
</span></p>
]]></content:encoded>
			<wfw:commentRss>http://ben.neise.co.uk/index.php/2009/04/hal-rottenbergs-book-available-for-pre-order/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VI Toolkit Quick Reference</title>
		<link>http://ben.neise.co.uk/index.php/2009/03/vi-toolkit-quick-reference/</link>
		<comments>http://ben.neise.co.uk/index.php/2009/03/vi-toolkit-quick-reference/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 12:22:37 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Powershell Script]]></category>
		<category><![CDATA[VMware]]></category>
		<category><![CDATA[VI Toolkit]]></category>

		<guid isPermaLink="false">http://ben.neise.co.uk/?p=82</guid>
		<description><![CDATA[I just found Alan Renouf &#8216;s VI Toolkit Quick Reference [PDF], which I wish I&#8217;d seen a few months ago.]]></description>
			<content:encoded><![CDATA[<p>I just found <a href="http://www.virtu-al.net/2009/02/23/vi-toolkit-quick-reference-guide/"></a><a href="http://www.virtu-al.net/2009/02/23/vi-toolkit-quick-reference-guide/">Alan Renouf</a> &#8216;s <a href="http://virtu-al.net/Downloads/VIToolkitQuickReferenceGuide.pdf">VI Toolkit Quick Reference</a> [PDF], which I wish I&#8217;d seen a few months ago.</p>
]]></content:encoded>
			<wfw:commentRss>http://ben.neise.co.uk/index.php/2009/03/vi-toolkit-quick-reference/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running SQL Queries against the Virtual Center Database</title>
		<link>http://ben.neise.co.uk/index.php/2009/03/running-sql-queries-against-the-virtual-center-database/</link>
		<comments>http://ben.neise.co.uk/index.php/2009/03/running-sql-queries-against-the-virtual-center-database/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 11:37:17 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[VMware]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[VI Toolkit]]></category>

		<guid isPermaLink="false">http://ben.neise.co.uk/?p=67</guid>
		<description><![CDATA[In a previous post, I said that there was no real way of extracting information from the VC database. This isn&#8217;t entirely true, as until I started using PowerShell, I was using SQL to query the VC database directly. List XP Machines and their RAM allocation SELECT VPX_VM.GUEST_OS, VPX_VM.MEM_SIZE_MB FROM VC_DB.dbo.VPX_VM VPX_VM WHERE &#40;VPX_VM.GUEST_OS='winXPProGuest'&#41; List [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://ben.neise.co.uk/?p=46">previous post</a>, I said that there was no real way of extracting information from the VC database.</p>
<p>This isn&#8217;t entirely true, as until I started using PowerShell, I was using SQL to query the VC database directly.</p>
<h3>List XP Machines and their RAM allocation</h3>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> VPX_VM<span style="color: #66cc66;">.</span>GUEST_OS<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>MEM_SIZE_MB
<span style="color: #993333; font-weight: bold;">FROM</span> VC_DB<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>VPX_VM VPX_VM
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span>VPX_VM<span style="color: #66cc66;">.</span>GUEST_OS<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'winXPProGuest'</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>List Information about Templates</h3>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> VPX_VM<span style="color: #66cc66;">.</span>LOCAL_FILE_NAME<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>GUEST_OS<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>IS_TEMPLATE<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>MEM_SIZE_MB<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>HOST_ID<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>ID
<span style="color: #993333; font-weight: bold;">FROM</span> VC_DB<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>VPX_VM VPX_VM
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span>VPX_VM<span style="color: #66cc66;">.</span>IS_TEMPLATE<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> VPX_VM<span style="color: #66cc66;">.</span>LOCAL_FILE_NAME</pre></div></div>

<h3>Host names and memory</h3>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> VPX_HOST<span style="color: #66cc66;">.</span>ID<span style="color: #66cc66;">,</span> VPX_HOST<span style="color: #66cc66;">.</span>DNS_NAME<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">CAST</span> <span style="color: #66cc66;">&#40;</span>VPX_HOST<span style="color: #66cc66;">.</span>MEM_SIZE <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">FROM</span> VC_DB<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>VPX_HOST VPX_HOST
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> VPX_HOST<span style="color: #66cc66;">.</span>DNS_NAME</pre></div></div>

<h3>Guest Information</h3>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> VPX_VM<span style="color: #66cc66;">.</span>LOCAL_FILE_NAME<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>DNS_NAME<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>GUEST_OS<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>IS_TEMPLATE<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>IP_ADDRESS<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>MEM_SIZE_MB<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>GUEST_STATE<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>POWER_STATE<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>HOST_ID<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>ID<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>BOOT_TIME
<span style="color: #993333; font-weight: bold;">FROM</span> VC_DB<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>VPX_VM VPX_VM
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span>VPX_VM<span style="color: #66cc66;">.</span>IS_TEMPLATE&amp;lt;&amp;gt;;<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> VPX_VM<span style="color: #66cc66;">.</span>DNS_NAME<span style="color: #66cc66;">,</span> VPX_VM<span style="color: #66cc66;">.</span>LOCAL_FILE_NAME</pre></div></div>

<p>These were based on ideas from <a href="http://waynes-world-it.blogspot.com/2008/04/vmware-clustervirtual-center-statistics.html">Wayne&#8217;s World of IT</a>. While it&#8217;s a lot less friendly to work with, the advantage is that it&#8217;s a lot quicker than the VI Toolkit&#8217;s <em>Get-</em> commands, and I still use them from time-to-time.</p>
<p>Although you could use this approach to modify entries in the database, I would only ever feel comfortable using this to extract information.</p>
]]></content:encoded>
			<wfw:commentRss>http://ben.neise.co.uk/index.php/2009/03/running-sql-queries-against-the-virtual-center-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to using VMware vSphere PowerCLI</title>
		<link>http://ben.neise.co.uk/index.php/2009/03/intro-to-powercli/</link>
		<comments>http://ben.neise.co.uk/index.php/2009/03/intro-to-powercli/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 10:33:06 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Powershell Script]]></category>
		<category><![CDATA[VMware]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[vCenter]]></category>
		<category><![CDATA[VI Toolkit]]></category>
		<category><![CDATA[vSphere]]></category>

		<guid isPermaLink="false">http://ben.neise.co.uk/?p=46</guid>
		<description><![CDATA[An introduction to using VMware PowerCLI to manage virtual machines.]]></description>
			<content:encoded><![CDATA[<p>About 2 years ago, I moved from working as an application packager, and being responsible for two or three (VMware Workstation) virtual machines, to working on the supporting infrastructure for over 200 packagers who require around 700 virtual machines on VMware vSphere (which was at that point called VMware ESX).</p>
<p>While I was (and I still am) impressed by VMware&#8217;s hypervisor based virtualisation, there were a few things that started to grate with such an otherwise excellent product:-</p>
<ul>
<li>Performing repetitive tasks &#8211; for example setting a group of virtual hard-drives to non-persistent &#8211; using the vSphere Client GUI was time-consuming (and RSI inducing!).</li>
<li>There was also no real way of extracting information from vCenter in any structured way. For example, if I wanted to know how many of our Microsoft Windows XP guests had over 512MB RAM allocated to them, they had to be counted manually.</li>
</ul>
<p>I had a look into <a href="http://www.amazon.com/Scripting-VMware-Power-Tools-Infrastructure/dp/1597490598">running scripts on the host</a>, and toyed with the <a href="http://www.vmware.com/download/vi/drivers_tools.html">Remote CLI Appliance</a>, but it was the <a href="http://communities.vmware.com/community/vmtn/vsphere/automationtools/windows_toolkit">VMware PowerCLI</a> that unlocked the functionality I&#8217;d been looking for.</p>
<p>VMware <a href="http://communities.vmware.com/community/vmtn/vsphere/automationtools/windows_toolkit">PowerCLI</a> (formerly VI Toolkit for Windows)  utilises <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx">Windows PowerShell </a>to provide a command-line driven interface for your virtual infrastructure. This can dramatically reduce the amount of time taken to perform almost all batch-style tasks and also enables some pretty advanced reporting functionality.</p>
<p>This is nowhere near a proper introduction in how to use PowerShell, but should give enough information to get you started, and hopefully make you want to find out more.</p>
<h2>PowerCLI pre-requisites</h2>
<p>Powershell is included in Windows 7, and Server 2008, but if it&#8217;s not included in your version of Windows, you&#8217;ll need to  <a href="http://support.microsoft.com/kb/968930" target="_blank">download and install the appropriate version of Windows PowerShell</a> for your operating system.</p>
<p>Then you need to <a href="http://www.vmware.com/support/developer/PowerCLI/index.html" target="_blank">download and install VMware vSphere PowerCLI</a> (registration required).</p>
<p>If you&#8217;re running  PowerShell for the first time you need to change the default execution policy.  To do this:</p>
<ol>
<ol>
<li>As a user with local Administrator rights, run Windows PowerShell  (on machines with <a href="http://en.wikipedia.org/wiki/User_Account_Control" target="_blank">UAC</a>, right-click and <strong>Run as Administrator</strong>)</li>
<li>In that window &#8211; run the command</li>
</ol>
</ol>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Set-ExecutionPolicy</span> <span style="color: pink;">-</span> ExecutionPolicy RemoteSigned</pre></div></div>

<p style="padding-left: 30px;">This allows you to run local scripts which have not been signed with a digital signature (which will almost certainly include the scripts you&#8217;re using to learn). Scripts from remote sources will still require signing.</p>
<h2>Security considerations</h2>
<p>The PowerShell security model is designed to address some of the  failures of VBScript &#8211; a common virus attack vector. PowerShell scripts (which have a PS1 suffix) do not run by default when invoked in Windows.  Also, as noted above, the default execution policy is not to run unsigned scripts. In order to run a script, you need to modify the execution policy, and then run the script name from the command line. This helps to prevent them from being launched by accident.</p>
<p>In vSphere, users running PowerCLI scripts  have the same permissions as they would get if they logged into vSphere Client. However, as with all scripting languages,  when modifications can be  made easier and faster, the potential impact of mistakes is made greater. PowerShell includes <a href="http://www.computerperformance.co.uk/powershell/powershell_whatif_confirm.htm">specific measures to alleviate risk</a>, and it&#8217;s worth being familiar with this functionality before trying anything more complex.</p>
<h2>PowerShell fundamentals</h2>
<p>PowerShell works using <em>cmdlets</em>. These are typically fairly descriptive, and great care has been taken to make them work in a consistent and logical way. Most cmdlets follow the format verb-noun, with modifiers for the target of the action, and any cmdlet specific options. They are not case sensitive.</p>
<p>The downside of the commands being so descriptive is that they are sometimes quite long. In order to alleviate this PowerShell allows Aliases to be created. Most common Windows Shell commands already exist in PowerShell as aliases. For example <strong>CD</strong>, <strong>DIR</strong>, <strong>CLS </strong>&amp; <strong>REN </strong>all work as expected. I find these quite useful when working interactively (entering commands at the prompt for immediate execution), but I tend to avoid them in scripts for the sake of consistency and clarity.</p>
<p>When launching scripts, you need to use absolute paths. For example, if you want to launch the script <strong>C:\Scripts\ExampleScript.ps1</strong>, when you&#8217;re in <strong>C:\Scripts</strong> you would either need to enter the whole path, or use  <strong>./ExampleScript.ps1.</strong></p>
<p>In order to use PowerShell, you need to import the VI commands using</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Add-PSSnapin</span> VMware.VimAutomation.Core</pre></div></div>

<p>Running the VMware vSphere PowerCLI shortcut created when you install the application does this on launch.</p>
<p><img class="alignnone size-full wp-image-225" title="PowerCLI_Shortcut" src="http://ben.neise.co.uk/wp-content/uploads/2009/03/PowerCLI_Shortcut1.png" alt="PowerCLI_Shortcut" width="46" height="44" /></p>
<p>Running the standard PowerShell shortcut does not.</p>
<p><img class="alignnone size-full wp-image-226" title="PowerShell_Icon" src="http://ben.neise.co.uk/wp-content/uploads/2009/03/PowerShell_Icon.png" alt="PowerShell_Icon" width="47" height="42" /></p>
<p>You can however add it to your <a href="http://www.howtogeek.com/50236/customizing-your-powershell-profile/" target="_blank">PowerShell profile</a>, which will enable it in all PowerShell sessions, no matter which shortcut you use to launch them.</p>
<p>PowerShell is object-oriented, meaning that the information returned from commands can be easily used as the input for another command.</p>
<p>If you want to put comments into your scripts, PowerShell ignores anything after the <strong>#</strong>symbol.</p>
<h2>Some simple Cmdlets</h2>
<p>Here are a couple of commands to get you started. Open up the PowerCLI command line using the VMware vSphere PowerCLI shortcut, then enter them as shown.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-Help</span></pre></div></div>

<p>Can display help on the various cmdlets. Running this as above shows the syntax for getting help.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-Command</span></pre></div></div>

<p>Use to find out all the commands containing certain keywords. For example&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-Command</span> <span style="color: pink;">*-</span>VM</pre></div></div>

<p>&#8230;uses the wildcard character (*) to show all commands that end with VM, this shows all the  cmdlets that can be used to operate on Virtual Machines. Let&#8217;s try a simple one&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">Get<span style="color: pink;">-</span>VM</pre></div></div>

<p>You should now get an error message saying &#8220;<em>You are not currently connected to any servers. Please connect first using Connect-VIServer or one of its aliases</em>.&#8221;. Let&#8217;s do that&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">Connect<span style="color: pink;">-</span>VIServer Name_of_your_vCenter_Server</pre></div></div>

<p>This uses your current windows credentials to connects to the specified server.  You need to do this before you run any VMware specific PowerShell commands. Now try this again&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">Get<span style="color: pink;">-</span>VM</pre></div></div>

<p>You should now be looking at a list of virtual machines managed by your vCenter server.  You can reduce the scope by adding switches, for example&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">Get<span style="color: pink;">-</span>VM –Name A<span style="color: pink;">*</span></pre></div></div>

<p>&#8230;gets all machines with names starting “A”. For more information, try</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-Help</span> Get<span style="color: pink;">-</span>VM <span style="color: #008080; font-style: italic;">-Detailed</span></pre></div></div>

<p>Variables in PowerShell are always preceded by a $ symbol. You can set a variable to the result of any kind of PowerShell command, for example, you can store the results of a Get-VM in a variable&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$objVMs</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>VM</pre></div></div>

<p>then use that variable any time you need it, typing</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">$objVMs</pre></div></div>

<p>Will display the virtual machine objects stored in the variable. This variable is a collection of objects, each object representing a virtual machine, so we can run more commands against this variable:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">Get<span style="color: pink;">-</span>VMGuest <span style="color: pink;">-</span>VM $objVMs</pre></div></div>

<p>This lists the State, IP Address and guest OS of all your machine objects.</p>
<p>Instead of using variables for commands like this, you can also pipe the result of one command, straight into another. The equivalent of the above command, using pipes rather than variables is</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">Get<span style="color: pink;">-</span>VM <span style="color: pink;">|</span> Get<span style="color: pink;">-</span>VMGuest</pre></div></div>

<p>The objects output byt he first command are piped straight into the second command. Pipes are used extensively in PowerShell, and many cmdlets can be linked together using pipes. This means you can run some complex commands in PowerShell at the command prompt in one line, rather than resorting to writing a script.</p>
<p>Have a  play around with these commands in your test environment before moving onto the next section. As long as you&#8217;re using <strong>Get-</strong> based commands, (rather than <strong>Set-</strong> or <strong>Remove-</strong>) you shouldn&#8217;t make any changes, but append <strong>-WhatIf</strong> and/or <strong>-Confirm</strong> to the end of your Cmdlets if you&#8217;re feeling extra-cautious.</p>
<h2>Example scripts</h2>
<p>Like batch files, PowerShell scripts are simply collections of commands linked together into a text file.</p>
<p>Here are a couple of example scripts, showing what can be done.  Copy into notepad, and save with a PS1 extension. You should run <strong>Connect-VIServer</strong> interactively before running any of the scripts (or add it as the first line to the script file).</p>
<h3>Get information about a specific machine</h3>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$strVm</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Read-Host</span> <span style="color: #800000;">&quot;Please enter the VM name&quot;</span>
<span style="color: #800080;">$vm</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>VM <span style="color: #008080; font-style: italic;">-Name</span> <span style="color: #800080;">$strVm</span>
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$vm</span>.PowerState <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;PoweredOn&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
<span style="color: #800080;">$event</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>VIEvent <span style="color: pink;">-</span>Entity <span style="color: #800080;">$vm</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Where-Object</span> <span style="color: #000000;">&#123;</span><span style="color: #000080;">$_</span>.fullFormattedMessage <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;Task: Power on Virtual Machine&quot;</span><span style="color: #000000;">&#125;</span>
<span style="color: #800080;">$VM</span>.Name
<span style="color: #800080;">$VM</span>.PowerState
<span style="color: #000000;">&#40;</span>Get<span style="color: pink;">-</span>VMGuest <span style="color: pink;">-</span>VM <span style="color: #800080;">$VM</span><span style="color: #000000;">&#41;</span>.IPAddress<span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span>
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$event</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Set-Variable</span> <span style="color: #008080; font-style: italic;">-Name</span> user <span style="color: #008080; font-style: italic;">-Value</span> <span style="color: #800000;">&quot;N/A&quot;</span> <span style="color: #000000;">&#125;</span> <span style="color: #0000FF;">else</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Set-Variable</span> <span style="color: #008080; font-style: italic;">-Name</span> user <span style="color: #008080; font-style: italic;">-Value</span> <span style="color: #800080;">$event</span><span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span>.username <span style="color: #000000;">&#125;</span>
<span style="color: #800080;">$strMessageText</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Machine: &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$VM</span>.Name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`n</span>&quot;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;Power State: &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$VM</span>.PowerState <span style="color: pink;">+</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`n</span>&quot;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;IP Address: &quot;</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#40;</span>Get<span style="color: pink;">-</span>VMGuest <span style="color: pink;">-</span>VM <span style="color: #800080;">$VM</span><span style="color: #000000;">&#41;</span>.IPAddress<span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`n</span>&quot;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;Switched on by: &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$user</span>
<span style="color: #800080;">$strMessageText</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">else</span>
<span style="color: #000000;">&#123;</span><span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Machine not powered on&quot;</span><span style="color: #000000;">&#125;</span></pre></div></div>

<p>This script asks the user for a machine name (using <strong>Read-Host</strong>), then converts that string to a computer object, then (assuming the machine is switched on and VMware Tools is running), displays the DNS name, IP address and the username of the last user to power on the machine (as shown in the machine&#8217;s Event Log). The <strong>`n</strong> is a carriage return.</p>
<h3>Get All Windows XP Machines with more than 2Gb of RAM</h3>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$strMachine</span> <span style="color: #0000FF;">in</span> <span style="color: #000000;">&#40;</span>get<span style="color: pink;">-</span>vm <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Where-Object</span> <span style="color: #000000;">&#123;</span><span style="color: #000080;">$_</span>.MemoryMB <span style="color: #FF0000;">-gt</span> <span style="color: #800000;">&quot;2000&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
Get<span style="color: pink;">-</span>VMGuest <span style="color: pink;">-</span>VM <span style="color: #800080;">$strMachine</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Where-Object</span> <span style="color: #000000;">&#123;</span><span style="color: #000080;">$_</span>.OSFullName <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;Microsoft Windows XP Professional*&quot;</span><span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Select-Object</span> VMName<span style="color: pink;">,</span> IPAddress
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This script could easily be modified and used as a component to make modifications on machines fulfilling certain criteria.</p>
<h2>What else can you do?</h2>
<p>Almost anything that can be done in the GUI can be done in PowerShell.  Machines can be deployed, customized, switched on, migrated between hosts and resource pools etc. Or you could get the last time a machine was switched on, and by whom.</p>
<p>You can also use PowerCLI to report on the status of guests and hosts. Check out Alan Renouf&#8217;s excellent <a href="http://www.virtu-al.net/2009/08/18/powercli-daily-report-v2/">PowerCLI Daily Report</a>, or Hugo Peeter&#8217;s script to <a href="http://www.peetersonline.nl/index.php/vmware/track-datastore-free-space/">track free space in your datastores</a>.</p>
<p>One drawback of the API is that performance of cmdlets (especially <strong>Get-VM</strong>) is quite slow.  Hopefully this will be addressed in future versions.</p>
<h2>Further resources</h2>
<p>There are many tools, example scripts and on-line resources available. Your first stop for help should be the <a href="http://communities.vmware.com/community/vmtn/vsphere/automationtools/windows_toolkit">VMware vSphere PowerCLI Community</a>. I also recommend you keep <a href="http://www.virtu-al.net/2009/05/17/icomasoft-powercli-reference-card/">Alan Renouf&#8217;s PowerCLI reference card</a> close-to-hand when you&#8217;re just starting out.</p>
]]></content:encoded>
			<wfw:commentRss>http://ben.neise.co.uk/index.php/2009/03/intro-to-powercli/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

