{"id":835,"date":"2017-09-28T14:56:40","date_gmt":"2017-09-28T13:56:40","guid":{"rendered":"http:\/\/virtualworlduk.co.uk\/?p=835"},"modified":"2017-09-28T14:56:40","modified_gmt":"2017-09-28T13:56:40","slug":"vsphere-custom-attributes-powercli","status":"publish","type":"post","link":"https:\/\/www.virtualworlduk.co.uk\/wordp\/vsphere-custom-attributes-powercli\/","title":{"rendered":"vSphere Custom Attributes &#8211; PowerCLI"},"content":{"rendered":"<p>I know that a lot of things have been discussed about Custom Attributes and Tags across various blogs but I thought that I would just share some scripts that I have been using for a particular scenario that other people may also run into.<\/p>\n<p>In the environment I was looking after, we had over 1000 virtual machines which had been deployed over the past few years (most of them just being deployed with templates and no real automation involved).\u00a0 These virtual machines were not owned by a single person or team but multiple people across multiple teams, a scenario seen at most companies.\u00a0 In the past we had utilised the &#8216;Notes&#8217; field of the virtual machine to list some simple information about the virtual machine, such as the owner, email address etc.<\/p>\n<p>This method was not great as all of the information was contained in a single field and therefore searching on the information using scripts etc. proved to be difficult.\u00a0 Previously, we had also looked at utilising Custom Attributes in earlier versions of vSphere but this proved to be tricky to view or manage as the Custom Attributes did not appear on the summary screen for the VM (especially in the Web Client).<\/p>\n<p>Thankfully, in vSphere 6.5, Custom Attributes can now be fully viewed and managed directly from the vSphere Web Client&#8230; meaning that utilising Custom Attributes becomes a real possibility now.<\/p>\n<p>&nbsp;<\/p>\n<p>I therefore revisited Custom Attributes and utilised the following scripts to help create the Custom Attribute names, update the virtual machines from a CSV file and also to export the virtual machines including the Custom Attributes.\u00a0 I have to admit that I am not the original author of these scripts although I have tweaked them and unfortunately I cannot remember who the various authors are (therefore I apologise to the original authors for not being able to give them the full credit they deserve).\u00a0 I&#8217;m sure that these scripts can be improved upon but they do what I need them to do.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Create Custom Attributes<\/strong><\/p>\n<p>The first script is a simple script to create a number of Virtual Machine Custom Attributes, the ones listed were specific to my requirements but you can adjust these to your requirements.\u00a0 Please remember to change the <em><span style=\"color: #ff0000;\">***vCenterName***<\/span><\/em> to your vCenter name.<\/p>\n<pre><span style=\"color: #3366ff;\">connect-viserver<\/span>\u00a0<em><span style=\"color: #ff0000;\">***vCenterName***<\/span><\/em>\n\n<span style=\"color: #3366ff;\">New-CustomAttribute -TargetType \"VirtualMachine\" -Name \"VMOwner\"<\/span>\n<span style=\"color: #3366ff;\">New-CustomAttribute -TargetType \"VirtualMachine\" -Name \"OwnerEmail\"<\/span>\n<span style=\"color: #3366ff;\">New-CustomAttribute -TargetType \"VirtualMachine\" -Name \"OwnerADLocation\"<\/span>\n<span style=\"color: #3366ff;\">New-CustomAttribute -TargetType \"VirtualMachine\" -Name \"BusinessReason\"<\/span>\n<span style=\"color: #3366ff;\">New-CustomAttribute -TargetType \"VirtualMachine\" -Name \"Application\"<\/span>\n<span style=\"color: #3366ff;\">New-CustomAttribute -TargetType \"VirtualMachine\" -Name \"ServerType\"<\/span>\n\n<span style=\"color: #3366ff;\">Disconnect-VIServer -Server<\/span> <span style=\"color: #ff0000;\"><em>***vCenterName***<\/em><\/span> <span style=\"color: #3366ff;\">-Confirm:$False<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Import Custom Attribute Values<\/strong><\/p>\n<p>The second script takes an input from a csv file and updates the relevant virtual machine Custom Attributes with the additional details from the csv file.\u00a0 In the csv file there is a column called VMName (housing the full virtual machine name) with additional columns for each of the Custom Attributes.\u00a0 Once again, remember to change the<span style=\"color: #ff0000;\"> ***vCenterName***<\/span> to your vCenter name and the <span style=\"color: #ff0000;\">***CSVfileLocation***<\/span> to the location of the csv file.<\/p>\n<pre><span style=\"color: #3366ff;\">connect-viserver<\/span> <em><span style=\"color: #ff0000;\">***vCenterName***\n<\/span><\/em><span style=\"color: #3366ff;\">$FileList = \"<\/span><span style=\"color: #ff0000;\">***CSVfileLocation***<\/span><span style=\"color: #3366ff;\">\"\n$VMList=Import-CSV $FileList\nForEach($Line in $VMList)<\/span>\n<span style=\"color: #3366ff;\"> {<\/span>\n<span style=\"color: #3366ff;\"> Get-Vm $Line.VMName | Set-Annotation -CustomAttribute \"VMOwner\" -Value $Line.VMOwner<\/span>\n<span style=\"color: #3366ff;\"> Get-Vm $Line.VMName | Set-Annotation -CustomAttribute \"OwnerEmail\" -Value $Line.OwnerEmail<\/span>\n<span style=\"color: #3366ff;\"> Get-Vm $Line.VMName | Set-Annotation -CustomAttribute \"OwnerADLocation\" -Value $Line.OwnerADLocation<\/span>\n<span style=\"color: #3366ff;\"> Get-Vm $Line.VMName | Set-Annotation -CustomAttribute \"BusinessReason\" -Value $Line.BusinessReason<\/span>\n<span style=\"color: #3366ff;\"> Get-Vm $Line.VMName | Set-Annotation -CustomAttribute \"Application\" -Value $Line.Application<\/span>\n<span style=\"color: #3366ff;\"> Get-Vm $Line.VMName | Set-Annotation -CustomAttribute \"ServerType\" -Value $Line.ServerType<\/span>\n<span style=\"color: #3366ff;\"> }\n<\/span><span style=\"color: #3366ff;\">Disconnect-VIServer -Server<\/span> <em><span style=\"color: #ff0000;\">***vCenterName*** <\/span><\/em><span style=\"color: #3366ff;\">-Confirm:$False<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Export Custom Attributes<\/strong><\/p>\n<p>The final script scans over the vCenter server and exports all of the virtual machines with their Custom Attributes to a csv file (to allow you to check for any missing data or as a base to update the information before importing it again).\u00a0 As in the previous scripts, please remember to modify the <span style=\"color: #ff0000;\">***vCenterName***<\/span> and <span style=\"color: #ff0000;\">***CSVfileLocation***<\/span> with your information.<\/p>\n<pre><span style=\"color: #3366ff;\">connect-viserver\u00a0<\/span><em><span style=\"color: #ff0000;\">***vCenterName*** <\/span><\/em>\n\n<span style=\"color: #3366ff;\">$Report = @()<\/span>\n\n<span style=\"color: #3366ff;\">Get-VM | foreach{<\/span>\n\n<span style=\"color: #3366ff;\">$Summary = \"\" | Select VMName<\/span>\n\n<span style=\"color: #3366ff;\">$Summary.VMName = $_.Name<\/span>\n\n<span style=\"color: #3366ff;\">$_ | Get-Annotation | foreach{<\/span>\n\n<span style=\"color: #3366ff;\">Add-Member -InputObject $Summary -MemberType NoteProperty -Name $_.Name -Value $_.Value<\/span>\n\n<span style=\"color: #3366ff;\">}<\/span>\n\n<span style=\"color: #3366ff;\">$Report += $Summary<\/span>\n\n<span style=\"color: #3366ff;\">}<\/span>\n\n<span style=\"color: #3366ff;\">$Report | Export-Csv -Path\u00a0<\/span><span style=\"color: #ff0000;\">***CSVfileLocation***\u00a0<\/span><span style=\"color: #3366ff;\">-NoTypeInformation<\/span>\n\n<span style=\"color: #3366ff;\">Disconnect-VIServer -Server\u00a0<\/span><em><span style=\"color: #ff0000;\">***vCenterName***\u00a0<\/span><\/em><span style=\"color: #3366ff;\">-Confirm:$False<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">I hope that this proves to be useful for other people.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I know that a lot of things have been discussed about Custom Attributes and Tags across various blogs but I thought that I would just share some scripts that I have been using for a particular scenario that other people may also run into. In the environment I was looking after, we had over 1000 virtual machines which had been [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":735,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[9],"tags":[],"class_list":["post-835","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vmware"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/posts\/835","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/comments?post=835"}],"version-history":[{"count":0,"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/posts\/835\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/media?parent=835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/categories?post=835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/tags?post=835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}