VMware Cloud Community
robbyd2
Contributor
Contributor
Jump to solution

Script get information from excel to Vm description

Hello,

i'm interesting to do a particular script with powershell ( if it is possible) who can get a data from my excel file and put it in custom field in vm description.


In particular, i have a lot of vm ( about 800 ) and i have all data of them ( description , service installed etc..)  in a file excel and i would like put them into a custom field created with Vsphere client. Is it possible with Powershell???

I searched on web but i see only export information from vsphere to excel... but not from excel to vcenter/esx.

The version of vsphere is 4.1 .


Thank you

0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

If you have a .csv file like:

"VM","Name","Value"
"scomp0016","CreatedBy","Unknown"
"scomp0016","CreatedOn","05/25/2009 10:26:30"
"scomp0016","Function","Print Server"
"scomp0016","Owner","Research"

then you can use the following script to import the annotations into vSphere:

Import-Csv -Path VMAnnotations.csv | Where-Object {$_.Value} | ForEach-Object {
  Get-VM $_.VM | Set-Annotation -CustomAttribute $_.Name -Value $_.Value
}

The description field is not an annotation. You can set the description field of a VM with the Set-VM -Description command.

Regards, Robert

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
3 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

If you have a .csv file like:

"VM","Name","Value"
"scomp0016","CreatedBy","Unknown"
"scomp0016","CreatedOn","05/25/2009 10:26:30"
"scomp0016","Function","Print Server"
"scomp0016","Owner","Research"

then you can use the following script to import the annotations into vSphere:

Import-Csv -Path VMAnnotations.csv | Where-Object {$_.Value} | ForEach-Object {
  Get-VM $_.VM | Set-Annotation -CustomAttribute $_.Name -Value $_.Value
}

The description field is not an annotation. You can set the description field of a VM with the Set-VM -Description command.

Regards, Robert

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid that this will only work when the Custom Attribute already exists.

Better would be to test for the existance and create the Custom Attribute if it doesn't exist.

The code becomes

Import-Csv -Path VMAnnotations.csv | Where-Object {$_.Value} | ForEach-Object {
    $attr = Get-CustomAttribute -Name $_.Name -ErrorAction SilentlyContinue
   
if(!$attr){         New-CustomAttribute -Name $_.Name -TargetType VirtualMachine
    }     Get-VM $_.VM | Set-Annotation -CustomAttribute $_.Name -Value $_.Value }


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

RvdNieuwendijk
Leadership
Leadership
Jump to solution

That's a nice improvement Luc.

My script came from my export/import annotations scripts. So the annotations already existed.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos