Windows 脚本主机概述

Windows 脚本主机概述

首先,什么是WSH?

http://search.microsoft.com/zh-cn/results.aspx?form=MSHOME&setlang=zh-cn&q=WSH

Windows Script Host (WSH) is a Windows administration tool.

http://technet.microsoft.com/zh-cn/subscriptions/shzd7dy4(v=vs.80).aspx

WSH creates an environment for hosting scripts. That is, when a script arrives at your computer, WSH plays the part of the host — it makes objects and services available for the script and provides a set of guidelines within which the script is executed. Among other things, Windows Script Host manages security and invokes the appropriate script engine.

WSH Objects and Services

Windows Script Host provides several objects for direct manipulation of script execution, as well as helper functions for other actions. Using these objects and services, you can accomplish tasks such as the following:

  • Print messages to the screen
  • Run basic functions such as CreateObject and GetObject
  • Map network drives
  • Connect to printers
  • Retrieve and modify environment variables
  • Modify registry keys

Windows Script Host Object Model

http://technet.microsoft.com/zh-cn/subscriptions/a74hyyw0(v=vs.80).aspx

关于WSH的架构信息,这篇文章描述的不错

http://technet.microsoft.com/zh-tw/library/dd180733.aspx

' WSHInfo.vbs 顯示 WSH 的若干訊息

Option Explicit

Dim strHostName,strHostFullPathName,strScriptFullName

Dim strMessage

strHostName = WScript.Name & " " & WScript.Version & vbCrLf

strHostFullPathName = "目前的 WSH 程式是由 " & WScript.FullName &

"所執行" & vbCrLf

strScriptFullName = "所執行的 WSH 程式是 " & WScript.ScriptFullName

strMessage = strHostName & vbCrLf & strHostFullPathName &

vbCrLf & strScriptFullName

MsgBox strMessage, vbInformation, "WSH 相關訊息"  

下面是我写的一段测试代码:

count=0

set shell=wscript.createObject("wscript.shell")

password = inputbox("pig pangpang, my password please:")

do while password<>"888888"

       count = count +1

       msgbox "error password,exit now " & count & " times",48,"authentication"

             

       if count=10 then

              msgbox "you are not lucky, little girl. evil is fallen!",0,"DEAD PC"

              shell.run "shutdown -r -t 60",1

              exit do

       elseif count =9 then

              msgbox "there is only 1 times, reaching to 10 your PC will restart!",48,"Critical!"

       end if

       password = inputbox("pig pangpang, my password please:")

loop

shell.run "notepad.exe",1

wscript.Sleep 200

shell.AppActivate "无标题 - 记事本"

wscript.Sleep 200

'set msg = shell.readkeys;

'对于需要与Shift、Ctrl、Alt三个控制键组合的按键,SendKeys使用特殊字符来表示:Shift —— +;Ctrl —— ^;Alt —— %

shell.sendkeys "hello,pig pangpang!"

shell.sendKeys "{TAB}{TAB}"

shell.sendKeys "it's so amazing...."

shell.sendkeys "{enter}"

shell.sendkeys "{enter}"

shell.sendkeys "{f5}"

shell.sendkeys "{enter}"

shell.sendKeys "by Big Uncle"

shell.sendkeys "^%+{DEL}"

原文地址:https://www.cnblogs.com/flaaash/p/2861128.html