CreateObject / wscript.CreateObject

Create a Windows Scripting Host (WSH) automation object / run an external command.

Syntax:
      Set objObject = CreateObject(ServerName.typeName, [Location])

Key:
   ServerName   The name of the application
   typeName     Type or class of object to create
   Location     The name of the server. (creates the object on a remote server via DCOM)


Syntax:
      Set objObject = WScript.CreateObject(strProgID[, strPrefix])

Key:
   strProgID    The programmatic identifier of the object to create. String value.
   strPrefix    String value indicating the function prefix.
                Create a local object and hook up its event handlers

Objects created with the wscript.CreateObject method using the strPrefix argument are connected objects.
These are useful when you want to sync an object’s events.

CreateObject is a wscript method.

Examples

Run a cmd command from vbs

Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.run("%comspec% /c ipconfig /release")

Run a cmd batch file from vbs

Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.run("%comspec% /c mybatchfile.cmd")

Create a WshController object to run scripts on a remote machine:

strServer = "machine_name"
strScript = "script_name.vbs"
Set objWshController = WScript.CreateObject("WshController")
Set objRemoteScript = objWshController.CreateScript(strScript, strServer)
objRemoteScript.Execute

For the above to work, enable DCOM , then enable WshRemote in the registry:

HKLM\Software\Microsoft\Windows Script Host
String value: Remote

Set Remote to "1" to enable WshRemote and "0" to disable.

Create a WshNetwork object (for mapping to a network share)
WshNetwork = WScript.CreateObject("WScript.Network")

Echo the script mode.
WScript.Echo (WScript.Interactive)

“..an understanding of Visual Basic would be advantageous although not to a programming level.” ~ Job advert on Monster.com

Related:

cscript - Run a VBScript .vbs file.
.GetObject - Get an Automation object.
.ConnectObject - Connect to a COM object.
.DisconnectObject - Disconnect from a COM object.
.Exec - Run a command.
The difference between WScript.CreateObject and CreateObject - Eric Lippert.
psExec - Run commands remotely.
Equivalent PowerShell: New-Object


 
Copyright © 1999-2024 SS64.com
Some rights reserved