I'm struggling to figure out how to list the VMs that don't have a tag named "LINUX" or "WINDOWS" in "OS" category.
For now, i can find all VMs without a tag, like that :
Get-VM | Where-Object{(Get-TagAssignment $_) -eq $null)}
But, this result can be false because VMs can have another tag (not LINUX or WINDOWS).
Any idea is welcome.
Regards
A light bulb moment.
I was forgetting the ones without any tags of course.
Get-VM -PipelineVariable vm |
ForEach-Object -Process {
$tags = Get-TagAssignment -Entity $_
if($tags -eq $null){$vm}
else{
if($tags.Tag.Category.Name -notcontains $tgtCat){
$vm
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this.
Note that this will only check VMs that have a tag in the OS category.
$tagNames = 'LINUX','WINDOWS'
Get-TagAssignment -Category $tagCategory | where {$tagNames -notcontains "$($_.Tag.Name)"}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Use RVtool and get detail in excel and later on you can compare which VM has a Tag or not with filtering either it's false or not.
I didn't try with script so far , so not sure on that.
unfortunely, it doesn't work as expected.
No VM are displayed whereas, i'm sur that i've at least one with no tag.
To list all VMs that match the category "OS", i did that:
Get-TagAssignment -Category $tagCategory
and the result is that:
Uid : /VIServer=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Tag : OS/WINDOWS
Entity : PRD-webxxxxxxx
Id : com.vmware.cis.tagging.TagAssociationModel
Name : com.vmware.cis.tagging.TagAssociationModel
So, the mistake is probably in the "where-object" condition. But, i'm beginner, and don't know how to fix that.
Just to clarify, you also want to list the VMs that do not have a tag in the OS tag category assigned?
The above script only lists the VMs that have a tag in the OS tag category that is not LINUX or WINDOWS.
If that is the case, you probably want to run something like this
Get-TagAssignment | where{$SpecificTags -notcontains $_.Tag.ToString()}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
sorry, i probably bad explained.
i want, all my VMs to be tagged with an OS tag (Linux or Windows).
So, to check, i want to list all VMs that don"t have OS tag.
But, this VMs can have another tag like backup.
To summ up:
CATEGORY | TAG |
---|---|
OS | LINUX |
OS | WINDOWS |
BACKUP | BACKUP-PRD |
BACKUP | BACKUP-OFF |
regards.
$SpecificTags = 'Cat1/Tag1','Cat1/Tag3'
Get-TagAssignment | where{$SpecificTags -notcontains $_.Tag.ToString()}
this script list all VMs that have another tag than OS category but it don"t say if the tag OS category is set or not.
i explain:
the VM PRD-001 has 2 tags:
- OS/LINUX
- BACKUP/BACKUP-PRD
the VM PRD-002 has only one tag:
- BACKUP/BACKUP-PRD
your script will display PRD-001 and PRD-002 because, they have BACKUP/BACKUP-PRD tag.
i want a script that display only PRD-002 because no OS tag is assigned.
Those were sample tags, in your case it should probably be
Get-TagAssignment | where{$SpecificTags -notcontains $_.Tag.ToString()}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I want something like that :
foreach VM
get tags of the VM
foreach tag of tags
get category of tag
is this tag category == OS
if yes, NEXT tag
if not, display in the list
i hope, it will help
Those were sample tags, in your case it should probably be
$SpecificTags = 'OS/LINUX','OS/WINDOWS'
Get-TagAssignment | where{$SpecificTags -notcontains $_.Tag.ToString()}
yes, thank you Luc, i'd well understood.
i had already replace samples tags by mines.
but i confirmed that it list tags not VM.
So, a VM that have a tag BACKUP/BACKUP-PRD will be displayed.
The goal is to display VMs that don't have OS/LINUX or OS/WINDOWS.
😉
Something like this you mean?
Get-TagAssignment |
Group-Object -Property {$_.Entity.Name}|
ForEach-Object -Process {
if($_.Group.Tag.Category.Name -notcontains $tgtCat){
$_.Name
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
we are close to the good answer i think.
when i run your script, it display nothing.
but, if i add an "else" like that :
if($_.Group.Tag.Category.Name -notcontains $tgtCat){
$_.Name
}
else {
$_.Name
}
all VMs are displayed expect the target one (the VM with no OS category tag)
That can only mean that all your VMs have a tag in the OS tagcategory but not with the tags LINUX or WINDOWS.
Which you said is not the case.
I give up.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
sorry for consumed your time.
But, in the web GUI, i have a test VM with no tag at all.
So, i wanted a script that warn me of a VM "OS untagged".
Never mind.
A light bulb moment.
I was forgetting the ones without any tags of course.
Get-VM -PipelineVariable vm |
ForEach-Object -Process {
$tags = Get-TagAssignment -Entity $_
if($tags -eq $null){$vm}
else{
if($tags.Tag.Category.Name -notcontains $tgtCat){
$vm
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
YES !!! it works !!!
thank you very much Luc for the time spent and your availability.