<?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>sentania.net &#187; Powershell</title>
	<atom:link href="http://www.sentania.net/category/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sentania.net</link>
	<description>A day in the life of a systems administrator</description>
	<lastBuildDate>Wed, 10 Feb 2010 14:23:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>An explanation why get-harddisk is so slow</title>
		<link>http://www.sentania.net/2009/03/an-explanation-why-get-harddisk-is-so-slow/</link>
		<comments>http://www.sentania.net/2009/03/an-explanation-why-get-harddisk-is-so-slow/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 18:37:58 +0000</pubDate>
		<dc:creator>Scott Bowe</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[VMware]]></category>

		<guid isPermaLink="false">http://www.sentania.net/?p=75</guid>
		<description><![CDATA[It just so happened that today I was browsing the VMware blog for the Windows VI Toolkit, and came across a post the described in a fair amount of detail why get-harddisk was slow for me. Looks like the culprit is that I typically use get-view to pull my information out of the API, and [...]]]></description>
			<content:encoded><![CDATA[<p>It just so happened that today I was browsing the VMware blog for the Windows VI Toolkit, and came across a post the described in a fair amount of detail <a href="http://blogs.vmware.com/vipowershell/2009/03/why-is-my-code-so-slow.html" target="_blank">why get-harddisk was slow for me</a>.</p>
<p>Looks like the culprit is that I typically use get-view to pull my information out of the API, and as such I had to pass a VM name into get-harddisk, which results in some extremely slow object name translation voodoo.</p>
<p>This:</p>
<p>get-harddisk -vm (get-vm -name s092464).name</p>
<p>was over a minute slower than this:</p>
<p>get-harddisk -vm (get-vm -name s092464).</p>
<p>So imagine that times 1200.</p>
<p>That said, I think my script will still stay with the method I wrote about last week, simply because get-view suits my data needs better, plus it&#8217;s WAY faster:</p>
<p>get-vm -name s092464 = 2 minutes</p>
<p>get-view -viewtype virtualmachine -filter @{&#8220;Name&#8221; = &#8220;s092464&#8243;} = 5 seconds</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sentania.net/2009/03/an-explanation-why-get-harddisk-is-so-slow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>get-vm is retarded slow</title>
		<link>http://www.sentania.net/2009/03/get-vm-is-retarded-slow/</link>
		<comments>http://www.sentania.net/2009/03/get-vm-is-retarded-slow/#comments</comments>
		<pubDate>Fri, 20 Mar 2009 19:58:13 +0000</pubDate>
		<dc:creator>Scott Bowe</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[VMware]]></category>

		<guid isPermaLink="false">http://www.sentania.net/?p=69</guid>
		<description><![CDATA[I have been working a lot with the Windows VI Toolkit lately, it is a very straight forward way of working with your Virtual Infrastructure programmatically. One of the things I have noticed is that there are a few cmdlets, that are exceedingly slow.  These cmdlets aren&#8217;t so bad to use if you are working [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working a lot with the Windows VI Toolkit lately, it is a very straight forward way of working with your Virtual Infrastructure programmatically.</p>
<p>One of the things I have noticed is that there are a few cmdlets, that are exceedingly slow.  These cmdlets aren&#8217;t so bad to use if you are working with a single VM, but if you start doing some scripting against hundreds or thousands of machines, whoa boy &#8211; I mean hit enter, go make a cup of coffee, drink it, drop the kids off at the pool, and you&#8217;ll still have time to spare.</p>
<p>Thus far the worst cmdlets I&#8217;ve come across are get-vm and get-harddisk.  get-vmhost, and get-cluster are pretty pokey as well, but fortunately even in a large environment, there aren&#8217;t that many hosts and there are even fewer clusters so they don&#8217;t annoy me with their pokeyness much.  <em>Yet.</em></p>
<p>Here&#8217;s an example of how slow these cmdlets are, I have a script that I wrote that looks at my VI, and grabs every single VM that fits a certain naming pattern, which business rules dictate are VDI instances.  Next, I take the VM collection, and pull some interesting data: name, memory allocation, guestOS, IP, VLAN (portgroup), and VMDK size.  <a href="http://itsjustanotherlayer.com/?p=47" target="_self">I knew from the get go that get-vm was slow, so I was using get-view</a>.  get-view doesn&#8217;t return quite as friendly an object, but if you are willing to explore, it has everything you need.</p>
<p>So my collection of ~1200 VMs, comes back, and I do a little dance with it, then I get the portgroup:</p>
<p><span style="color: #ff0000;">$tempvm.vlan = (Get-View -Id $vm.network).name</span> <span style="color: #000000;">fairly speedy return</span></p>
<p><span style="color: #000000;">Then I get the VMDK size:</span></p>
<p><span style="color: #ff0000;">get-harddisk -VM $vm.name | foreach-object -process {$tempvm.diskGB += $_.CapacityKB}<br />
    $tempvm.diskGB = [math]::round(($tempvm.diskGB / 1048576),0)<br />
    if ($tempvm.diskGB -eq 0)<br />
    {<br />
        $tempvm.diskGB = &#8220;N-A&#8221;<br />
    }</span></p>
<p><span style="color: #000000;">Let&#8217;s jsut say you can go take a nice nap, but it returns the total size of the VMDKs attached to the system.</span></p>
<p><span style="color: #000000;">I then scheduled this script to run via AutoSys, and it happily generated a CSV of all my VDI instances, and the info I wanted &#8211; in <strong>35 hours</strong>!</span></p>
<p><span style="color: #000000;">I was a bit perturbed by the run time, but figured it was a batch job, and I could adjust the start times, and I&#8217;d just live with it.</span></p>
<p><span style="color: #000000;">Not any longer!  Today I had an epiphany of sorts while exploring the get-view object returned for a VM &#8211; it was if the skies had parted, God spoke to me, while a chorus of Angels sang.  Or I could have seen a Narwhal, I&#8217;m not really sure, but it was that cool.</span></p>
<p><span style="color: #000000;">The epiphony caused me to modify two lines of code &#8211; the temp.vlan line, and the get-harddisk line &#8211; replacing them with:</span></p>
<p><span style="color: #ff0000;">foreach ($device in ($myvm.config.hardware.device)) {if ($device.backing.devicename -like &#8220;VLAN*&#8221;) {$tempvm.vlan=$device.backing.devicename}}</span></p>
<p><span style="color: #000000;">and</span></p>
<p><span style="color: #ff0000;">foreach ($device in ($vm.config.hardware.device)) {if ($device.backing.filename -like &#8220;*vmdk&#8221;){$tempvm.diskGB += $device.capacityinKB}}</span></p>
<p><span style="color: #000000;">After the changes, I force started my job and waited eagerly for it to complete &#8211; 2 minutes and 15 seconds later.  Same result, only 35 hours less of waiting.</span></p>
<p> </p>
<p><span style="color: #000000;">And if you don&#8217;t believe me, here is the run log from AutoSys</span></p>
<p><span style="color: #000000;">Original Code:</span></p>
<p><span style="color: #000000;"></p>
<table border="0">
<tbody>
<tr bgcolor="#dddddd">
<td>[FORCE_STARTJOB]</td>
<td>Fri Mar 13 11:50:24 2009</td>
<td> </td>
<td> </td>
</tr>
<tr bgcolor="#eeeeee">
<td>Starting</td>
<td>Fri Mar 13 11:50:25 2009</td>
<td> </td>
<td> </td>
</tr>
<tr bgcolor="#dddddd">
<td>Running</td>
<td>Fri Mar 13 11:50:28 2009</td>
<td> </td>
<td> </td>
</tr>
<tr bgcolor="#eeeeee">
<td>Success</td>
<td>Sat Mar 14 23:21:53 2009</td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
<p>Revised Code:</p>
<p></span></p>
<table border="0">
<tbody>
<tr bgcolor="#eeeeee">
<td>Starting</td>
<td>Fri Mar 20 14:25:13 2009</td>
<td> </td>
<td> </td>
</tr>
<tr bgcolor="#dddddd">
<td>Running</td>
<td>Fri Mar 20 14:25:16 2009</td>
<td> </td>
<td> </td>
</tr>
<tr bgcolor="#eeeeee">
<td>Success</td>
<td>Fri Mar 20 14:27:25 2009</td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.sentania.net/2009/03/get-vm-is-retarded-slow/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Powershell script against VC &#8211; in batch!</title>
		<link>http://www.sentania.net/2009/03/powershell-script-against-vc-in-batch/</link>
		<comments>http://www.sentania.net/2009/03/powershell-script-against-vc-in-batch/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 20:33:11 +0000</pubDate>
		<dc:creator>Scott Bowe</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[VMware]]></category>

		<guid isPermaLink="false">http://www.sentania.net/?p=66</guid>
		<description><![CDATA[Not long ago, I posted a powershell script, that would set a standard memory and CPU reservation across an entire virtual center instance. As cool as that is, let&#8217;s make it even cooler, let&#8217;s do it as a scheduled task and so you don&#8217;t have to enter credentials! The first thing you need to do [...]]]></description>
			<content:encoded><![CDATA[<p>Not long ago, I posted a powershell <a href="http://www.sentania.net/2009/03/setting-a-standard-resource-reservation-across-your-vi/">script</a>, that would set a standard memory and CPU reservation across an entire virtual center instance.</p>
<p>As cool as that is, let&#8217;s make it even cooler, let&#8217;s do it as a scheduled task and so you don&#8217;t have to enter credentials!</p>
<p>The first thing you need to do is create a text file that contains an encrypted password.</p>
<blockquote><p><strong>read-host -assecurestring | convertfrom-securestring | out-file C:\securestring.txt</strong></p></blockquote>
<p>This will store the password you type into a text file, that is only decryptable by the user that ran the script.</p>
<p>Next we make a little modification of the script I posted earlier (the modfied script is below, with updated parts in <em>italics)</em></p>
<blockquote><p><strong>add-pssnapin VMware.VimAutomation.Core -erroraction Silentlycontinue<br />
$vcServer=”virtualcenter”<br />
<em>$user=&#8221;Cooluser&#8221;<br />
$credentialFile=&#8221;C:\securestring.txt&#8221;<br />
$pass = cat $credentialFile | convertto-securestring<br />
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$pass<br />
$vcConnection=connect-viserver -server $vcServer -credential $credentials -warningaction Silentlycontinue<br />
</em>$vms=get-view -viewtype VirtualMachine -server $vcConnection</strong></p>
<p><strong>$vms | % {$spec = new-object VMware.Vim.VirtualMachineConfigSpec;<br />
        $spec.memoryAllocation = New-Object VMware.Vim.ResourceAllocationInfo;<br />
        $spec.memoryAllocation.Shares = New-Object VMware.Vim.SharesInfo;<br />
        $spec.memoryAllocation.Shares.Level = “normal”;<br />
        $spec.memoryAllocation.Limit = -1;<br />
        $spec.memoryAllocation.Reservation = 512;<br />
        $spec.cpuAllocation = New-Object VMware.Vim.ResourceAllocationInfo;<br />
        $spec.cpuAllocation.reservation = 0<br />
        $spec.cpuallocation.Shares = New-Object VMware.Vim.SharesInfo;<br />
        $spec.cpuAllocation.Shares.Level = “normal”;<br />
        $spec.cpuAllocation.Limit= -1;<br />
        Get-View($_.ReconfigVM_Task($spec))}<br />
       <br />
disconnect-viserver -Confirm:$false</strong></p></blockquote>
<p>The best part of this script is, if you do run it interactively, it won&#8217;t display a certificate warning message, and it will disconnect your VC session &#8211; kind of like &#8220;Be kind, rewind&#8221;</p>
<blockquote><p><strong></strong></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.sentania.net/2009/03/powershell-script-against-vc-in-batch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting a standard resource reservation across your VI</title>
		<link>http://www.sentania.net/2009/03/setting-a-standard-resource-reservation-across-your-vi/</link>
		<comments>http://www.sentania.net/2009/03/setting-a-standard-resource-reservation-across-your-vi/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 18:56:05 +0000</pubDate>
		<dc:creator>Scott Bowe</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[VMware]]></category>

		<guid isPermaLink="false">http://www.sentania.net/?p=53</guid>
		<description><![CDATA[Me, I&#8217;m a big fan of standards.  Standard processes, standard this, standard that &#8211; standards are a great way to make your life easier when dealing with hundreds or thousands of objects. Inside of my Virtual Infrastructure, I&#8217;m a huge fan of ignoring the micro level of things, and paying attention to things at a [...]]]></description>
			<content:encoded><![CDATA[<p>Me, I&#8217;m a big fan of standards.  Standard processes, standard this, standard that &#8211; standards are a great way to make your life easier when dealing with hundreds or thousands of objects.</p>
<p>Inside of my Virtual Infrastructure, I&#8217;m a huge fan of ignoring the micro level of things, and paying attention to things at a macro level.  One of the ways I do that, is by ensuring that every virtual machine is configured with identical reservations &#8211; regardless of how much resources they are allocated.  If a given virtual machine (or group of virtual machines) requires a guaranteed amount of resources, I create a resource pool and toss them in there.</p>
<p>Not only does this prevent a &#8220;rouge&#8221; VM from stealing resources it may not need at a moment in time, it calls out the big hitters and puts them out in the open.</p>
<p>Our virtual machine creation process defines all new virtual machines with our standard reservation, but over the course of weeks and months, these drift.  Machines change, an admin &#8220;tests&#8221; something and forget&#8217;s to change it back, or we might even change the standard.  Here is a script that will locate all virtual machines in your infratructure, and update them to whatever makes you happy.</p>
<blockquote><p><strong>add-pssnapin VMware.VimAutomation.Core -erroraction Silentlycontinue<br />
$vcServer=&#8221;virtualcenter&#8221;<br />
$credentials = get-credential<br />
$vcConnection=connect-viserver -server $vcServer -credential $credentials<br />
$vms=get-view -viewtype VirtualMachine -server $vcConnection</strong></p>
<p><strong>$vms | % {$spec = new-object VMware.Vim.VirtualMachineConfigSpec;<br />
        $spec.memoryAllocation = New-Object VMware.Vim.ResourceAllocationInfo;<br />
        $spec.memoryAllocation.Shares = New-Object VMware.Vim.SharesInfo;<br />
        $spec.memoryAllocation.Shares.Level = &#8220;normal&#8221;;<br />
        $spec.memoryAllocation.Limit = -1;<br />
        $spec.memoryAllocation.Reservation = 512;<br />
        $spec.cpuAllocation = New-Object VMware.Vim.ResourceAllocationInfo;<br />
        $spec.cpuAllocation.reservation = 0<br />
        $spec.cpuallocation.Shares = New-Object VMware.Vim.SharesInfo;<br />
        $spec.cpuAllocation.Shares.Level = &#8220;normal&#8221;;<br />
        $spec.cpuAllocation.Limit= -1;<br />
        Get-View($_.ReconfigVM_Task($spec))}</strong></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.sentania.net/2009/03/setting-a-standard-resource-reservation-across-your-vi/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
