Automation

 View Only
Expand all | Collapse all

how can I read the contents of *.vmx file

  • 1.  how can I read the contents of *.vmx file

    Posted Sep 18, 2008 03:44 PM

    I want to read the contents of a .vmv file using power shell but I am running into this problem. At the shell I do the following:

    $datastoreObject = get-datastore -name <my datastore>

    $DSDrive = New-PSDrive -Name datastoreUnderTest -Location $datastoreObject -PSProvider VimDatastore -Root /

    cd datastoreUnderTest:\testVM

    From here I can do an ls and see all of the files in the directory. But if I try to do:

    $content = get-content testVM.vmx

    It fails with:

    Get-Content : Cannot use interface. The IContentCmdletProvider interface is not

    implemented by this provider.

    At line:1 char:23

    What should I be using to get the contents of the file?

    Also where can I get more information on PSProvider VimDatastore does anyone have link where I can read up on this?

    Thanks



  • 2.  RE: how can I read the contents of *.vmx file

    Posted Sep 18, 2008 06:49 PM

    Unfortunately, the two PSProvider interfaces are experimental (says so in the release notes I believe) and suffer from a lack of features and poor performance. I'm sure this will improve in future versions.

    But before you run off, there's two ways to do what you want. You can use the VI API, or HTTP. Try browsing to http://esxserver/folder, http://vcenter/folder. The VMX files are in there. You can also go to http://esxserver/host for some other files.

    This stuff is not well doc'd at all. You can find tips here and there in VI API forum. One benefit of the HTTP method though is that it is blazing fast.

    If you search this forum, there are other posts dealing with searching datastores using the API. I'd give you a direct link but I'm running out the door.






    Author of the upcoming book: Managing VMware Infrastructure with PowerShell

    Co-Host, PowerScripting Podcast (http://powerscripting.net)



  • 3.  RE: how can I read the contents of *.vmx file

    Posted Sep 22, 2008 07:01 AM

    I've wanted to be able to read VMX entries for a while but I can't remember why. Anyway I've added a function that parses VMX files to the VI Toolkit Extensions on Codeplex. To use it, download the module and run

    get-vm yourvm | get-tkevmx

    . It should spit out an object-ified version of your VMX file. Note that this requires a separate login because it uses the HTTP thing Hal mentioned.

    Note you'll need PowerShell CTP2 v2 to load the module.



  • 4.  RE: how can I read the contents of *.vmx file

    Posted Sep 22, 2008 12:43 PM

    Awesome Carter, thanks for that. Do you plan on adding an upload? I was working that for my book but kinda stalled and moved to something else.






    Author of the upcoming book: Managing VMware Infrastructure with PowerShell

    Co-Host, PowerScripting Podcast (http://powerscripting.net)



  • 5.  RE: how can I read the contents of *.vmx file

    Posted Sep 27, 2008 09:10 PM

    I am in the same boat. I used Carter`s CodePlex example for the download (System.Net.WebClient) and it worked great.

    Now trying to use the same System.Net.WebClient class and get errors:

    .UploadFile($URL,"PUT",$File) returns (501) not implemented

    .UploadFile($URL,$File) returns (400) Bad request

    I have read elsewhere that this stuff uses PUT, or at least the host site does. I have got the following code working when called as a VBScript so it is some form of PUT

    Set oHTTP = WScript.CreateObject("MSXML2.ServerXMLHTTP")
    oHTTP.setOption 2,13056
    oHTTP.open "PUT", sURL, False, sUserID, sPassword
    iReturn=oHTTP.send(sData)

    Any assistance would be most appreciated.



  • 6.  RE: how can I read the contents of *.vmx file

    Posted Sep 28, 2008 03:43 AM

    Hi, got your mail but since you're here I'll just post it here.

    Have you tried this method of uploading using PowerShell? I haven't taken the time to understand all the details but Hal had mentioned some difficulties uploading using system.net.webclient and that this approach works better. There's a good chance you'll need to tweak the parameters a bit, but if it works, please let us know.



  • 7.  RE: how can I read the contents of *.vmx file

    Posted Sep 28, 2008 04:40 AM

    I was working on this some today but didn't have a chance to finish. There's three more popular ways to do web stuff in powershell: system.net.webclient (.NET), system.net.HttpWebRequest (.NET), and Xml Http (COM). Shay wrote a post some months ago which did a great job of summarizing their use (in response to a discussion I'd had with him about the topic).

    http://scriptolog.blogspot.com/2007/08/query-http-status-codes-and-headers.html

    Anyway, my point is that if you can do it in Vbscript then you can do it in PowerShell. The syntax may be a little different but the logic is going to be the same when talking about accessing that COM object.

    Further, I messed with uploading files and got stuck so I punted it as my first Stackoverflow test: http://stackoverflow.com/questions/65856/how-can-i-perform-http-put-uploads-to-a-vmware-esx-server-in-powershell

    I never had a chance to test Jaykul's reply, but it probably works when tuned to the problem at hand.

    So in other words--I'd love the answer as well. It's one of those things that I wanted to do, but when I found it wasn't quite as simple as I wanted it to be, I moved on to something else. :smileyhappy:






    Author of the upcoming book: Managing VMware Infrastructure with PowerShell

    Co-Host, PowerScripting Podcast (http://powerscripting.net)



  • 8.  RE: how can I read the contents of *.vmx file

    Posted Sep 28, 2008 11:52 AM

    I found the real trick for this at http://communities.vmware.com/thread/117504, and I think it is to use $WebRequest.PreAuthenticate=$True.

    You need to use the WebRequest object, since for the strangest reason you can't easily get at the WebRequest object underlying a Webclient object. It looks like $WebClient.GetWebRequest() can only be implemented inside a new class that inherits from WebClient

    Aside from that, I used the code at for the model.

    Thanks very much for the help with this.



  • 9.  RE: how can I read the contents of *.vmx file

    Posted Sep 28, 2008 01:51 PM

    Awesome. Could you post the full example for everyone's benefit?

    (I had read that thread before, but didn't notice it had a second page with the vital PreAuthenticate key.)






    Author of the upcoming book: Managing VMware Infrastructure with PowerShell

    Co-Host, PowerScripting Podcast (http://powerscripting.net)



  • 10.  RE: how can I read the contents of *.vmx file

    Posted Sep 28, 2008 03:01 PM

    UPDATE: The original unedited code post doesn't work as intended. The file is uploaded but the format is changed. I suspect it is an encoding issue, and will post an update when possible.



  • 11.  RE: how can I read the contents of *.vmx file

    Posted Oct 03, 2008 01:35 AM

    Update to the update

    Carter Shaklin has done an excellent job at this link http://communities.vmware.com/message/1063296 presenting my rather cumbersome Powershell code to get uploads to work.

    This mechanism seems to work to upload things to the URL (e.g., SSL key and certificate, hosts file), and to Datastores as is shown in the link.