Yet another role added to my Job description, Now I am managing our Virtual Servers. So true to form I’ve dived headfirst into powershell / powercli to see what I can do. Some of the posts coming up on this will likely be similar to others around the web as I reinvent the wheel while I learn. But you never know one of you might find me 1st or I might do it in a way that suits you better.
First up is a simple script to read data from an xml file and then update custom attributes on the vmhosts.
Custom Attributes Required:
AssetTag ServiceTag PurchaseDate WarantyExpires
Here is the xml format to copy
<updatehostcustom>
<VMHost>
<name>vmhost1</name>
<AssetTag>ABCD123</AssetTag>
<ServiceTag>A12B3C4</ServiceTag>
<PurchaseDate>2009-04-23</PurchaseDate>
<WarrantyExpires>2012-04-23</WarrantyExpires>
</VMHost>
</updatehostcustom>
Due to WordPress formatting issues I have removed the text in favour of a download.
Download script here: [Download not found]
The above script is giving me an error. I defined the VMhost.xml file with the AssetTag & service tag. When I run the script it gives me the below error message. Any ideas?
Get-VMHost : Cannot validate argument on parameter ‘Name’. The argument is null
or empty. Supply an argument that is not null or empty and then try the comman
d again.
At C:\temp\scripts\asset.ps1:9 char:35
+ Set-Annotation -Entity (get-vmhost <<<< $_.Name) -CustomAttribute AssetTag –
Value $_.AssetTag
+ CategoryInfo : InvalidData: (:) [Get-VMHost], ParameterBindingV
alidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
ation.ViCore.Cmdlets.Commands.GetVMHost
Get-VMHost : Cannot validate argument on parameter 'Name'. The argument is null
or empty. Supply an argument that is not null or empty and then try the comman
d again.
At C:\temp\scripts\asset.ps1:10 char:35
+ Set-Annotation -Entity (get-vmhost <<<< $_.Name) -CustomAttribute ServiceTag
-Value $_.ServiceTag
+ CategoryInfo : InvalidData: (:) [Get-VMHost], ParameterBindingV
alidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
ation.ViCore.Cmdlets.Commands.GetVMHost
Line 5 is erronous (wordpress has garbbled it.):
1$xml = Get-Content $xmlFile
should be
[ xml ] $xml = Get-Content $xmlFile
^ Remove spaces inside brackets
I’ll fix it now.
Can you try that and see if it works.
Thanks for the reply. Even after removing the erronous character, I am getting the error below.
Get-VMHost : Cannot validate argument on parameter ‘Name’. The argument is null
or empty. Supply an argument that is not null or empty and then try the comman
d again.
At C:\temp\scripts\asset.ps1:9 char:35
+ Set-Annotation -Entity (get-vmhost <<<< $_.Name) '-CustomAttribute AssetTag
-Value $_.AssetTag
+ CategoryInfo : InvalidData: (:) [Get-VMHost], ParameterBindingV
alidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
ation.ViCore.Cmdlets.Commands.GetVMHost
This is the content of my VMHost.xml File-
Hostname
ABCD123
A12B3C4
This is the content of my AssetTag.ps1 File-
Connect-VIServer -Server adm-vc05-wh
$xmlFile = “VMHost.xml”
#Get the File
$xml = Get-Content $xmlFile
#Populate the Fields
$xml.updatehostcustom.vmhost | foreach {
Set-Annotation -Entity (get-vmhost $_.Name) ‘-CustomAttribute AssetTag -Value $_.AssetTag
Set-Annotation -Entity (get-vmhost $_.Name) ‘-CustomAttribute ServiceTag -Value $_.ServiceTag
}
I am running the Asset.ps1 file to add the AssetTage & ServiceTag in the annotation fields for the host.
Content of the VMHost.xml file.
Hostname
ABCD123
A12B3C4
Please see my last update again, wordpress screwed up the response.
Now I am seeing this error. I fixed the line to
$xml = Get-Content $xmlFile
Set-Annotation : A positional parameter cannot be found that accepts argument ‘
-CustomAttribute AssetTag -Value $_.AssetTag
Set-Annotation -Entity (get-vmhost $_.Name) ‘.
At C:\temp\scripts\asset.ps1:9 char:15
+ Set-Annotation <<<< -Entity (get-vmhost $_.Name) '-CustomAttribute AssetTag
-Value $_.AssetTag
+ CategoryInfo : InvalidArgument: (:) [Set-Annotation], Parameter
BindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,VMware.VimAutomation
.ViCore.Cmdlets.Commands.SetAnnotation
I found the issue. I think it’s there was another erronous character ‘ in the script which was added by wordpress. It works like a charm now. I can do the same for adding annotations for the Virtual machine attributes?
I’ve changed the text example to an upload as everything I tried resulted in corruption.
Thanks for pointing out the issue.
The script was intended as a here is how to update X attributes all you need do is a bit of tweaking,
change Get-VMHost to Get-VM (I’d change the XML for clarity too) and set the appropriate annotations.
Thanks Ryan! The updated script works perfectly!