How to use PowerCLI to tag multiple vSphere virtual machines.
In this post PowerCLI with an encrypted password is used to connect to the vSphere vCentre and tag multiple virtual machines based on a certain criteria like OS type
VIRTUALISATION
Task:
To create and maintain a SQL database, listing all powered-on virtual machines in a vSphere vCenter, along with their associated location, owner, and operating system details.
A scheduled PowerShell script can then run on regular basis, e.g. every night, to update the SQL database.
Prerequisites:
To enable our system to read VM related information from our vCentre and write data to the SQL database, we need to establish access to the vSphere vCenter. This involves creating two service accounts with specific permissions.
Read-Only Access: A service account with read-only permission to the vSphere vCenter is used to retrieve VM-related information. This account allows us to gather the necessary data and update our SQL database without making any changes to the vCenter.
Tagging Admin Access: Additionally, a separate service account has been set up with "Tagging Admin" permissions. This enables us to access the vCenter and apply tags to Virtual Machines (VMs) for easy identification
vSphere Tags: To facilitate the tagging process, we have created three pre-defined tag categories in our vCenter:
Location: To track where VMs are deployed.
Owner: To identify who is responsible for each VM.
Operating System: To keep record of which operating system is running on each VM
By having these service accounts and tags in place, we can streamline the process of gathering data from our vSphere vCenter and updating our SQL database
Method:
Login to the vCentre and create the tag categories for Operating system, owner and location from "Tags & Custom Attributes". Later on we will use these categories to tag all VMs:

