LucD
Leadership
Leadership

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

Reply
0 Kudos
Macleud
Enthusiast
Enthusiast

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

 

aplechaty
Enthusiast
Enthusiast

OK here is the code I have the new tag commented. 

For $Tag do I replace with $chosenBackupTag or the $BackupTag variable I am thinking the first.

Reply
0 Kudos
LucD
Leadership
Leadership

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

View solution in original post

aplechaty
Enthusiast
Enthusiast

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

Reply
0 Kudos
aplechaty
Enthusiast
Enthusiast

Her are my tags in vCenter.

Reply
0 Kudos
LucD
Leadership
Leadership

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

aplechaty
Enthusiast
Enthusiast

Spoiler
 

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

Reply
0 Kudos
LucD
Leadership
Leadership

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

aplechaty
Enthusiast
Enthusiast

I just commented out those lines completely and it worked. Thank you for all the assistance. It is greatly appreciated.

cool_breeze
Enthusiast
Enthusiast

Glad you got the help you needed. This is a great community.

Reply
0 Kudos