Server.CreateObject("Wscript.Shell") issue - Object Server required error??? ASP/VBscript error

StackOverflow https://stackoverflow.com/questions/17708019

سؤال

I'll try to keep this basic. I'm trying to run a shell on the server side (not the client). I've broken down my code so it's pretty basic. right now if I run this on the client side using CreateObject("Wscript.shell") it will 'document.write' the user in my browser.

<script type="text/vbscript" >
Set Shell = CreateObject("WScript.Shell")
Set whoami = shell.exec("whoami")
Set whoamiOutput = whoami.StdOut
strWhoamiOutput = whoamiOutput.ReadAll
document.write strWhoamiOutput

</script>

Now if I change my code to run on the server side:

<script type="text/vbscript" >
Set Shell = Server.CreateObject("WScript.Shell")
Set whoami = shell.exec("whoami")
Set whoamiOutput = whoami.StdOut
strWhoamiOutput = whoamiOutput.ReadAll
document.write strWhoamiOutput

</script>

I get an error in my browser telling me 'object required: server' at line 11. Line 11 is the 'Server.CreateObject' line. What am I missing here?

Thanks

هل كانت مفيدة؟

المحلول

From your 'document.write' and 'script' lines it would appear that you are trying to run this code in the browser... if so, you won't be able to do what you want to do.

server.createobject would be for VBScript/ASP usage on the server itself. (the 'server' object is an ASP object and would not be available in VBScript in the client browser)

To do what you want (if I am reading between the lines correctly) you would need to create an ASP script (or similar) on your server to grab the output from 'whoami' and return/output it. You could call it from your client-side page via javascript/AJAX.

(Keep in mind that running a command using 'WScript.Shell' carries its own set of security challenges, as well)

نصائح أخرى

In order for the script to run on the server, you need the runat attribute on your script tag:

<script type="text/vbscript" runat="server">

or, if your default scripting language is VBScript (which it will be unless you've explicitly changed it), then you should use the ASP script delimiters <% %> instead to avoid any unexpected results due to script execution order (see this SO question for more information).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top