메뉴 건너뛰기

Dev tips

Windows Server [VBS] WScript 메소드

taknim 2013.01.29 04:07 조회 수 : 6986 추천:654

http://www.pctools.com/guides/scripting/id/2/?act=referencehttp://cafe.naver.com/sharedtalk/90

 

출처: http://www.pctools.com/guides/scripting/id/2/?act=reference

WScript Methods
Methods provide access to Windows functions

 

AddPrinterConnection Method
The AddPrinterConnection method is used to map a network printer to a local printer resource.

AddWindowsPrinterConnection Method
The AddPrinterConnection method is used to add a printer connection to the local system.

AppActivate Method
The AppActivate method is used to change the focus of a running application.

CreateObject Method
The CreateObject method is used to create an instance of the specified object.

CreateShortcut Method
The CreateShortcut method is used to create shortcuts to files and remote URL's.

DisconnectObject Method
The DisconnectObject method is used to disconnect a previous created object.

Echo Method
The Echo method to display information to the user through the console (CSCRIPT) or dialog boxes (WSCRIPT).

EnumNetworkDrives Method
The EnumNetworkDrives method creates a array containing the drive letter and network path for the locally mapped drives.

EnumPrinterConnections Method
The EnumPrinterConnections method creates a array containing the port and device names for the installed printers.

ExpandEnvironmentStrings Method
The ExpandEnvironmentStrings method returns a string containing the requested substituted environment variable.

GetObject Method
The GetObject method retrieves an object from a file or from an object specified.

LogEvent Method
The LogEvent method is used to log an event to the Event Log (Windows NT/2000) or the WSH.log file (Windows 95, 98, Me).

MapNetworkDrive Method
The MapNetworkDrive method is used to map a network file share to a local drive resource.

Popup Method
The Popup method is used to display a dialog box to a user and wait a button to be pressed, or optionally, a timeout to occur.

Quit Method
The Quit method is used to terminate the execution of the current script with a specified error code.

RegDelete Method
The RegDelete method is used to delete a value or key from the Windows registry.

RegRead Method
The RegRead method is used to retrieve a value from the Windows registry.

RegWrite Method
The RegWrite method is used to write a value to the Windows registry.

RemoveNetworkDrive Method
The RemoveNetworkDrive method is used to disconnect a network drive mapping from the local system.

RemovePrinterConnection Method
The RemovePrinterConnection method is used to disconnect a printer device from the local system.

Run Method
The Run method is used to launch a local application.

SendKeys Method
The SendKeys method is used to send keystrokes to the currently active window as if they where typed from the keyboard.

SetDefaultPrinter Method
The SetDefaultPrinter method is used to specify which printer should be set as the default device.

Sleep Method
The Sleep method is used to freeze the script execution for a specified number of milliseconds and then continue execution.

 

AddPrinterConnection Method

Description

The AddPrinterConnection method is used to map a network printer to a local printer resource.

object.AddPrinterConnection LocalDevice, RemoteDevice [,SaveConnection] [,Username] [,Password]

Parameters
The LocalDevice parameter is used to specify the local printer resource.

The RemoteDevice parameter is used to specify the remote printer resource.

The optional SaveConnection parameter is a boolean value used to specify whether the mapping is stored in the user profile.

The optional Username and Password parameters are used to specify alternate credentials for the mapping.

Example

'VBScript  Example
Set WshNetwork = CreateObject("WScript.Network")

WshNetwork.AddPrinterConnection "LPT1", "\\printserver\printer", _
   True, "jbloggs", "password"

 

AddWindowsPrinterConnection Method

Description

The AddPrinterConnection method is used to add a printer connection to the local system.

The parameters for this method are based on the operating system type:

Windows NT/2000
object.AddWindowsPrinterConnection PrinterPath

Windows 95,98 & Me
object.AddWindowsPrinterConnection PrinterPath, Driver [, Port]

Parameters
The PrinterPath parameter is a string representing the path to the printer device.

The Driver parameter is onl y required with Windows 9x and specifies the printer driver name, that is already installed on the system, to use with the printer device.

The optional Port parameter is onl y required with Windows 9x and specifies the local port to attach the printer connection to.

Example

'VBScript  Example for Windows NT/2000
Set WshNetwork = CreateObject("WScript.Network")

WshNetwork.AddWindowsPrinterConnection "\\printserver\printer"

'VBScript  Example for Windows 95, 98 & Me
Set WshNetwork = CreateObject("WScript.Network")

WshNetwork.AddWindowsPrinterConnection "\\printserver\printer", _
   "HP Laserjet 5", "LPT1"

 


AppActivate Method

Description

The AppActivate method is used to change the focus of a running application.

object.AppActivate (Title)

The Title parameters is a string that specifies the title (or task identifier) of the application to be activated (receive focus). The Title parameter is compared the string titles of the running applications and the best match is activated.

Example

'VBScript  Example
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate ("Internet Explorer")

 

CreateObject Method

Description

The CreateObject method is used to create an instance of the specified object.

object.CreateObject(ProgramID [,Prefix])

Parameters
The ProgramID parameter specifies the object's program identifier.

The optional Prefix parameter specifies that WSH should listen for events and then call a subroutine based on the combination of the Prefix and the event name. For example, if the Prefix is "MyDoc" and an event called "Refresh" occurs, WSH will call the "MyDoc_Refresh" subroutine in the script.

Example

'VBScript  Example
Set WshNetwork = WScript.CreateObject("WScript.Network")

  

CreateShortcut Method

Description

The CreateShortcut method is used to create shortcuts to files and remote URL's.

object.CreateShortcut (Filename)

The Filename parameter is used to specify the path and file name of the shortcut to be created. This parameter must end with the extension ".lnk" to create a file shortcut or ".url" to create a URL shortcut.

Depending on the file extension various parameters must be set on the object that is returned.

For file shortcuts the following properties may be specified: Arguments Property, Description Property, FullName Property, Hotkey Property, IconLocation Property, TargetPath Property, WindowStyle Property, WorkingDirectory Property

For URL shortcuts the following properties should be specified: FullName Property and TargetPath Property.

Once the properties have been supplied the Save method should be used to save the shortcut.

Example

set WshShell = WScript.CreateObject("WScript.Shell")
DesktopFolder = WshShell.SpecialFolders("Desktop")

set FileShortcut = WshShell.CreateShortcut(DesktopFolder & "\Script Shortcut.lnk")
FileShortcut.TargetPath = WScript.ScriptFullName
FileShortcut.Description = "Script Shortcut"
FileShortcut.WorkingDirectory = DesktopFolder
FileShortcut.Save

set URLShortcut = WshShell.CreateShortcut(DesktopFolder & "\Windows Scripting Guide.url")
URLShortcut.TargetPath = "http://scripting.winguides.com/"
URLShortcut.Save

 

DisconnectObject Method

Description

The DisconnectObject method is used to disconnect a previous created object.

object.DisconnectObject object_reference

Example

'VBScript  Example
Set WshNetwork = WScript.CreateObject("WScript.Network")

WScript.Echo "User Name: " & WshNetwork.UserName

WScript.DisconnectObject WshNetwork

 

Echo Method

Description

The Echo method to display information to the user through the console (CSCRIPT) or dialog boxes (WSCRIPT).

object.Echo [Arg1] [,Arg2] ... [,ArgX]

Example

'VBScript  Example
WScript.Echo "Hello World!"
WScript.Echo "Hello" & " " & "World!"
WScript.Echo "Hello", "World!"

'JScript  Example
WScript.Echo("Hello World!")
WScript.Echo("Hello" + " " + "World!")
WScript.Echo("Hello", "World!")

  

EnumNetworkDrives Method

Description

The EnumNetworkDrives method creates a array containing the drive letter and network path for the locally mapped drives.

object.EnumNetworkDrive

Returns a collection containing the drive letters and network paths coupled in sequential pairs. For example, the first item is the drive letter of the first mapping, the second item is the network path of the first mapping, the third item is the drive letter of the second mapping and so on.

Example

'VBScript  Example
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set Drives = WshNetwork.EnumNetworkDrives

For i = 0 to Drives.Count - 1 Step 2
   WScript.Echo Drives.Item(i), Drives.Item(i+1)
Next


EnumPrinterConnections Method

Description

The EnumPrinterConnections method creates a array containing the port and device names for the installed printers.

object.EnumPrinterConnections

Returns a collection of port and device names coupled in sequential pairs. For example, the first item is the port of the first printer, the second item is the device name of the first printer, the third item is the port of the second printer and so on.

Example

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set Printers = WshNetwork.EnumPrinterConnections

For i = 0 to Printers.Count - 1 Step 2
   WScript.Echo Printers.Item(i), Printers.Item(i+1)
Next


ExpandEnvironmentStrings Method

Description

The ExpandEnvironmentStrings method returns a string containing the requested substituted environment variable.

object.ExpandEnvironmentStrings (EnvVariable)

The EnvVariable parameter specifies which environment variable to expand and return.

Example

'VBScript  Example
set WshShell = CreateObject("WScript.Shell")
WScript.Echo WshShell.ExpandEnvironmentStrings("%SystemRoot%")
WScript.Echo WshShell.ExpandEnvironmentStrings("%WinDir%")

 

GetObject Method

Description

The GetObject method retrieves an object from a file or from an object specified.

object.GetObject(Filename [,ProgramID], [Prefix])

Parameters
The Filename parameter specifies the full path and filename containing the object to be retrieved.

The optional ProgramID parameter specifies the object's program identifier.

The optional Prefix parameter specifies that WSH should listen for events and then call a subroutine based on the combination of the Prefix and the event name. For example, if the Prefix is "MyDoc" and an event called "Refresh" occurs, WSH will call the "MyDoc_Refresh" subroutine in the script.

Example

Set MyDocument = GetObject("c:\work\order.doc")

MyDocument.SaveAs "c:\work\order_new.doc"

  

LogEvent Method

Description

The LogEvent method is used to log an event to the Event Log (Windows NT/2000) or the WSH.log file (Windows 95, 98, Me).

object.LogEvent(Type, Message [, System])

Parameters
The Type parameter specifies the type of event to log according to the table below.

Type

Value

0

SUCCESS

1

ERROR

2

WARNING

4

INFORMATION

8

AUDIT_SUCCESS

16

AUDIT_FAILURE

The Message parameter is a string that specifies the text message to be logged.

The optional System parameter is available under Windows NT/2000 to specify an alternate host to log the information to instead of the local host.

One Windows 9x the WSH.log is found in the Windows directory.

Example

'VBScript  Example
set WshShell = CreateObject("WScript.Shell")
WshShell.LogEvent 0, "Successful Script Completion"

WshShell.LogEvent 2, "Warning - device not responding"

 

MapNetworkDrive Method

Description

The MapNetworkDrive method is used to map a network file share to a local drive resource.

object.MapNetworkDrive LocalDevice, RemoteDevice [,SaveConnection] [,Username] [,Password]

Parameters
The LocalDevice parameter is used to specify the drive letter.

The RemoteDevice parameter is used to specify the remote network path.

The optional SaveConnection parameter is a boolean value used to specify whether the mapping is stored in the user profile.

The optional Username and Password parameters are used to specify alternate credentials for the mapping.

Example

'VBScript  Example
Set WshNetwork = WScript.CreateObject("WScript.Network")

WshNetwork.MapNetworkDrive "S:", "\\server\share"

 

Popup Method

Description

The Popup method is used to display a dialog box to a user and wait a button to be pressed, or optionally, a timeout to occur.

object.Popup (Message [, TimeOut][, Title] [, Format])

The return value will depend on which button was clicked as seen in [id=53#results]this table[/id].

Parameters
The Message parameter is a string that specifies the text to display in the dialog box.

The optional TimeOut parameter specifies the number of seconds to wait before automatically closing the box and returning a value of "-1".

The optional Title parameter is a string that specifies the title of the dialog box.

The optional Format parameter is the sum of values specifying the number and type of buttons to display, the icon to use and the default button. See [id=53]these tables[/id] for the available options.

Example

'VBScript  Example
set WshShell = CreateObject("WScript.Shell")

Result = WshShell.Popup("This is a popup box!", 10, "Title", 65)

 

Quit Method

Description

The Quit method is used to terminate the execution of the current script with a specified error code.

object.Quit [ErrorCode]

The optional ErrorCode parameter is used to specify a process exit code. If omitted, 0 (zero) is returned as the process exit code.

Example

'terminate the current script
WScript.Quit

'terminate the current script with exit code 2
WScript.Quit 2

  

RegDelete Method

Description

The RegDelete method is used to delete a value or key from the Windows registry.

object.RegDelete Name

<