Why Can't I Create The WScript Object?

Why Can't I Create The WScript Object?

Rate This
  • Comments 59

Every now and then someone will ask me why the WSH shell object and the WSH network object are creatable from Visual Basic, but the actual root WScript object is not.

 

I am always completely mystified by why people ask this!  Why would you WANT to create the WScript object in a VB app?  What would it do for you?  It isn't creatable as an in-process object because it represents the state of a WSH process, and a VB app is not a WSH process!  The whole point of the WScript object is that it represents everything about the presently running Windows Script Host.  You can get from it all kinds of properties: the name and version of the script host, the path of the running script, the arguments which were passed in, the standard streams of the process, and whether the script was set to time out or not.  None of these properties make the slightest bit of sense to access outside of a running instance of WSH!  What the heck could this possibly mean?

 

Set WScript = CreateObject("WSH.WScript")

Timeout = WScript.TimeOut

 

The timeout of what?  You've just got some object there, not a running instance of the script host. 

 

What about the methods on the object?  Again, all of them are deeply tied into the underlying script hosting mechanisms.  WScript.CreateObject, GetObject, ConnectObject and DisconnectObject manipulate WSH-owned event sinks on the script dispatch.  I discussed yesterday how deeply tied WScript.Sleep is to the underlying hosting mechanism, and WScript.Echo is not particularly interesting -- there are better ways to create a message box in VB already.

 

I just don't get it.  Why do people keep asking me to make the WScript object creatable?  I can't figure out what they think they're going to do with it once they've created it and no one ever gives me a straight answer when I askIt's like asking for the ability to create the Response object without starting ASP!  What could it possibly mean to have a Response object but no web server process?  It doesn't make any sense, so we don't let you do it.

 

We do provide an object model whereby you can spawn off WSH processes (both locally and remotely) and query their status as they run.  Perhaps that is what these people are looking for?  Remote WSH is a complicated topic in of itself, so perhaps I'll talk about that in a later blog entry.

  • I need to get the Local machine name of the client using vbscript.

    Then how to get the machine name without creating an obeject to the wscript.network?

    How ever the vbscipt is not allowing me to create the object.

    How come i can get the local machine name of the client

  • for example, I'd like to use   WScript.ScriptFullName in HTA. How I can do it?

    It doesn't work, but it can be very useful

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

    <title>My HTML Application</title>

    <script language="vbscript">

    name.InnerHTML = WScript.ScriptFullName

    </script>

    <hta:application

    applicationname="MyHTA"

    border="dialog"

    borderstyle="normal"

    caption="My HTML Application"

    contextmenu="no"

    icon="myicon.ico"

    maximizebutton="no"

    minimizebutton="yes"

    navigable="no"

    scroll="no"

    selection="no"

    showintaskbar="yes"

    singleinstance="yes"

    sysmenu="yes"

    version="1.0"

    windowstate="normal"

    >

    </head>

    <body>

    <!-- HTML goes here -->

    <p id="name"></p>

    </body>

    </html>

  • All of the comments here that support accessing the WScript object or it's functions are classic examples of over thinking a problem.  There is absolutely no need for the WScript object outside of the WSH environment.

    The most recent comment was that it would be nice to have the script name available from with an HTML Application.  The fact is that you already do.  Remember that HTAs run using an instance of the IE engine, therefore most browser capabilities are available to you.  Here's a simple, reusable function for use in an HTA.

    Function ScriptFullName

    strLocation = Replace(window.location, "%20", " ")

    strLocation = Replace(strLocation, "/", "\")

    ScriptFullName = Right(strLocation, Len(strLocation) - 8)

    End Function

    The window.location property is used to return the URL to the current HTA.  This needs to be formatted since it is an encoded URL.  I replace the %20 entity with a space and convert the backslashes to forward slashes.  Finally, I remove the file:/// protocol.

    As for a Sleep function, there are several reusable sleep functions available with a quick Google search.  In a most simplistic form, you can create one using the Time functions.

    Sub Sleep(intSeconds)

    dteStart = Time()

    dteEnd = TimeValue(dteStart) + TimeValue(intSeconds)

    While dteEnd > Time()

    DoNothing

    Wend

    End Sub

    Sub DoNothing

    'While/Wend has quirks when it is empty

    End Sub

    This simply takes an integer number of seconds and adds it to the current time.  It then loops over an empty subroutine until the end time is reached, indicating that the amount of sleep time has elapsed.  I've had to create an empty subroutine because the While/Wend doesn't function properly if it is empty or only contains comments.

  • The latest comment gave me a reason to revisit this post so I thought I'd add a little more useful information.  As I've said before, the WScript family of objects is not necessary in pure VB, however, the WshShell object (specifically the Popup method) IS very useful in VBA.  VBA does not provide a method of creating message boxes through the Windows API but the WshShell object's Popup method provides us with a scriptable object that can.  Here's how to use it.

    'Early bound to "Windows Script Host Object Model"

    Dim WshShell As IWshRuntimeLibrary.WshShell

    Set WshShell = New IWshRuntimeLibrary.WshShell

    'Or shorter syntax for early binding...

    Dim WshShell As New IWshRuntimeLibrary.WshShell

    'Using late binding (no reference)

    Dim WshShell As Object

    Set WshShell = CreateObject("WScript.Shell")

  • PingBack from http://www.nilpo.com/2009/06/nilpo-sightings/comments-about-wscript-on-eric-lipperts-blog/

  • I would just like to see some simple examples of how to translate from WScript calls to .NET equivalent code.  I have NO background in scripts, just need to reproduce teh functionality in .NET.  Are there any articles or resources to enlighten one about the topic?  And about how to work with Active Directories in .NET?

  • I am trying to find out the userid and domain of user who is using my web page The user is running IE from a remote workstation.  My web page resides on a windows 2003 server.

    my page is called whoisuser.html and contains vbscript.  It is running in default web directory under IIS V6.0

    I am trying by any means to get username and any other known information about the user.  THis is just one of many I have tried.  All run fine if they are alone in a .vbs file but fail if I try to run them in the script section of an html page.

    <HTML>

    <HEAD>

    <TITLE>Determine userid</TITLE>

    <SCRIPT LANGUAGE="VBScript">

    document.write("<h1>Who Is User</h1>")

    Set oWshShell = CreateObject("WScript.Shell")

    MsgBox oWshShell.ExpandEnvironmentStrings("User name=%UserName%")

    </SCRIPT>

    </HEAD>

    </HTML>

    Thanks in advance for any help you can provide.

  • Remove the Wscript from the CreateObject so instead of Wscript.CreateObject("Wscript.Shell") just use CreateObject("Wscript.Shell")

  • I'll tell you why.

    I'm trying to use SAP Business Objects, specifically, to rename an exported report to something that includes a custom string.  I have a VB script that creates the string and renames the file.  The VB script needs to accept arguments.  It all works fine from the command line.

    Business Objects has this feature where you can embed a "Program File" for later use, which can be an .exe, Java, or a script file such as VBScript or JavaScript.  When I try to use my VBScript file and pass it some arguments (and the Business Objects interface gives you a place to put the arguments), I get the old "Variable not defined - WScript" error.  Nobody has been able to tell me how in the heck I can pass arguments to a VBScript file without using the WScript object.  I have had to resort to creating a VB script file for each different filename situation  instead of simply one script to which i can pass parameters.  Maddening.  If I had a gun I would shoot my computer right now.

  • @MaryK: each scripting host exposes its environment in its own way. WSH exposes it through 'WScript'. IE exposes it through 'window'.

    Business Objects has its own way. You should have a look at the documentation of the scripting API of product and report issues to SAP. Microsoft can't do anything for you in this case.

    @Robert "Nilpo" Dunham:

    For HTA applications, the scripting host is IE. So you can (should) use window.setTimeout().

  • Reading all that stuff, it is still unclear to me, why I can have a script including e.g.:

    WScript.Sleep 5000 that works when launched from cmd-line.

    But raises an error: "Object required: 'WScript'Line: 79", when used within a ScriptControl (AddCode).

  • Ok, here's mine. I want to invoke a VB script from powershell using MSScriptControl. I want to produce debugging output from the VB script and WScript.Echo seems like it'd be the obvious way to do it. Unfortunately MSScriptControl doesn't seem to provide a environment object t (which would need to hook up to it's host environment)

  • The only feature of WScript that I'd like to be able to use in VBScript is .ScriptName.   I use a 3rd party software tool, Docklight, that has custom built in functions but can also run VBScript.  There is a script file (.pts) and a data file (.ptp)  that are linked together.  When I run the script file I'd like to be able to get the filename of the script file so I can create the file name for the data file and open it.  Right now I have to add a variable with the file name in every script file, but it would be a lot easier to just use something like WScript.ScriptName and be done with it.

  • @NoSimDash @RCMorey11 @dirk

    Read again what I wrote.  The WScript object is only available when running from cscript.exe/wscript.exe.

    To put it clearly; what you are asking is impossible.

Page 4 of 4 (59 items) 1234