VMware {code} Community
shrikant169
Contributor
Contributor

help with RunScriptInGuest()

Hi,

I'm trying to implement the function RunScriptInGuest in c#.

Here is the code:

public void RunScriptInGuest(string interpreter, string scriptText, int options)

{ VMWareJobCallback callback = new VMWareJobCallback();

VMWareJob job = new VMWareJob(_handle.RunScriptInGuest(interpreter, scriptText, options, null, callback),callback);

}

and the code where i call it is like this:

//Sample vbscript code copied from http://www.computerperformance.co.uk/vbscript/vbscript_file_create.htm

//Escape characters are inserted by me.

//The following code lies within a button_click function.

string str = "Option Explicit\r\n";

str += "Dim objFSO, objFSOText, objFolder, objFile\r\n";

str += "Dim strDirectory, strFile\r\n";

str += "strDirectory = \"C:
logs\"\r\n";

str += "strFile = \"
Summer.txt\"\r\n";

str += "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\r\n";

str += "Set objFolder = objFSO.CreateFolder(strDirectory)\r\n";

str += "Set objFile = objFSO.CreateTextFile(strDirectory & strFile)\r\n";

str += "Wscript.Echo \"Just created \" & strDirectory & strFile\r\n";

str += "Wscript.Quit\r\n";

vmachine.RunScriptInGuest("C:
Windows
system32
wscript.exe", str, 0);

textBox1.Text += ("Successful! \r\n");

Now when i run this code, no error is thrown and "Successful!" appears in the textBox1.

However the file summer.txt is not created in the virtual machine.

Can someone pls point out where i am going wrong??

Also, in C#, for the textbox i need to put "\r\n" for a clean new line as putting only "\n" yields an extra box character.

So for the code, for every line what exactly should be put?? \r or \n or \r\n??

Kindly help, thanks.

-Shrikant

Reply
0 Kudos
4 Replies
stanguturi
VMware Employee
VMware Employee

> textBox1.Text += ("Successful! \r\n");

From the code, I see that you are printing "Successful" text irrespective of the result of "RunScriptInGuest" operation. Check the documentation available at http://www.vmware.com/support/developer/vix-api/vix18_reference/

Can you retrieve the error code/result for RunScriptInGuest operation and let us know the result. Result of the "RunScriptInGuest" will give you more information about the error.

-Thanks

Sankar.

shrikant169
Contributor
Contributor

Hi Sankar,

I'll try to get the error code. Having problems with getNthProperty(). As soon as i get that i will post it here.

Thanks.

Regards,

Shrikant

Reply
0 Kudos
shrikant169
Contributor
Contributor

I've just finished spending an hour on trying to get properties in RunScriptInGuest, but i've failed.

Here's the code i'm using:

>private void button3_Click(object sender, EventArgs e)

>{ try

{ textBox1.Text += (vmachine.RunScriptInGuest(null, "echo shrikant> C:
abcd.txt\r", 0));

textBox1.Text += (vmachine.RunScriptInGuest(null, "echo shrikant> C:
abcd.txt\r\n", 0));

textBox1.Text += (vmachine.RunScriptInGuest(null, "echo shrikant> C:
abcd.txt\n", 0));

}

catch (Exception exp)

{ textBox1.Text += exp.ToString();

}

>}

Here is the RunScriptInGuest

>public String RunScriptInGuest(string interpreter, string scriptText, int options)

>{ VMWareJobCallback callback = new VMWareJobCallback();

VMWareJob job = new VMWareJob(_handle.RunScriptInGuest(interpreter, scriptText, Constants.VIX_RUNPROGRAM_RETURN_IMMEDIATELY, null, callback),callback); //i also tried option = 0. same result..

object[] properties =

{ Constants.VIX_PROPERTY_JOB_RESULT_PROCESS_ID,

Constants.VIX_PROPERTY_JOB_RESULT_GUEST_PROGRAM_ELAPSED_TIME, //the 3 properties mentioned in documentation.

Constants.VIX_PROPERTY_JOB_RESULT_GUEST_PROGRAM_EXIT_CODE

};

return job.GetNumProperties(Constants.VIX_PROPERTY_JOB_RESULT_PROCESS_ID).ToString();

>}

The textbox shows 000 when i execute this. (ie. no instances of the property in result in all 3 calls)

I checked the GetNumProperties() with RunProgramInGuest and it works fine.

Please Help,

Thanks.

Regards,

Shrikant.

Reply
0 Kudos
shrikant169
Contributor
Contributor

Hey,

I sincerely apologize for wasting ppl's time.. I've found out what my mistake was..

The VM i was using to check code on, was created using vmware server 1.0.6, and thus had an obsolete version of VM Tools on it.

Just on a hunch i reinstalled VM Tools via code, and now its working fine.. A total noob mistake.. Sorry.

Regards,

Shrikant.

Reply
0 Kudos