PowerCLI Script to Change Tag Assignment

I have an environment where, using tags, unused VMs are automatically shutdown. This is done by creating a Tag Category called “Suspend Date” and when the current value is in the past, your VM gets shutdown. I don’t have many VMs that I own in this “test” environment, but the ones that I DO have there, I want to be available when I need them.

I TRY to login once every week or so and change the tag manually, but then I thought to myself… I should find a way to automate this! So I sat around for a while thinking about automating it… The problem is, I need to connect to the VPN first. Until today, that was a process that involved manual intervention. I had to launch the VPN program (in this case, Shrewsoft VPN Client for Windows), Select the profile, Enter the Username/Password, and click CONNECT.

Upon doing some research, I found I can use the IPSECC.exe command from Shrewsoft to connect to the VPN from the command line! HINT, the following services MUST be running for this to work.

How to fix Shrew Soft VPN failed to attach to key daemon error 02

Now that I can automate connecting to the VPN, the rest is SIMPLE! It’s a matter of listing the VMs that are mine, getting / removing the tag assignment, and assigning a new one. 🙂 I even added a section at the end to prompt to ask if I want to disconnect from the VPN or not. If not, it’ll stay connected! 🙂


################################################################
#
# PowerCLI Script to Remove the old Suspend Date
# tag and Assign the latest one.
# Script Created by Ben Liebowitz on 3/12/2021
#
################################################################
# Assign variables
# The tag format is 2021.03.12. The command below will use the get-date cmdlet to create the tag in this format.
$tag = ((get-date).AddDays(14)).ToString("yyyy.MM.dd")
# The tag category is Suspend Date
$category = "Suspend Date"

# Check to see if already connected to VPN
$process = Get-Process | Where {$_.Name -match "ipsecc"}
if (-not ($process)) {
Write-host "Connecting to the VPN..."
C:\"Program Files"\ShrewSoft\"VPN Client"\ipsecc.exe -r VPN -u ben.liebowitz -p PASSWORD -a
}
else {
Write-host "already connected"
}

# Sleep for a few seconds while the VPN connects
sleep 10

# Connect to lab vCenter
connect-viserver vcenter.demo -user ben.liebowitz -password "PASSWORD"

# Get each VM with my name in it, get the tag for the Suspend Date category, and  Remove It
Get-VM | Where {$_.Name -match "Ben Liebowitz"} | Get-TagAssignment -category $category | Remove-TagAssignment -confirm:$false
# Assign the latest tag to each VM.
Get-VM | Where {$_.Name -match "Ben Liebowitz"} | New-TagAssignment -tag $tag -confirm:$false

# Disconnect from lab vcenter
disconnect-viserver vcenter.demo -confirm:$false

# Ask if you want to disconnect from the VPN
# If yes, disconnect
$vpn = read-host 'Do you want to disconnect from the VPN?'
If ($vpn -match "yes") {
Get-Process |Where {$_.Name -match "ipsecc"} | Stop-Process }
else {
exit }

Thanks, Ben Liebowitz, VCP, vExpert NJ VMUG Leader

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.