메뉴 건너뛰기

Dev tips

Windows Server [VBS] WScript 메소드

taknim 2013.01.29 04:07 조회 수 : 6788 추천: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

<

번호 제목 글쓴이 날짜 조회 수
42 윈도우 가상화 이미지 taknim 2016.12.20 49
41 포트쿼리 사용 (PortQry) taknim 2015.06.16 6348
40 FTP 패시브모드 포트 고정 taknim 2014.12.30 2277
39 윈도우7 인터넷 속도제한 해제 (QoS 제한 해제) taknim 2014.10.23 4129
38 findstr 사용법(linux grep 유사) taknim 2014.04.24 3384
37 코드페이지 변경 taknim 2014.02.11 3438
36 윈도7에서 mklink 명령을 이용한 심볼릭 디렉토리 생성하기 taknim 2013.11.15 4165
» [VBS] WScript 메소드 taknim 2013.01.29 6788
34 [VBS] WshShell.CreateShortcut - 바로가기 생성 taknim 2013.01.29 6191
33 xampp taknim 2012.12.13 4890
32 IIS 로그 분석 개요 및 무료 로그 분석 툴(LogParser) taknim 2012.11.23 7875
31 윈도우 7 정품 인증 secret taknim 2009.11.03 4
30 배치파일에서 날짜 계산하기 file taknim 2009.09.29 10540
29 명령 프롬프트 창이 보이지 않는 배치 파일 실행하기 taknim 2009.07.04 12849
28 알아두면 유용한 윈도우의 환경 변수들 taknim 2009.06.25 8712
27 윈도우 리부팅.끄기.잠금 아이콘 만들기 [1] taknim 2009.01.08 9609
26 Include 파일은 부모 디렉터리를 표시하기 위해 '..'를 사용할 수 없습니다. 와 같은 오류 발생 taknim 2009.01.06 9894
25 USB 메모리의 파일 시스템 FAT32를 NTFS로 변환하는 방법 taknim 2008.12.23 10194
24 윈도우 XP 한 폴더에 저장할 수 있는 파일의 최대 갯수? taknim 2008.12.23 12215
23 윈도우XP 속도를 빠르게 하는 Tip 총정리(21개) taknim 2008.12.21 7352