VMware Cloud Community
drivera01
Enthusiast
Enthusiast
Jump to solution

read in file specific to vm

Hello,

So on to something else. I have the bulk of my script done but I do not know how to do one thing.

scenario: I have 300 windows vms. I want to update their notes field with relevent info.

updating the notes field for all is the easy part via powercli, where I am unsure is how do I update it with specific data that is vm specfic. So in theory I would have 300 unique text files with relevent info for each VM, call it the master lists.

So I am wondering if I can read in a variable specific to the vm to update it accordingly?

never said I was good at this scripting Smiley Happy

$vms = "windows01"windows02","windows03;

 
ForEach ($vmname in $vms)
{
   
  
     $vm = Get-VM -Name $vmname;
      $notes = " whoooo: meee`n whatttt: idontknow`n whereeeee: hereee`n whyyyy:becauseeee";
     Set-VM -VM $vm -Description $notes;  -Confirm:$false;
}

As you can see all my vm's will get the same notes entry...that is not cool. So I was wondering can I point each vm to a vm specific file:

E:\temp\windows01 for instance?

Thanks!!!

0 Kudos
1 Solution

Accepted Solutions
Grzesiekk
Expert
Expert
Jump to solution

Hi drivera

create a text files like this

vmname,notes
vm1,"something something"
vm2,"something else"
vm3,"really nice something"

sAve this content in a file name is : c:\mydata.csv

then

$csvdata=import-csv c:\mydata.csv

then

ForEach ($vm in $csvdata)
{
    $notes=$vm.notes

    $vm=$vm.vmname


     Set-VM -VM $vm -Description $notes -Confirm:$false
}

if you really want to do it this way

"

As you can see all my vm's will get the same notes entry...that is  not cool. So I was wondering can I point each vm to a vm specific file:

E:\temp\windows01 for instance?

"

let me know, i think that having all notes for vms in 1 csv file is way better, easier

--- @blog https://grzegorzkulikowski.info

View solution in original post

0 Kudos
14 Replies
Grzesiekk
Expert
Expert
Jump to solution

Hi drivera

create a text files like this

vmname,notes
vm1,"something something"
vm2,"something else"
vm3,"really nice something"

sAve this content in a file name is : c:\mydata.csv

then

$csvdata=import-csv c:\mydata.csv

then

ForEach ($vm in $csvdata)
{
    $notes=$vm.notes

    $vm=$vm.vmname


     Set-VM -VM $vm -Description $notes -Confirm:$false
}

if you really want to do it this way

"

As you can see all my vm's will get the same notes entry...that is  not cool. So I was wondering can I point each vm to a vm specific file:

E:\temp\windows01 for instance?

"

let me know, i think that having all notes for vms in 1 csv file is way better, easier

--- @blog https://grzegorzkulikowski.info
0 Kudos
drivera01
Enthusiast
Enthusiast
Jump to solution

only question is how do you deal with carriage returns your way?

It would be nice for it to be formatted like:

this is the info

here is more

last info for the day


right now, I see it as:

this is the info here is

more last info for the

day

your way is much better though..

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Just insert `n (back-tick-n) where you want a newline

$text = "Line1`nLine2"

Get-VM -Name MyVM | Set-VM -Description $text -COnfirm:$false


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

drivera01
Enthusiast
Enthusiast
Jump to solution

ya thats what I was doing  when I was trying to do it my way, but Grzesiekk suggested putting everything in a csv, which made sense. So I was wondering in regards to csv files.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can include that in the CSV file as well.

vmname,notes
vm1,"Line 1`nLine 2"

And update with

Import-Csv C:\vmnotes.csv -UseCulture | %{
  Set-VM -VM $_.vmname -Notes $_.notes.Replace('`n',[char]10) -Confirm:$false
}


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

drivera01
Enthusiast
Enthusiast
Jump to solution

hmm, weird

I still just see it wrap. I see the `n

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, you're right.

I updated the code above, give it another shot


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

0 Kudos
Grzesiekk
Expert
Expert
Jump to solution

Hmmmm have you tested putting enters in the csv file ? it works for me

PS C:\Users\gregu> $a[2].name.gettype()

IsPublic IsSerial Name
-------- -------- ----
True     True     String

and this $[2] is

vm,name
vm1,'Some name'
vm2,"some other name"
vm3,"aasdf                   <--- this one
more test lines
more new lines in this vm3 line
more"
vm4,"easy test"

PS C:\Users\gregu> $a[2].name
aasdf
more test lines
more new lines in this vm3 line
more

--- @blog https://grzegorzkulikowski.info
0 Kudos
drivera01
Enthusiast
Enthusiast
Jump to solution


Hi Grzesiekk

I believe I tried that initially and it bombed on me.. let me double check though...

since it worked for you I would assume it should work for me...

0 Kudos
drivera01
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

That did the trick, many thanks!!!

just for my own education can you explain what this is doing?

I know you are doing a replace/substitution of some sort but...

$_.notes.Replace('`n',[char]10

Thank you!!!


0 Kudos
drivera01
Enthusiast
Enthusiast
Jump to solution

question - if I make the modifications to my notes field. and later I need to add a particular item but keep what is existing, is there a way to append ?

unless i am mistaking, it seems to overwrite right now....

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, the `n sequence is interpreted by the PowerShell output routines. These output routines replace the `n by a character with decimal value 10, or a newline.

The Set-VM cmdlet just takes the string as is and places it in the property of the VM. The cmdlet does NOT replace the `n by a char(10). That's why you saw the `n sequence in the Notes field.

To emulate what the output routines are doing I did the Replace in the script; replace `n by [char]10.

You have to do the append yourself, in other words first get the current value from the Notes property, append your new text and save that with the Set-VM cmdlet.


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

0 Kudos
Grzesiekk
Expert
Expert
Jump to solution

Hi,

  well enters do work for me, tested it in PS and in vsphere. take a look

descwithenter.jpg

When you have lines

" sdfsa sd fsdf sad fsa

sfdsad  sdfa

sdfads "

you do not need to use replacing for new line, they are read perfectly fine by set-vm, well at least for me, i pasted screenshot above.

Greg

--- @blog https://grzegorzkulikowski.info
0 Kudos
drivera01
Enthusiast
Enthusiast
Jump to solution

Hi Grzesiekk,

You are absolutley right, it does work as you mentioned. What I was doing wrong was I thought I had to start out with a CSV file. I did not know the import command makes it into the cvs file.  Even when I ran it with the cvs file, it did work as you expected but it did throw an error.

Anyways, when I first did it, I must of not tested it like I thought.

Either way, it is nice to have more than one way to skin a cat. Depending on how the data is formatted..

Thank you much to you and LucD for the help!!!

0 Kudos