PowerShell script exchange
Post Reply
digitalexpl0it
Influencer
Posts: 18
Liked: 4 times
Joined: Jul 21, 2016 1:31 am
Full Name: Derrick Rose
Contact:

VMWare Tagging auto Script

Post by digitalexpl0it » 2 people like this post

Hello,

I figure I would post this out to the world and see if anyone could use it. I have written a powershell script for VMWare that will automatically tag VM's. Create Tag Category and create Tags based on the folder structure setup in VMWare.

Full Code is found here
https://www.digitalexpl0it.com/index.ph ... powershell

This is a bit crud, and has been cleaned up on my production server but it should still work fine and the code is there to tweak to your likings. This script is setup using windows task scheduler to run every night. Any new VM's added to your clusters will automatically be tagged.
digitalexpl0it
Influencer
Posts: 18
Liked: 4 times
Joined: Jul 21, 2016 1:31 am
Full Name: Derrick Rose
Contact:

Re: VMWare Tagging auto Script

Post by digitalexpl0it » 2 people like this post

After the tags are set, use Veeam to use Tags in your backup jobs.

here is another script to check that your VM's are tagged, results output all Virtual Machines without a tag

Code: Select all

# Adds the base cmdlets
Get-Module -ListAvailable VM* | Import-Module
#Add-PSSnapin VMware.VimAutomation.Core

# Force Elevation
#param([switch]$Elevated)
function Check-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Check-Admin) -eq $false)  {
    if ($elevated) {
        # could not elevate, quit
    } else {
        Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
    }
exit
}

#Configure Script
$cServer = "VCENTER01"	# Hostname/FQDN of vCenter
$cUser = "Foo\bar"	# Username for vCenter
$cPassword = "passwordhere"	# password forvCenter user
$file = "C:\_Scripts\VerifyTags.csv"

# Connect to server
Connect-VIServer -Server  $cServer -User $cUser -Password $cPassword

#Get all VMs
$Tagged_VM = Get-VM | Where{Get-TagAssignment -Entity $_} | Select Name,@{N="Tag";E={(Get-TagAssignment $_.name).tag}} #| Export-Csv $file

foreach($tags in $Tagged_VM) {
	if($tags.Tag -eq "") {
		$Results += "VM Name: " + $tags.Name + "`n"
	} else {
		#$Results = "0"
	}
}

write-host $Results
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 8 guests