How do I go about doing things like:
1) Assign this tag if....connected to these portgroups
2) Assign this tag if....within this folder
These both follow somewhat the same layout.
1) Something like this.
Note that this is very basic. There is, for example, no test if that Tag was already assigned.
$tgtPG = 'PG'
$tagCategory = 'cat'
$tagName = 'whatever'
$tag = Get-Tag -Name $tagName -Category $tagCategory
if(Get-VM -Name $vmName -PipelineVariable vm | Get-NetworkAdapter | where{$_.NetworkName -eq $tgtPG}){
New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false
}
2) Same logic here
$folderName = 'MyFolder'
$tagCategory = 'cat'
$tagName = 'whatever'
$tag = Get-Tag -Name $tagName -Category $tagCategory
if(Get-VM -Name $vmName -PipelineVariable vm | where{$_.Folder.Name -eq $folderName}){
New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I am looking at your port group tagging and I would like to have it scan the entire VM collection and anything attached to these portgroups get this tag, so I altered it, but I am missing something on how it gets all the VMs, but needs to individually tag. Here is what I have:
$tgtPG = 'Corp-61'
$tagCategory = 'IP Use'
$tagName = 'Internal IP'
$tag = Get-Tag -Name $tagName -Category $tagCategory
if(Get-VM | Get-NetworkAdapter | where{$_.NetworkName -eq $tgtPG}){
New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false
}
I really appreciate this!
Try with a nested Where-clause
$tagCategory = 'IP Use'
$tagName = 'Internal IP'
$tag = Get-Tag -Name $tagName -Category $tagCategory
Get-VM -PipelineVariable vm | where{Get-NetworkAdapter -VM $_ | where{$_.NetworkName -eq $tgtPG}} |
ForEach-Object -Process {
New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I have been testing this and it mostly works, but the replicas that I have generated with veeam are attached to the portgroup, but the NICs are disconnected so the script doesn't see them. Is there a way to allow that?
Is there a way to recognise the replica and find the original VM for which they are a replica?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sure, they are <IDENTICAL_VM NAME>_replica
Try like this
$tagCategory = 'IP Use'
$tagName = 'Internal IP'
$tag = Get-Tag -Name $tagName -Category $tagCategory
Get-VM -PipelineVariable vm | where{Get-NetworkAdapter -VM $_ | where{$_.NetworkName -eq $tgtPG}} |
ForEach-Object -Process {
New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false
Get-VM -Name "$($vm.Name)_replica" -ErrorAction SilentlyContinue |
New-TagAssignment -Tag $tag -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I am trying to apply tags via folders, but they are not searching recursively and not sure how to make that work.
### Mgmt ###
$folderName = 'Mgmt'
$tagCategory = 'Admin'
$tagName = 'Team Tag'
$tag = Get-Tag -Name $tagName -Category $tagCategory
Get-VM -PipelineVariable vm | where{$_.Folder.Name -eq $folderName} |
ForEach-Object -Process {
New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false
Get-VM -Name "$($vm.Name)_replica" -ErrorAction SilentlyContinue |
New-TagAssignment -Tag $tag -Confirm:$false
Do you mean you have other folders below the folder 'Mgmt'?
And VMs in those folders should also be tagged?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Correct!
It's not pretty, but try like this
$folderName = 'Mgmt'
$tagCategory = 'Admin'
$tagName = 'Team Tag'
$tag = Get-Tag -Name $tagName -Category $tagCategory
$folder = Get-Folder -Name $folderName
$folders = @($folder)
$folders += Get-Folder -Location $folder
Get-VM -Location $folders -PipelineVariable vm
ForEach-Object -Process {
New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false
Get-VM -Name "$($vm.Name)_replica" -ErrorAction SilentlyContinue |
New-TagAssignment -Tag $tag -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference