Automation

 View Only
Expand all | Collapse all

read in file specific to vm

  • 1.  read in file specific to vm

    Posted Nov 30, 2012 06:43 PM

    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 :smileyhappy:

    $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!!!



  • 2.  RE: read in file specific to vm
    Best Answer

    Posted Nov 30, 2012 07:03 PM

    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



  • 3.  RE: read in file specific to vm

    Posted Nov 30, 2012 09:06 PM

    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



  • 4.  RE: read in file specific to vm

    Posted Nov 30, 2012 09:58 PM

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

    $text = "Line1`nLine2"

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



  • 5.  RE: read in file specific to vm

    Posted Nov 30, 2012 10:01 PM

    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.



  • 6.  RE: read in file specific to vm

    Posted Nov 30, 2012 10:36 PM

    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
    }


  • 7.  RE: read in file specific to vm

    Posted Nov 30, 2012 11:07 PM

    hmm, weird

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



  • 8.  RE: read in file specific to vm

    Posted Nov 30, 2012 11:54 PM

    Yes, you're right.

    I updated the code above, give it another shot



  • 9.  RE: read in file specific to vm

    Posted Dec 01, 2012 07:17 PM

    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!!!




  • 10.  RE: read in file specific to vm

    Posted Dec 01, 2012 09:53 AM

    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



  • 11.  RE: read in file specific to vm

    Posted Dec 01, 2012 07:11 PM


    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...



  • 12.  RE: read in file specific to vm

    Posted Dec 01, 2012 08:54 PM

    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....



  • 13.  RE: read in file specific to vm

    Posted Dec 01, 2012 09:24 PM

    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.



  • 14.  RE: read in file specific to vm

    Posted Dec 03, 2012 07:54 AM

    Hi,

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

    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



  • 15.  RE: read in file specific to vm

    Posted Dec 03, 2012 06:01 PM

    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!!!