I have a script currently for deploying virtual machines. What I am looking to do is prompt the user with a list of Tags to select from as follows.
$Tags = "DEV-2-Week-Linux","DEV-4-Week-Linux","PROD-2-Week-Linux","PROD-2-Week-Windows"
So as the script runs I want the user to be prompted to select one of the tags. They make their selection and the script continues on. I then need the script at some point to assign the selected tag to the VM as it is created.
I am having a hard time figuring out how to actually set the tag on the newly created VM. Any help is greatly appreciated. Thanks in advance.
Yes, you have to replace $tag with the variable where you stored the tags you want to assign.
It looks like the $vm variable is empty, I would need to see your actual code again to analyse
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try this. But if there is more than one VM with that name in vCenter, it will assign a tag to all VMs with that name.
$VmTag = Get-Tag "TagName"
Get-VM -Name $ChosenName | New-TagAssignment -Tag $VmTag
You are not storing the object returned by New-VM in a variable, and, yes, you have to use the variable where you define the tags instead of $tag.
$vm = New-VM -Name $ChosenName -Template $ChosenTemplate -OSCustomizationSpec $ChosenCustomization -Datastore $ChosenDatastore -Location $ChosenFolder -NetworkName $ChosenNetwork -VMHost $ChosenHost -ResourcePool $ChosenCluster
New-TagAssignment -Entity $vm -Tag $BackupTag
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
When i add what LucD suggested in his last post, i get the foolowing.
New-TagAssignment : 7/21/2022 12:00:20 PM New-TagAssignment com.vmware.vapi.std.errors.unauthenticated {'messages':
[com.vmware.vapi.std.localizable_message {'id': com.vmware.vapi.endpoint.method.authentication.required,
'default_message': Authentication required., 'args': [], 'params': , 'localized':}], 'data': , 'error_type':
UNAUTHENTICATED, 'challenge': Basic realm="VAPI endpoint",SIGN
realm=6375ea75a6f71b1c60bdf03344aadb65017b2991,service="VAPI endpoint",sts="https://EXPVCENTER.quadax.net/sts/STSService/vsphere.local"} At C:\Users\adplechaty\PowerShell-Scripts\vCenter Server Deployment\7-24-2022_Deployment.ps1:325 char:13 + New-TagAssignment -Entity $vm -Tag $ChosenBackupTag + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [New-TagAssignment], CisException
+ FullyQualifiedErrorId : VMware.VimAutomation.ViCore.Impl.V1.Service.Tagging.Cis.TaggingServiceCisImpl.GetTag.Err
or,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
New-TagAssignment : 7/21/2022 12:00:20 PM New-TagAssignment Could not find Tag with name 'DEV-4-Week-Windows'.
At C:\Users\adplechaty\PowerShell-Scripts\vCenter Server Deployment\7-24-2022_Deployment.ps1:325 char:13
+ New-TagAssignment -Entity $vm -Tag $ChosenBackupTag
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (DEV-4-Week-Windows:String) [New-TagAssignment], VimException
+ FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdle
ts.Commands.Tagging.NewTagAssignment
New-TagAssignment : 7/21/2022 12:00:20 PM New-TagAssignment Value cannot be found for the mandatory parameter Tag
At C:\Users\adplechaty\PowerShell-Scripts\vCenter Server Deployment\7-24-2022_Deployment.ps1:325 char:13
+ New-TagAssignment -Entity $vm -Tag $ChosenBackupTag
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-TagAssignment], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTag
The 1st error (unauthenticated) is a known issue, try stopping/starting your PowerShell/PowerCLI session.
There is also a tag () that is not found, it might be the case in the name.
In your vCenter it shows Dev-4-Week-Windows, not DEV-4-Week-Windows
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This is so frustrating but teh Tag is now working Thank you so very Much! Now suddenly for somereason I am getting the following. This didn't happen before updating what you sent me for the Tag.
Get-OSCustomizationSpec : 7/21/2022 12:30:53 PM Get-OSCustomizationSpec Could not find Customization Specification
with name 'Temp'.
At C:\Users\adplechaty\PowerShell-Scripts\vCenter Server Deployment\7-24-2022_Deployment.ps1:314 char:13
+ Get-OSCustomizationSpec -Name Temp | Remove-OSCustomizati ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Temp:String) [Get-OSCustomizationSpec], VimException
+ FullyQualifiedErrorId : Common_CommonUtil_FilterCollection_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdlets.Co
mmands.GetOSCustomizationSpec
That seems to say there is no OSCustomizationSpec by that name.
Do you see it when you do
Get-OSCustomizationSpec
In fact, if that OSCustomization might not be there in some cases, you could perhaps better do
Get-OSCustomizationSpec -Name Temp -ErrorAction SilentlyContinue | Remove-OSCustomizationSpec -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I just commented out those lines completely and it worked. Thank you for all the assistance. It is greatly appreciated.
Glad you got the help you needed. This is a great community.
