Hello there!
An organization I recently did contract worth with asked for an easy method to decommission VMware VMs. They were just starting to explore automation and also self-service for the owners of the VMs. This script was to be used by the Windows Systems Admins and/or VMware Admins during their normal decommissioning process. The process before hand was entirely manual – they would get a list of VMs from their change control system, log into vCenter server, find the VM, shut it down, and rename it (appended a -decomm to the VM name). After a two week hold, the VM would then be deleted.
Instead of changing the entire process to work a bit better, they requested that I simply automate each step in the current process. So, for part 1 (of 2) in this series, I will be giving you Decomm-PoweroffRename.ps1.

This script is rough, but workable. To start, the parameters:

This was a TXT file that was saved in a central location from the Change Control system. The name of it varied based on who was doing the export, so it was not an easy matter to statically define it. Instead, we chose to make it a parameter to pass in.

This is probably one of my least favorite things, but one you’ll see crop up from time to time in my scripts. One requirement I was given by the business was to be able to just run the script without and kind of user input or validation. -Force will do just that. If -Force is not set, the list of VMs will be presented in the console with an escape option. Setting -Force will skip this validation step and continue… placing a sizable amount of faith on the TXT file contents.

.PARAMETER SkipPowerOffCheck
This was another one of the requests/requirements from leadership. The script, by default, will check to make sure the VM actually powers off. If it is found to still be powered on, a more forceful method of powering off is attempted. If that fails, an error is logged. -SkipPowerOffCheck will skip this validation sequence – all in the name of saving time (in a large list of VMs, this section could add a few minutes to execution time).

There are also a few variables you may want to review and change for your environment:

$vcenter = “vcenter.resting-blade.com” – this should be updated for your vCenter server name
$tempDir = “\\scripts\output\nightlyReports”
$tempPath = “$tempDir\$scriptName”
$outputFilePath = “$tempPath\$scriptName.$date.csv” — all of these paths should be accessible to the computer running the script. We had a central repository for these outputs.
$logDir = “\\scripts\output\logs\$scriptName”
$logPath = “$logDir\$scriptName.$date.txt”
$seconds = 120 — this is a delay timer, to allow the VM enough time to gracefully shutdown”

Once you have those set, you are ready to begin using the script. To break it down, there are 3 sections to this script:
1) Lines 78 through 95 contain the List Validation sequence. This will display the VM List in the console, and ask the user to continue. Any input besides “yes” or “y” (case INsensitive) will escape the script. This section is skipped if -Force is set. Setting -Force is logged.

2) Lines 105 to 145: this section loops through the list of VMs. Lines 112 to 122 is the section for renaming the VM (appending -decomm to the name). Lines 125 to 145 is where a graceful shutdown is attempted.

3) Lines 147 to 200 is where the final check is performed to ensure the VM is powered off. If it is not showing as powered off, a forceful power off is attempted. If this still manages to fail, that is logged and should be caught when the log is reviewed. If -SkipPowerOffCheck parameter was set, this check is skipped entirely. Setting this parameter is logged.

The $report object is built up in all three sections, and available to handle as you need. For this script, they wanted a CSV saved to a repository, which would be reviewed by either admins or change control board on a scheduled basis. You could also use my Send-HTMLEmail script to email the object out to as well!

That is it for Part 1. I will tidy up Part 2 and publish it in the next week or so. You can download Part 1’s script from this link: Decomm-PowerOffRename.ps1