Hi All,
I am trying to do something that I think should be simple (but this is the first time I'm using powercli and am not a good code writer), but I'm finding it challenging to say the least.
I created 2 custom attributes (Business System Owner and SSG System Owner) in vCenter for my virtual machines. I have a csv list of the vm names and their associated "owners". I'd like to use powercli to read that csv file and connect to the VC server and input the "owners" in the appropiate field.
I've borrowed com code from various places across the internet and these forums but I cannot seem to make it all work. I have been successful in at least reading in the information from the csv file and getting it displayed on my laptop screen. What I can't seem to do is get the 3 columns in the csv file to be recognized as variables (unless there are some default variables I don't know about) that I can put into the Set-Annotation lines.
The code I have is :
# Input array, includes all VM names from DCI that are registered.
$data = Import-Csv "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\vmowners1.csv" -Header('Hostname','Business Owner', 'SSG Owner')
####$data |Get-Member
Write-Output $data
# Loop through all VMs within vCenter
ForEach ($row in $data)
{
# Create variables for the current VM and its "Contact" and "Description" which are custom attribute fields. Also create a string $note to use with Set-VM -Description
$hostname = Get-VM -Name $vmname;
### $contact = $vm | Get-Annotation -CustomAttribute User;
### $description = $vm | Get-Annotation -CustomAttribute Description;
### $note = $contact.name+":`t"+$contact.value+"`n`n"+$description.name+":`t"+$description.value;
Set-Annotation -Entity $hostname -CustomAttribute "Business System Owner" -value $SSG_owner
Set-Annotation -Entity $hostname -CustomAttribute "SSG System Owner" -value $system_owner
### Set-VM -VM $vm -Description $note -Confirm:$false;
}
The output I get on my screen is :
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts> .\setvmownership.ps1
Hostname Business Owner SSG Owner
-------- -------------- ---------
ABPPROXY-00-AH
ACE-01-AH Morade Laallmmi Morade Laallmmi
Get-VM : 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 command aga
in.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\setvmownership.ps1:13 char:29
+ $hostname = Get-VM -Name <<<< $vmname;
+ CategoryInfo : InvalidData: (:) [Get-VM], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM
Set-Annotation : Cannot validate argument on parameter 'Entity'. The argument is null. Supply a non-null argument and try the command again.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\setvmownership.ps1:17 char:23
+ Set-Annotation -Entity <<<< $hostname -CustomAttribute "Business System Owner" -value $SSG_owner
+ CategoryInfo : InvalidData: (:) [Set-Annotation], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAnnotation
Set-Annotation : Cannot validate argument on parameter 'Entity'. The argument is null. Supply a non-null argument and try the command again.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\setvmownership.ps1:18 char:23
+ Set-Annotation -Entity <<<< $hostname -CustomAttribute "SSG System Owner" -value $system_owner
+ CategoryInfo : InvalidData: (:) [Set-Annotation], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAnnotation
Get-VM : 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 command aga
in.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\setvmownership.ps1:13 char:29
+ $hostname = Get-VM -Name <<<< $vmname;
+ CategoryInfo : InvalidData: (:) [Get-VM], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM
Set-Annotation : Cannot validate argument on parameter 'Entity'. The argument is null. Supply a non-null argument and try the command again.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\setvmownership.ps1:17 char:23
+ Set-Annotation -Entity <<<< $hostname -CustomAttribute "Business System Owner" -value $SSG_owner
+ CategoryInfo : InvalidData: (:) [Set-Annotation], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAnnotation
Set-Annotation : Cannot validate argument on parameter 'Entity'. The argument is null. Supply a non-null argument and try the command again.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\scripts\setvmownership.ps1:18 char:23
+ Set-Annotation -Entity <<<< $hostname -CustomAttribute "SSG System Owner" -value $system_owner
+ CategoryInfo : InvalidData: (:) [Set-Annotation], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAnnotation
The CSV file looks like this (but goes on for about 500 vm's:
I did go with the import-csv option because something i read in my troubleshooting efforts said that the blank columns would cause and error that I was getting and that import-csv would properly intrepret the empty columns.
hostname,business_owner,system_owner,,,,,,,,,,,,,,,,,,,,,,,,
ABPPROXY-00-AH,,,,,,,,,,,,,,,,,,,,,,,,,,
ACE-01-AH,Morade Laallmmi,Morade Laallmmi,,,,,,,,,,,,,,,,,,,,,,,
acrodev-00-ah,tflessa,Bruce Hastings,,,,,,,,,,,,,,,,,,,,,,,,
ACT1DDB00AH,Bill Mancini,Mike Bruni,,,,,,,,,,,,,,,,,,,,,,,,
ACT1TDB00AH,Bill Mancini,Mike Bruni,,,,,,,,,,,,,,,,,,,,,,,,
What do I need to do to be able to get this script to work? I am sure it's related to the "The argument is null." messages that are seen in the output.
Thanks
Dan