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.
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
You can use the New-TagAssignment cmdlet for that.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks, I am looking for the exact string that would assign the tag selected. I'm not quite sure how to format the command using a pulled in variable? Also this script creates the Vm it doesn't already exist so how do I add an entity that is yet to be created?
Here is my script below, as you can see I added the Backup tag variable now I just need the proper command to assign the selected tag to the VM.
On a separate note, I am also getting an error on line 130 which I have Italics below.
You can not add a Tag to "... entity that is yet to be created"
Not sure how you coded the creation of the VM, but a New-VM cmdlet returns an object which you can then use on the New-TagAssigment cmdlet as the value for the Entity parameter.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Here is the existing script. I added the Backup type variables just need to know if I can add the tag at some point so we don't have to do so after the fact.
I added the script here yesterday but it is gone. I was hoping at the end of the script the tag could be assigned just not sure how to format the command to propagate the selected variable.
Without seeing your code it is difficult to help you.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If you store the object returned by your New-VM into a variable, you can use the New-TagAssignment cmdlet
Not sure what the actual issue is you are having.
$vm = New-VM -Name $ChosenName -Template $ChosenTemplate -OSCustomizationSpec $ChosenCustomization -Datastore $ChosenDatastore -Location $ChosenFolder -NetworkName $ChosenNetwork -VMHost $ChosenHost -ResourcePool $ChosenCluster
New-TagAssignment -Entity $vm -Tag $tag
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Just to be clear do I remove the | Out-Null from the command below and then add the New-Tag=Assignment on the next line or do i leave the | Out-Null?
Since the idea is to capture the object returned by New-VM you should drop the Out-Null
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ugh I edited the script and added nettagassignment but now it is breaking higher up where it wasn't before. I attached the updated script. Thanks for the help by the way.
At C:\Users\myself\PowerShell-Scripts\vCenter Server Deployment\VMDeployment.ps1:67 char:68
+ ... .DESCRIPTION The function will retrieve a folder by it's path.
+ ~
Unexpected token 's' in expression or statement.
At C:\Users\adplechaty\PowerShell-Scripts\vCenter Server Deployment\VMDeployment.ps1:115 char:13
+ }
+ ~
Unexpected token '}' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
You have a typo in line 42, that should be
$BackupTag = "DomainControllers","DEV-2-Week-Linux","DEV-2-Week-Windows","DEV-4-Week-Linux","DEV-4-Week-Windows","PROD-2-Week-Linux","PROD-2-Week-Windows","PROD-4-Week-Linux","PROD-4-Week-Windows"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Still getting same errors as the previous post.
I don't.
That you change that single quote to a double quote?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I think I am in the home stretch with LucD's awesome assistance however i added the line you gave me
New-TagAssignment -Entity $vm -Tag $tag
I get the following when I run the script,
New-TagAssignment : Cannot bind argument to parameter 'Entity' because it is null.
At C:\Users\adplechaty\PowerShell-Scripts\vCenter Server Deployment\7-23-2022_Deployment.ps1:331 char:39
+ New-TagAssignment -Entity $vm -Tag $tag
+ ~~~
+ CategoryInfo : InvalidData: (:) [New-TagAssignment], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Comma
nds.Tagging.NewTagAssignment
Should I change $tag to my Variable which is $BackupTag or change it to $ChosenBackupTag?
Also do I need to enter something different than $vm?
WARNING: The 'Version' property of VirtualMachine type is deprecated. Use the 'HardwareVersion' property instead.
PowerState : PoweredOff
Version : Unknown
HardwareVersion : vmx-19
Notes :
Guest : Testing:
NumCpu : 2
CoresPerSocket : 2
MemoryMB : 6144
MemoryGB : 6
VMHostId : HostSystem-host-61
VMHost : myesx01.mydomain.net
VApp :
FolderId : Folder-group-v47
Folder : vm
ResourcePoolId : ResourcePool-resgroup-56
ResourcePool : Resources
PersistentId : 5025456a-4ac0-b3c3-79be-8cdb9a78816e
UsedSpaceGB : 100.00173801742494106292724609
ProvisionedSpaceGB : 106.21403405629098415374755859
DatastoreIdList : {Datastore-datastore-125378}
HARestartPriority : ClusterRestartPriority
HAIsolationResponse : AsSpecifiedByCluster
DrsAutomationLevel : AsSpecifiedByCluster VMSwapfilePolicy : Inherit VMResourceConfiguration : CpuShares:Normal/2000 MemShares:Normal/61440 GuestId : windows2019srvNext_64Guest CreateDate : 7/21/2022 2:12:19 PM SEVEnabled : False
BootDelayMillisecond : 0
MigrationEncryption : Opportunistic
MemoryHotAddEnabled : False
MemoryHotAddIncrement :
MemoryHotAddLimit :
CpuHotAddEnabled : False
CpuHotRemoveEnabled : False
Name : Testing
CustomFields : {[Business Division, ], [Created Date, ], [Creator, ], [Domain, ]...}
ExtensionData : VMware.Vim.VirtualMachine
Id : VirtualMachine-vm-139947
Uid : /VIServer=domain\myself@myvcenter.domain.net:443/VirtualMachine=VirtualMachine-vm-139947/
Hi.
Are you writing the result of New-VM to the $VM variable?
$vm = New-VM -Name $ChosenName -Template $ChosenTemplate -OSCustomizationSpec $ChosenCustomization -Datastore $ChosenDatastore -Location $ChosenFolder -NetworkName $ChosenNetwork -VMHost $ChosenHost -ResourcePool $ChosenCluster
Macleud I tried adding the $vm = in front of the New-Vm and it did not work. I have the script running error-free now but it still is not assigning the tag. due to my putting a # in front of the New-TagAssignment command since it was not working. if I can just get the Tag to assign now it would be perfect or close enough at least LOL.