VMware Cloud Community
afhamsani
Enthusiast
Enthusiast
Jump to solution

Set/Edit VM annotations in a mass lists from csv file thrown out error

Hi Folks,

I wanted to set or edit annotation on existing VMs at once from a csv file contains list of VM with annotation i want.

In the csv file, i put this way

Name    Server.End.Date

VM1          30/09/2019

VM2          30/09/2019

VM3          30/09/2019

With the simple script below, it thrown out errors 3 times as i have 3 VMs just for simulation purpose (plan to have long lists later).

Perhaps i wrote it wrongly, as i still learning this try and error.

ERROR

Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

At C:\Scripts\Set-EOL.ps1:15 char:18

+     Get-VM –Name $value.'VMname' | Set-Annotation -CustomAttribute Se ...

+                  ~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-VM], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

$VMlists="C:\Scripts\EOL_VMlists.csv"

SCRIPTS

Connect-VIServer -Server vcenterA

$VMs=Import-Csv -Path $VMlists

foreach($VM in $VMs)

{

    Get-VM –Name $value.'Name' | Set-Annotation -CustomAttribute Server.End.Date -Value $value.'Server.End.Date'

   

}

PowerCLI & PowerShell Version

PS C:\Users\~ksani> $PSVersionTable

Name                           Value                                                                                                                                                                                                                            

----                           -----                                                                                                                                                                                                                            

PSVersion                      5.1.14409.1012                                                                                                                                                                                                                   

PSEdition                      Desktop                                                                                                                                                                                                                          

PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                                                          

BuildVersion                   10.0.14409.1012                                                                                                                                                                                                                  

CLRVersion                     4.0.30319.36460                                                                                                                                                                                                                  

WSManStackVersion              3.0                                                                                                                                                                                                                              

PSRemotingProtocolVersion      2.3                                                                                                                                                                                                                              

SerializationVersion           1.1.0.1  

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Just noticed, i made a mistake in the snippet I posted earlier.

Try like this

Connect-VIServer -Server vcenterA

Try{

   $ca = Get-CustomAttribute -Name 'Server.EndDate' -ErrorAction Stop

}

Catch{

   $ca = New-CustomAttribute -Name 'Server.EndDate' -TargetType VirtualMachine

}


$vmNames=Import-Csv -Path $VMlists

foreach($vmName in $vmNames)

{

   $vm = Get-VM -Name $vmName.Name

   Get-VM –Name $vmName.Name | Set-Annotation -CustomAttribute $ca -Value $vmName.'Server.EndDate'

}


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

View solution in original post

Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

Each row of the CSV file is passed in the variable $VM.

You can address the individual columns by using the columnname.

Something like this

Connect-VIServer -Server vcenterA

$VMs=Import-Csv -Path $VMlists

foreach($VM in $VMs)

{

    Get-VM –Name $VM.Name | Set-Annotation -CustomAttribute 'Server.End.Date' -Value $VM.'Server.End.Date'

}

 


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

Reply
0 Kudos
afhamsani
Enthusiast
Enthusiast
Jump to solution

ok!i got it LucD

that make sense with script you corrected me.

i will give it a try and let you know.

Reply
0 Kudos
afhamsani
Enthusiast
Enthusiast
Jump to solution

it is throwing me out another error,sigh~ help help

Set-Annotation : 05/10/2018 02:16:53    Set-Annotation          Could not find a
nnotation
with name 'Server.End.Date'.
At C:\Scripts\Set-EOL.ps1:15 char:29
+ ...  $VM.Name | Set-Annotation -CustomAttribute Server.End.Date -Value $V ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-Annotation], ViError
    + FullyQualifiedErrorId : Client20_InventoryServiceImpl_SetAnnotationValue
   _AnnotationNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAnnota
  tion

Set-Annotation : 05/10/2018 02:17:02    Set-Annotation          Could not find a
nnotation
with name 'Server.End.Date'.
At C:\Scripts\Set-EOL.ps1:15 char:29
+ ...  $VM.Name | Set-Annotation -CustomAttribute Server.End.Date -Value $V ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-Annotation], ViError
    + FullyQualifiedErrorId : Client20_InventoryServiceImpl_SetAnnotationValue
   _AnnotationNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAnnota
  tion

Set-Annotation : 05/10/2018 02:17:11    Set-Annotation          Could not find a
nnotation
with name 'Server.End.Date'.
At C:\Scripts\Set-EOL.ps1:15 char:29
+ ...  $VM.Name | Set-Annotation -CustomAttribute Server.End.Date -Value $V ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-Annotation], ViError
    + FullyQualifiedErrorId : Client20_InventoryServiceImpl_SetAnnotationValue
   _AnnotationNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAnnota
  tion

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like that custom attribute wasn't created yet.

You can add the following to create it when it doesn't exist.

Connect-VIServer -Server vcenterA

Try{

    $ca = Get-CustomAttribute -Name 'Server.End.Date' -ErrorAction Stop

}

Catch{

    $ca = New-CustomAttribute -Name 'Server.End.Date' -TargetType VirtualMachine

}

$vmNames=Import-Csv -Path $VMlists

foreach($vmName in $vmNames)

{

    $vm = Get-VM -Name $vm.Name

    Get-VM –Name $VM.Name | Set-Annotation -CustomAttribute $ca -Value $VM.'Server.End.Date'

}


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

Reply
0 Kudos
afhamsani
Enthusiast
Enthusiast
Jump to solution

it strange when you said attribute wasnt created yet, and found out it was my silly mistake due to a typo.

it should Server.EndDate as snips below, but somehow even after i corrected this with your script here,

still throwing out more or less same error.

pastedImage_0.png

Errorr,

pastedImage_1.png

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is the column with the VM names in your CSV perhaps not 'Name'?

Or is there an empty line in the CSV?


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

Reply
0 Kudos
afhamsani
Enthusiast
Enthusiast
Jump to solution

Hi LucD​,

It is there and no empty line in row or even in column, snips below

pastedImage_0.png

Reply
0 Kudos
afhamsani
Enthusiast
Enthusiast
Jump to solution

Hi LucD​,

It is there and no empty line in row or even in column, snips below

pastedImage_0.png

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Just noticed, i made a mistake in the snippet I posted earlier.

Try like this

Connect-VIServer -Server vcenterA

Try{

   $ca = Get-CustomAttribute -Name 'Server.EndDate' -ErrorAction Stop

}

Catch{

   $ca = New-CustomAttribute -Name 'Server.EndDate' -TargetType VirtualMachine

}


$vmNames=Import-Csv -Path $VMlists

foreach($vmName in $vmNames)

{

   $vm = Get-VM -Name $vmName.Name

   Get-VM –Name $vmName.Name | Set-Annotation -CustomAttribute $ca -Value $vmName.'Server.EndDate'

}


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

Reply
0 Kudos
afhamsani
Enthusiast
Enthusiast
Jump to solution

Hi LucD

alright it works now mate (nless you think i should look at this single error everytime it tries to change the annotation on each VMs :smileysilly:)!

thank you very much and bothering your wekeend.

Had limitation on accessing network past 3 days as i attended a training, sorry for the delays

pastedImage_0.png

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think something went wrong with your copy, that line should say.


$vm = Get-VM -Name $vmName.Name


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

Reply
0 Kudos
afhamsani
Enthusiast
Enthusiast
Jump to solution

you're right mate. I did not change there.

thanks mate!

Reply
0 Kudos