VMware

This Question is Possibly Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
10 Replies Last post: Oct 2, 2008 6:35 PM by SCampbell  

how can I read the contents of *.vmx file posted: Sep 18, 2008 8:44 AM

Click to view jzim's profile Lurker 5 posts since
Aug 13, 2008

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


Re: how can I read the contents of *.vmx file

1. Sep 18, 2008 11:48 AM in response to: jzim
Click to view halr9000's profile Master 814 posts since
Jun 7, 2007
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)

Re: how can I read the contents of *.vmx file

2. Sep 22, 2008 12:00 AM in response to: halr9000
Click to view c_shanklin's profile Master 754 posts since
Dec 3, 2007
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.

Re: how can I read the contents of *.vmx file

3. Sep 22, 2008 5:43 AM in response to: c_shanklin
Click to view halr9000's profile Master 814 posts since
Jun 7, 2007
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)

Re: how can I read the contents of *.vmx file

4. Sep 27, 2008 2:10 PM in response to: halr9000
Click to view SCampbell's profile Enthusiast 106 posts since
Apr 18, 2005

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.

Re: how can I read the contents of *.vmx file

5. Sep 27, 2008 8:42 PM in response to: SCampbell
Click to view c_shanklin's profile Master 754 posts since
Dec 3, 2007
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.

Re: how can I read the contents of *.vmx file

6. Sep 27, 2008 9:40 PM in response to: c_shanklin
Click to view halr9000's profile Master 814 posts since
Jun 7, 2007
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. :)






Author of the upcoming book: Managing VMware Infrastructure with PowerShell
Co-Host, PowerScripting Podcast (http://powerscripting.net)

Re: how can I read the contents of *.vmx file

7. Sep 28, 2008 4:51 AM in response to: halr9000
Click to view SCampbell's profile Enthusiast 106 posts since
Apr 18, 2005

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 http://stackoverflow.com/questions/65856/how-can-i-perform-http-put-uploads-to-a-vmware-esx-server-in-powershell for the model.

Thanks very much for the help with this.

Re: how can I read the contents of *.vmx file

8. Sep 28, 2008 6:50 AM in response to: SCampbell
Click to view halr9000's profile Master 814 posts since
Jun 7, 2007
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)

Re: how can I read the contents of *.vmx file

9. Sep 28, 2008 1:13 PM in response to: halr9000
Click to view SCampbell's profile Enthusiast 106 posts since
Apr 18, 2005
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.

Re: how can I read the contents of *.vmx file

10. Oct 2, 2008 6:35 PM in response to: SCampbell
Click to view SCampbell's profile Enthusiast 106 posts since
Apr 18, 2005

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 https://esxserver/hosts URL (e.g., SSL key and certificate, hosts file), and to Datastores as is shown in the link.


VMware Developer

SDKs, APIs, Videos, Learn and much more in the Developer community.

Learn More

Developer Sample Code

Increase your developer productivity with VMware API sample code.

Learn More

VMworld Sessions & Labs

Online access to the latest VMworld Sessions & Labs and online services.

Learn more

Purchase PSO Credits Online

Purchase credits to redeem training and consulting services online.

Buy Now

Community Hardware Software

View reported configurations or report your own.

Learn More

VMware vSphere

Come witness the next giant leap in virtualization.

Register Today

Communities