Hello @LucD,
I am currently using the script below to tag the VMs on the vCenter. I obtain my import file from ServiceNow, and my goal is to tag all the VMs using the hostname of the VM. However, I am encountering an issue where I am unable to tag the VMs on the vCenter if the VM name on the vCenter is in FQDN format.
For example, the hostname of the VM in the CMDB and my import file is "server1", but the VM name on the vCenter is "server1.vmware.com". In this scenario, my script is unable to find the VM "server1" on the vCenter. How can I resolve this issue?
=======================
Connect-viserver lab.vc.com -user anirudh -pass chali@123
# Import CSV of VM Tags (In the format VMName,Tag,Tag,Tag,etc with Column headers for Tag Categories)
Write-host "Importing CSV File now..."
$VMWareInfo = Import-CSV D:\Scripts\SNOWInfo.csv
# Gather all VMs from vCenter to find ones missing tags
Write-Host "Gathering all VMs"
$AllVMs = Get-VM | select name | sort name
# Get the header names to use as tag category names
Write-Host "Gathering Tag Categories"
$TagCatNames = $VMWareInfo | Get-Member | Where {$_.MemberType -eq "NoteProperty"} | Select -Expand Name |Sort-Object
$TagCatNames = $TagCatNames.trim()
# Get the currently assigned tags for the VMs
Write-Host "Gathering Current VM Tag Information"
$CurrentTagAssignment = Get-Tagassignment | Select Entity, tag |Sort-Object entity
# Create the tag category if it doesnt exist
Foreach ($CatName in ($TagCatNames | Where {$_ -ne "Name"})) {
if (Get-TagCategory $CatName) {Write-Host "Entry" $CatName "exists, Skipping..."}
if (-Not (Get-TagCategory $CatName -ea SilentlyContinue)) {
Write-Host "Creating Tag Category $CatName"
New-TagCategory -Name $CatName -Description "SNOW Category"
}
}
# Create tags under the tag categories
Foreach ($CatName in ($TagCatNames | Where {$_ -ne "Name"})) {
$Uniquetags= $VMWareInfo |select -Expand $CatName.Trim() |sort-object -Unique
# Assign the tags to the categories
Foreach ($TagName in $UniqueTags) {
# Error checking for blank or null tags
if ($TagName -eq "" -or $null) {
Write-Host "Blank Entry: " $CatName "-" $TagName}
# Adding new tags to each category
elseif (-Not (Get-Tag -Category $Catname -Name $TagName -ea SilentlyContinue)) {
Write-Host "Creating New Tag under $CatName of $TagName"
New-Tag -Name $TagName -Category $CatName -Description "SNOW Tag"
}
# Skip adding if the tag already exists in the category
elseif (Get-Tag -Category $CatName -Name $TagName) {Write-Host "Tag Entry Already Exists: " $CatName "-" $tagName "Skipping..."
}
}
}
# Checking tags against currently entered tags
# Checking against each category
Foreach ($CatName in ($TagCatNames | Where {$_ -ne "Name"})) {
# Creating list of tags to be checked for current category
$Uniquetags= $VMWareInfo |select -Expand $CatName.Trim() |where {$_ -notlike $null}| sort-object -Unique
# Adding line for blank entries
$Uniquetags+= ""
# Checking each tag
foreach ($TagName in $uniquetags) {
Write-Host "Working on" $CatName"/"$TagName
# Listing all VMs that have the currently selected tag
$VMList=$VMwareinfo | Where {$_.($CatName) -eq $TagName}
foreach ($VMName in $VMList){
# Find all the VMs that the current tag applies to
$CurrentVMTags= $CurrentTagassignment |select entity,tag| where { $_.entity -like $VMName.name -and $_.tag.Category -like $CatName}
# If tag is blank on the spreadsheet
If($VMName.$CatName -like "" -or $VMName.$CatName -like $null){
# Check to see if the is blank on the VM
if ($CurrentVMTags -like "" -or $CurrentVMTags -eq $null){
Write-Host "Blank Entry for Category" $CatName "on VM" $VMName.name "Ignoring"
}
# If the tag was erroneously written to vCenter
elseif ($CurrentVMTags -notlike "" -or $CurrentVMTags -notlike $null){
Write-Host "Tag erroniously written to VM" $VMName.Name "Removing incorrect entry"
Get-TagAssignment -entity $VMName.Name -Category $CatName |Remove-Tagassignment -Confirm:$false
}
}
# If a tag already exists in vCenter
If(($CurrentVMTags -ne "" -or $CurrentVMTags -ne $null)-and ($VMName.$catname -ne "" -or $VMname.catname -ne $null)) {
# Skip tag if it already matches
if (($currentVMTags.Tag.Category -like $catname -and $CurrentVMTags.Tag.name -like $VMname.$catname) -or ($vmname.$catname -like "" -or $vmname.$catname -like $null)) {
Write-Host "Written Tag" $CurrentVMTags.Tag "Matches" $CatName"/"$TagName for $Vmname.name "Skipping Entry..."
}
# Re-write tag if it does not match
elseif ($CurrentVMTags.Tag.name -notlike $VMname.$catname -and $currentVMTags.tag.name -ne $null -and $VMname.$catname -ne $null) {
Write-Host "Tag Mis-Match for" $VMname.Name " Tags to Write" $CatName"/"$TagName "Written Tag" $CurrentVMTags.tag.Category"/"$CurrentVMTags.tag.name
Write-Host "Removing Old Tag" $CurrentVMTags.tag.Category"/"$CurrentVMTags.tag.name
Get-TagAssignment -entity $VMName.Name -Category $CatName |Remove-Tagassignment -Confirm:$false
Write-Host "Adding New Tag" $CatName"/"$TagName "for VM" $VMName.Name
New-TagAssignment -Entity $VMName.Name -Tag $TagName
}
}
# If the tag does not exist, find out if the vm has no tags or doesn't exist
If (($CurrentVMTags -like "" -or $CurrentVMTags -eq $null) -and ($Vmname.$catname -notlike "" -or $VMname.$catname -notlike $Null)){
$VMExist=$AllVMs | where {$_.name -Like $VMName.Name}|Measure
# If the VM no longer exists in vSphere
if ($VMExist.count -eq 0) {
Write-Host $VMName.Name "Not Found in vCenter. Please Remove from Spreadsheet"
}
# If the VM exists, but has no tags assigned
elseif ($VMExist.count -eq 1){
Write-Host "VM Tag" $CatName"/"$Tagname "missing from" $VMName.name "Adding Now..."
New-TagAssignment -Entity $VMName.Name -Tag $TagName
}
# if there are multiple VMs with the same name in vSphere
elseif ($VMExist.Count -gt 1) {
Write-Host "Multiple VMs with the same name Exist. Is" $VMName.Name "a Template?"
}
}
}
}
}