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

bryce kinnamon picture bryce kinnamon · Jul 17, 2013 · Viewed 9.3k times · Source

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

Answer

CodeWhisperer picture CodeWhisperer · Jul 17, 2013

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)