I'd like to use PowerCLI to create tags and tag categories and then call those tags to create snapshots. I have my vm's split in to 2 categories at work, software related and Non software related. My monthly Windows patching is based on those 2 categories. Right now I have a csv file that is called from a .ps1 file that is scheduled via a batch file to create snapshots for all my servers. I'd like to use the Get-Tag command with the New-Snapshot command to create snapshots of all vm's in specific tags. Is this doable?
Also, how can I via PowerCLI pull a list of only my Windows 2003 servers? I can run get-vm | get-vmguest | select hostname,osfullname to see all my vm's and their OS version, but I'd like to call only specific OS versions and put them in their on tags. I've only just begun to use PowerCLI about 2 weeks ago so please forgive me if I'm asking newbie questions.
You can filter on the OS by using a Where-clause.
Something like this
Get-VM | Get-VMGuest
Where {$_.OSFullName -match "Windows Server 2003"} |
Select HostName,OSFullName
In the current PowerCLI build the cmdlets there are a set of tag related cmdlets Get-Tag, New-TagAssignment, Get-TagAssignment and Remove-TagAssignment
That doesn't offer all the functionality you are after I'm afraid.
And there is no access via an API either, since the tag API are private.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
To assign:
New-TagAssignment -Entity (Get-Vm $vmName) -Tag $tagName
To Retrieve:
Get-Vm -Tag $tagName
So if I create a tag called Software and apply it to my vm’s, I’d then be able to run Get-VM –Tag Software | New-Snapshot Patches and that will create a new snapshot called Patches for all my vm’s with a Tag of Software right?
That is correct
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Get-VM | Get-VMGuest | Where-Object {$_.OSFullName -like "Microsoft windows server 2003*"} | Select-Object VMName,OSFullName returns all Server 2003 vm's. So what would be the proper syntax to call all Server 2003 vm’s and pipe them in to a Tag under Categories "Operating Systems" with a Tag of "Server 2003"?
Get-VM | Get-VMGuest | Where-Object {$_.OSFullName -like "Microsoft windows server 2003*"} | New-TagAssignment -Tag “Server 2003”
This doesn't look like it's specifying any category. Do I need to manually create the category or not worry about it at all?
If i'm not mistaken, You will have to create the Tags manually and then you can then automate the assignment. I think the categories are only for organizational purposes.
So I created the Categories and the tags via the web client. The category is called Operating System and the tag is called Server 2003. I then ran Get-VM | Get-VMGuest | Where-Object {$_.OSFullName -like "Microsoft windows server 2003*"} | New-TagAssignment -Tag "Server 2003" and received the following error numerous times:
New-TagAssignment : 2/14/2014 10:57:48 AM New-TagAssignment The
specified tag with id
'InventoryServiceTag-05b71187-5625-468a-a3e3-57943a0e297e' was not found.
At line:1 char:94
+ ... rver 2003*"} | New-TagAssignment -Tag "Server 2003"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Tag:String) [New-TagAssignment]
, ViError
+ FullyQualifiedErrorId : ViCore_TaggingServiceImpl_TagAssignment_TagNotFo
und,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
Is it not possible to bulk add vm's to a tag based off their OSFullName...or is my syntax just wrong?
$myTag = Get-Tag "Server 2003"
$myVM = Get-VM | Get-VMGuest | Where-Object {$_.OSFullName -like "Microsoft windows server 2003*"}
New-TagAssignment -Tag $myTag -Entity $myVM
kunaludapi, I had already tried that and got this error:
New-TagAssignment : Cannot convert 'System.Object[]' to the type
'VMware.VimAutomation.Sdk.Types.V1.VIObjectCore' required by parameter
'Entity'. Specified method is not supported.
At line:1 char:39
+ New-TagAssignment -Tag $myTag -Entity $myVM
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-TagAssignment], Parame
terBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCor
e.Cmdlets.Commands.Tagging.NewTagAssignment
Also, if I simply do New-TagAssignment -Tag "Server 2003" -Entitiy "ServerName" I get:
New-TagAssignment : 2/14/2014 12:10:42 PM New-TagAssignment The
specified tag with id
'InventoryServiceTag-05b71187-5625-468a-a3e3-57943a0e297e' was not found.
At line:1 char:1
+ New-TagAssignment -Tag "Server 2003" -Entity Honeydew
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Tag:String) [New-TagAssignment]
, ViError
+ FullyQualifiedErrorId : ViCore_TaggingServiceImpl_TagAssignment_TagNotFo
und,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
If I do get-tag "server 2003" | select * I get:
Category : Operating System
Description : Microsoft Windows Server 2003
Id : InventoryServiceTag-05b71187-5625-468a-a3e3-57943a0e297e
Name : Server 2003
Uid : /VIServer=domain\username@vcenter:443/Tag=InventoryServiceTag-05
b71187-5625-468a-a3e3-57943a0e297e/
Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
The error says it can't find the tag with ID InventoryServiceTag-05b71187-5625-468a-a3e3-57943a0e297e
but you can see in the select * output the ID matches. As of right now I can't use PowerCLI to add any vm's to any tags.
$myTag = Get-Tag "Server 2003"
$myVM = Get-VM | Get-VMGuest | Where-Object {$_.OSFullName -like "Microsoft windows server 2003*"}
foreach ($vm in $myVM) {
New-TagAssignment -Tag "Server 2003" -Entity $VM.vm
}
Entity should be name of the object (ie vm name)
You can verify it by command -- Get-TagAssignment
Kunaludapi, same error. The specified tag with ID #### was not found.
It is working fine for me. as attached screenshot.
Run below command and check what is the result.
get-tag "Server 2003"
kunaludapi, Get-Tag "Server 2003" returns:
Name Category Description
---- -------- -----------
Server 2003 Operating System Microsoft Wind...
I think your script would work, but I think I have an underlying issue with my PowerCLI. If I run New-TagAssignment -Tag "Server 2003" -Entity "ServerName" I get:
New-TagAssignment : 2/18/2014 11:08:35 AM New-TagAssignment The
specified tag with id
'InventoryServiceTag-05b71187-5625-468a-a3e3-57943a0e297e' was not found.
At line:1 char:1
+ New-TagAssignment -Tag "Server 2003" SERVERNAME
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Tag:String) [New-TagAssignment]
, ViError
+ FullyQualifiedErrorId : ViCore_TaggingServiceImpl_TagAssignment_TagNotFo
und,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
If I run Get-Tag "Server 2003" | select * I get:
Category : Operating System
Description : Microsoft Windows Server 2003
Id : InventoryServiceTag-05b71187-5625-468a-a3e3-57943a0e297e
Name : Server 2003
Uid : /VIServer=liberty1\jmcelvoy@lb0810:443/Tag=InventoryServiceTag-05
b71187-5625-468a-a3e3-57943a0e297e/
Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
So I can't even just add a vm to a tag. PowerCLI CAN see the tag when I try to query it, but CAN'T see the tag when I try to write something to the tag. I think if I can figure this out and get this part working, I'll then be able to mass add vm's to tags. Has anyone had problems simply adding vm's to tags via PowerCLI?
Did you tried Getting the Tag like this?:
New-TagAssignment-Tag (Get-Tag "Server 2003" | select *)
Maybe the New-TagAssingnment needs na object that contains the Tag Info and not just a string as parameter... 😃
A little bit more digging and more info. I've read some posts where users have similar issues and people mentioned Linked Mode (which I'm using) and needing to specify the vCenter server. So I did that and received the same error "specified tag with ID...was not found". So I then created the exact same tag and category on my 2nd vCenter server and ran this command "new-tagassignment -server vCenterServerName -tag "Server 2003" -Entity ServerName" and got this:
New-TagAssignment : 2/18/2014 12:37:18 PM New-TagAssignment The
specified parameter 'Tag' expects a single value, but your name criteria
'Server 2003' corresponds to multiple values.
At line:1 char:1
+ New-TagAssignment -server vCenterServerName -Tag "Server 2003" -Entity ServerName
If I run run get-tag -server 2ndvCenterServerName I get:
get-tag : 2/18/2014 12:43:27 PM Get-Tag Could not find VIServer with
name '2ndvCenterServerName'.
At line:1 char:1
+ get-tag -server 2ndvCenterServerName
So PowerCLI CAN see when I created a tag on my 2nd vCenter Server with the same name, but CAN'T see the 2nd vCenter Server if I just run the Get-Tag command against it?
I think my problem revolves around Linked Mode, but I can't put my finger on the exact problem.
GuilhermeAlves wrote:
Did you tried Getting the Tag like this?:
New-TagAssignment-Tag (Get-Tag "Server 2003" | select *)
Maybe the New-TagAssingnment needs na object that contains the Tag Info and not just a string as parameter... 😃
GuilhermeAlves, I ran New-TagAssignment -Tag (Get-Tag "Server 2003") -Entity Server and got:
New-TagAssignment : Cannot convert 'System.Object[]' to the type
'VMware.VimAutomation.ViCore.Types.V1.Tagging.Tag' required by parameter
'Tag'. Specified method is not supported.
At line:1 char:24
+ New-TagAssignment -Tag (get-tag "Server 2003") -Entity ServerName
Well.. maybe trying something like this:
$myTag = Get-Tag "MyTag"
Get-VM "MyVM" | New-TagAssignment -Tag $myTag
--- Mensagem Original ---
De: "Jason McElvoy" <communities-emailer@vmware.com>
Enviado: 18 de fevereiro de 2014 15:53
Para: "GuilhermeAlves" <guilhermestela@hotmail.com>
Assunto: New message: "How to use PowerCLI to create tags to script snapshot creation?"
VMware Communities<https://communities.vmware.com/index.jspa>
How to use PowerCLI to create tags to script snapshot creation?
created by Jason McElvoy<https://communities.vmware.com/people/Jason+McElvoy> in VMware vSphere™ PowerCLI - View the full discussion<https://communities.vmware.com/message/2347176#2347176>
So Jason, i think you have to be sure of what you're giving as input to the new-tagassignment cmdlet.
Once you ensure that the input is right, you'll have the right output..
I'll try in an environment like yours to check that too.