Cleaning up files with powershell GUI confirmation

After automating my report last week, I decided I did not need a copy left lying around, so I decided to write a cleanup script.

My first version was very simple, and consisted of just the required code to recurse the directory and delete the files.

It occurred to me that a Yes/No MsgBox to confirm the deletion would be a nice addition. So without further adieu, here is the code.

And here is the output

MsgBox output from script

See how nice that looks? You can use all the Windows GUI Framework in your scripts. I will be designing a script next week that uses several parts of the windows framework to make the script more end user friendly. Keep reading to see my progress and learn how to apply these techniques to your script!

Report Automation with RVtools and Excel

Recently I needed to extract some configuration info from vCenter and email it someone on a semi-regular basis.

They wanted specific information about the hosts and VMs in each vCenter. I figured the easiest way to get the required info was to use Rob de Veij’s excellent RVTools.

To start, I ran RVTools, exported the host tab and the vm tab for 3 vCenters. Not the most efficient way this could be done. So, I made a simple batch file to output the info I wanted.

Now I was left with 6 spreadsheets full of data, most of which I do not need. Again, initially, I deleted the extraneous columns manually. Manual processes are tedious, boring and uninspired… Powershell to the rescue!

Since I had two different sets of output (vms and hosts) I needed two separate scripts to process them (yes I could have combined them, but I really like making things modular) The only difference is in line 2 (columns to keep), so I have only listed one here.

All these scripts do are remove the columns I do not need. A simple loop gets all the files from an array and processes them.

The only thing left is to email the modified Excel files to the person who requested the information. Why not automate that as well? And since we could add or subtract a vCenter, why not have the script e-mail every spreadsheet it finds in the output directory?

With all the pieces in place, the last step is to tie all these disparate scripts together. I created a control script that executes all the other scripts and displays the status as it goes.

And there you have it! All completely automated and no more work anytime I need the report sent.

Something I have considered changing is getting the list of files to process. I could read the directory and put them into the array automatically. Maybe next time…

I’d like to thank all the people who’s code I borrowed (who I promise to credit once I go back though my history and find them)

Removing a Datastore with PowerCLI/Powershell

Another issue that comes along with power outages? Hosts coming back up before their storage… With fiber channel, you can typically reconnect it with a refresh/rescan, but with NFS, they tend to become disconnected.

The fix is to remove and re-add the Datastore (see kb 1005057)

With no way to handle them en masse in the gui and no desire to have to repeat this process in the future, I used Powershell!

Here is the raw script, I will add comments in the near future for those interested.

I chose to run this per cluster

Fixing Greyed out Virtual Machines

As I am sure many of you have, I had an outage recently that caused my hosts to come up before my storage. This resulted in the dreaded invalid or “greyed out” VM.

There are, of course, other ways to recover from this, but I have found this to be the easiest!

Here are the steps to recover from this issue.

1. Determine which host the affected VM(s) are on.

2. SSH to the appropriate host (you will most likely have to enable SSH)

3. Type the following command:

This will list all the greyed out VMs (note it will present you with a numerical vm identifier)

4. Note the four digit ID and add it in place of 0000 in the following command:

If all goes well, in 30-60 seconds the VM will re-register and no longer show as invalid.

Windows vCenter to VCSA with a fling? yes, please!

If you are not familiar with VMWare Flings, then you should rush over and start checking them out NOW!!   If you are familiar, then maybe you also noticed that the engineers over at VMWare just released a vCenter to VCSA converter!!    There are a few limitations:  Must be vCenter 5.5, must be using a separate SQL server, downtime required and a few others. I have not had a chance to try it out in the lab, but I hope to soon and post my experience.

My vMetrics

Total Metrics:
44 GHz Total CPU
64 GB Total RAM
3,247 GB Total Disk
2 Host(s)
1 RPs
21 VMs
7 vMotions
Power States:
(3) (18) (0)
8 Physical NICs
8 Virtual PGs
DRS: DRS enabled (Full Auto)
HA: Highly Available
Current Deviation: 0.038
Target Deviation: 0.2
DRS Balance: Load Balanced
HP ProLiant DL360 G5

previous refresh
1297 days, 4 hrs and 34 min.

Recent Comments