Network stats must be the PowerShell topic of choice this week. The following all showed up in my RSS feed in the past few days:
- From Jeffery Hicks:
Turning IPConfig DNSCache into PowerShell - Lately I’ve been writing about techniques to turn command line tools into PowerShell tools. Although I suppose the more accurate description is turning command line output into PowerShell pipelined output. The goal is to run a command line tool and write objects to the PowerShell pipeline so I can do other operations with them. Today I have another example that takes the DNS cache from IPConfig and turns it into a collection of PowerShell objects.
- From Niklas:
PowerCLI to the rescue, how to check all VMs for Network card type - I got a question what network card some VMs had in a datacenter, as a best practice you should use VMXNET 3 where it is possible because it gives the best performance. So i wrote this very simple [PowerShell/PowerCLI] script in a few lines that do a csv export of all VMs and what kind of NIC they have, of course one could extend it with OS and stuff but that will have to be next time cause my schedule is kind of tight.
- From Luc Dekens (in answer to vite@1):
PowerCLI script that lists every IP within vCenter - I found a script that lists all VM’s IPs in vCenter and a command that list vMkernel ports adapter’s IPs. Is there a way that I can put these two together in a script? LucD - the following will give you the Name ,[IP Address] and the MAC
6 months ago - link
No sooner had I written my post on doing math in PowerShell, than PowerWF longtime friend Luc Dekens posted a new PowerShell function that does a very common calculation for you - converting big numbers to human readable units.
When you are dealing with big(ger) memory or storage values, it’s often not easy to present these in a human-readable format. I don’t know about you, but I find a number like 766509056 less readable, and meaningful, then 731 MB in a report.
You can introduce some nested If-Then-Else constructs in your scripts to do the conversion, but why repeat these in all your scripts ? That’s why I decided to write me a handy function, that would solve this problem once and for all.
(Read More)
Click over to get the script and see example usage.
6 months ago - link
Eric Wright has a blog post on Automating ESXi Deployment with PowerCLI.
Our Windows administration department has fully embraced Windows Powershell for our administration tasks not only with Windows administration but for our VIP environment as well. Since our VIP environment utilizes VMware vSphere Hypervisors, common administration tasks and configuration actions can be performed via vSphere PowerCLI libraries.
Practically anything you can do inside the vSphere Client you can perform in a PowerCLI session. Therefore, you can create functions to fully configure your ESX/ESXi host after initial deployment (configuring an IP address to the service console and setting a root password).
Eric includes PowerCLI scripts that configure the ESXi host and a virtual switch. You can get the scripts here.
6 months ago - link
I was writing a new PowerShell script for PowerVI the other day to monitor free drive space on my virtual machines. Using PowerCLI, it was very easy to get the basic storage information for my VMs - UsedSpaceGB, ProvisionedSpaceGB, but I was surprised that Free Space and Percent Used were not immediately available.
No problem, PowerShell makes it pretty easy for me to add my own calculated columns to my results.
Select-Object -Property Name, Host, NumCpu, MemoryMB, PowerState, UsedSpaceGB, ProvisionedSpaceGB, @{Name=”PercentUsed”;Expression={[System.Math]::Round(100 * $_.UsedSpaceGB / $_.ProvisionedSpaceGB, 2)}}, @{Name=”FreeGB”;Expression={ [System.Math]::Round($_.ProvisionedSpaceGB - $_.UsedSpaceGB , 2)}}
You can see that I added one column named PercentUsed, and a second column called FreeGB. The syntax is basically…
@{Name=”ColumnName”;Expression={your-expression}}
In this case, the Expression uses the $_ syntax to perform a calculation on the current object. PowerShell doesn’t have its own math cmdlets, and instead relies on the [System.Math] methods. You can see more examples here.
BTW - the complete script is included with the latest version of PowerVI.
6 months ago - link
Did you know that you can store default credentials for PowerCLI to use when it connects to a VIServer? Andrey Anastasov has a post on the VMware blogs that shows you how this works.
It just occurred to me that a very useful feature of PowerCLI never got the introduction it deserves. The feature is the Credential Store and as the name suggests its job is to store credentials. As a result:
- Credentials are kept securely (no need to hard code passwords along with scripts)
- You type less (no need to specify user and password to Connect-VIServer)
(read more)
Pretty slick, but I’m not sure if I would feel comfortable using it or not. I guess you could add a default user with minimal credentials to the store so you could have read access to data, but not necessarily hurt anything.
It does point out how valuable PowerVI’s shared credential feature is - In PowerVI, your credentials are based on the user that is logged into the vSphere Client, so only people with the proper permissions can execute certain scripts.
6 months ago - link
Virtu-Al is reporting that VMware has released PowerCLI cmdlets for managing vSphere Distributed Switches. These cmdlets are not officially supported and do not work in Windows 7 or Vista environments, but are definitely worth a look.
For a long time now when presenting at VMworld or VMUGS I have asked the question – What would you like to see from PowerCLI next ? The standard answer I get at most of these is vSphere Distributed Switch (VDS) cmdlets !
Luc Dekens did a great job with his series of posts showing how you could create your own VDS advanced functions, these allow you to add the functions to your PowerCLI session and work with VDS, find them here and take a look, they are a great example of how you can expand PowerCLI to support areas not yet written.
So what’s new ?
VMware have now released some PowerCLI cmdlets as a fling which work with VDS and allow you to do most of the common VDS tasks in your vSphere environment. Please note that this is a fling and therefore not officially supported by VMware. Having said that we are definitely keen for you to download these and try them out – please make sure you leave feedback as this will help with future versions !
Alan’s page includes download instructions, a list of the new cmdlets as well as some usage examples. What are you waiting for, jump over there and get working.
6 months ago - link
The PowerScripting podcast with Jim Hofer (me) and Kirk Munro has been posted.
Tonight on the PowerScripting Podcast, we talk to Jim Hofer and Kirk Munro with Devfarm Software
22:01:20] <alexandair> ## how do you see the future of powershell script editors now that powershell ise v3 is becoming very good?
[22:02:19] <ScriptingWife> ##be sure to have them talk about their dialog pop up window on their webpage. IT is so AWESOME
Jim – Superhero: Dark Phoenix
Kirk – Favorite software: Social media
6 months ago - link
Hal stubbed his toe on an interesting feature of PowerShell the other day. He was building a bunch of virtual machines from a data in a spreadsheet, when he realized that the column for memory in his spreadsheet was GB, but PowerCLI expects the memory to be in MB.
Long story short, I wrote a quick one-liner in PowerShell to “spec out” the newly-cloned virtual machines using this spreadsheet. As I said, it’s not a build process yet, but it will be when I’m done. Baby steps. The one-liner looks like this ($t is the variable that holds the data obtained from the spreadsheet, using a simple Import-Csv cmdlet):
$t | % { Set-VM -VM $_.name -NumCpu $_.cpu -MemoryMB $_.Memory }
Once I started that running, I quickly realized that 8MB VMs would do me no good. Smile So, I amended my script to this:
$t | % { Set-VM -VM $_.name -NumCpu $_.cpu -MemoryMB ( $_.Memory * 1024 ) }
That’s when I got a really weird error:
Set-VM : Cannot bind parameter ‘MemoryMB’. Cannot convert value “888888888888888…
(read more)
Since the spreadsheet data was interpreted as strings, PowerShell assumed he wanted the string appended to itself 1024 times. Hal solved the problem using type-casting. Read the entire post for details.
7 months ago - link
Daniel Holm at Interworks ran into a problem where his backup solution occasionally left virtual machine snapshots around -
The software is overall very good, but can have some quirks if things don’t go as planned. One issue we hit is longer-running replica jobs might conflict with local jobs, causing jobs to fail, and snapshots to get locked and abandoned.
Daniel wrote a quick PowerCLI script that connects to his virtual center and then removes all snapshots that match the name his backup solution uses. It might be worth creating a derivative of this for use in PowerVI - perhaps a script or workflow that finds all snapshots older than a certain date, and then prompts to see if you want to delete any of them.
7 months ago - link
Over at get-admin.com I found a recent post that talks about balancing LUN paths for a vSphere cluster using PowerCLI. The script basically:
- Gets the list of hosts on a cluster,
- Walks through each host
- Looks at the storage (LUNs) for each host and sorts them
- Find the desired paths for each host
- Make sure each host has the desired paths.
From the author:
I’ve found that it’s been somewhat difficult to keep the LUN paths to each host balanced. By balanced, I mean that for each LUN to each host, there is a number of paths and I want to make sure that, for example, each LUN 10 to each of the hosts is using path A as the primary and path B as the stand by. LUN 11 uses path B as the primary, LUN 12 back to path A, and so on.
[…]
I wanted the LUNs to be balanced based off their LUN identifier rather than the canonical name…they don’t always follow the same order, and in the case of my hosts with two HBAs (and consequentially, two paths per LUN), I wanted all odd LUNs to use one path for the primary and all even LUNs to use the other. Justin’s script* does an excellent job of ensuring that the paths are evenly distributed, as you will end up with the same number on each, but not in the pretty fashion I desired.
(read more)
* The original inspiration for this script came from Justin Emerson’s script.
7 months ago - link