powershell script that reads from excel and updates annotations

I could not find an existing topic, or maybe I couldn't find the right wording to find it.
I have a spreadsheet with server names in one column and a value in a second column.
This is the one liner that will update the annotation. (It is using powershell with powercli imported, to update vm annotations in VSPHERE.)
Set-annotation -entity vm-name -customattribute "location" -value "data center"

Open in new window


I could just create a script from Excel and export it to text, and run it, which I have done before. (Basically a bunch of one-liners)
Is it fairly simple for a script to pull 2 things from a spreadsheet? In this case it would be the VM name, and the value of the attribute that will change row to row.

Thanks!
LVL 1
SquigglyMonkeyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Yes, that's usually fairly simple.
Make sure that the sheet has a header line with the column names; I'm assuming "VMName" and "Location" (at least that's what I think it is you want to update).
Then save the sheet as csv file.
The cmdlet Import-Csv will read the file and turn the rows into objects with the column names as properties.
Just run this to see what it looks like:
Import-Csv -Path C:\Temp\Annotations.csv

Open in new window

And to update the annotations in a loop in a script:
Import-Csv -Path C:\Temp\Annotations.csv | ForEach-Object {
	Set-Annotation -Entity $_.VMName -CustomAttribute "location" -Value $_.Location
}

Open in new window

Or as a one-liner for the command line ('%' is an alias for ForEach-Object - but you shouldn't use aliases in scripts):
Import-Csv -Path C:\Temp\Annotations.csv | % {Set-Annotation -Entity $_.VMName -CustomAttribute "location" -Value $_.Location}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SquigglyMonkeyAuthor Commented:
Well, darn. not working for me as yet. Hopefully something simple.
I have a csv file with 2 columns, the first  cell a1 has "VMName"(no quotes) in the cell below it ,in a2 a3 and a4 are server names, lets say server1 server2, etc.
in b1, the cell has "value"(no quotes)  in it, since this is actually what is getting changed .
It is saved as anno.csv, in the same directory as the script.
When I open it in excel, I see the 2 columns and the data in them.
An administrative ps session is opened in that directory.
When I run this:
Import-Csv -path C:\scripts\anno.csv | ForEach-Object {
                Set-Annotation -Entity $_.VMName -CustomAttribute "location" -Value $_.value
}

Open in new window


I get this, which looks to me like the command is not getting the server names:

Set-Annotation : Cannot validate argument on parameter 'Entity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At C:\scripts\update-anno-from-csv.ps1:2 char:25
+     Set-Annotation -Entity $_VMName -CustomAttribute "location" ...
0
oBdACommented:
Open the csv file in Notepad or any other text editor and verify the format, and/or just run
Import-Csv -path C:\scripts\anno.csv
It could be that your standard delimiter is not a comma, and Import-Csv expects a comma by default.
If the delimiter in the csv file is, for example, a ";", just add the parameter
-Delimiter ";"
to the Import-Csv command.
Import-Csv -Path C:\scripts\anno.csv -Delimiter ";"
0
SquigglyMonkeyAuthor Commented:
So the issue is the csv file, which I am also seeing when I just import it, the top "headers" are run together,
I receive  a spreadsheet with two columns, which I save as anno.csv.
I guess that is not sufficient to create it in the correct format, if I open it in notepad there is no commas or any delimiter.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.