VB 用法
怎么运行 VBScript:cscript、wscript、ASP、以及调试手段。
命令行运行
cscript //nologo hello.vbs| Option | Meaning |
|---|---|
//nologo | 不打印版本信息 |
//B | 屏蔽弹窗与输入 |
//T:30 | 最多运行 30 秒 |
//Job:xxx | 运行 .wsf 里指定的作业 |
双击运行
Double-clicking uses wscript: WScript.Echo becomes a dialog and WScript.StdOut is unavailable. To see console output when double-clicking, detect the host in the script or use a .wsf file that specifies cscript.
<!-- run.wsf:强制用 cscript 运行 -->
<job id="main">
<script language="VBScript" src="hello.vbs"/>
</job>在 ASP 里用
<%
Dim total, i
total = 0
For i = 1 To 10
total = total + i
Next
Response.Write "1 到 10 的和是 " & total
%>1 到 10 的和是 55There is no WScript object in ASP: output uses Response.Write and input uses Request. Same language core, different host, different available objects.
调试手段
' 1. 打印中间值
WScript.Echo "i = " & i & ", total = " & total
' 2. 断言式检查
If total < 0 Then
WScript.Echo "异常:total 不应为负"
WScript.Quit 1
End If
' 3. 出错时停止并报告
On Error Resume Next
DoSomething
If Err.Number <> 0 Then
WScript.Echo "错误 " & Err.Number & ":" & Err.Description
WScript.Quit 1
End IfWScript.Quit 1 returns a non-zero exit code so callers (batch files, Task Scheduler) can detect failure. Echoing without quitting makes a broken script look successful.
常见运行问题
| Symptom | Cause |
|---|---|
| No output | 用 wscript 跑了,StdOut 不可用 |
| Object required | Set 漏了,或对象为 Nothing |
| Permission denied | 写系统目录、注册表需要管理员 |
| Script blocked | 组策略或杀软禁止 WSH |
| Chinese garbled | 文件编码与代码页不一致 |
.vbs files must be saved as ANSI (GBK) or UTF-16 LE; UTF-8 garbles Chinese strings. This is the biggest friction between VBScript and modern tooling, which defaults to UTF-8.安全性
WSH can read and write files, edit the registry and launch processes, so it is a favourite of malicious scripts. Enterprises often disable it by policy or allow only signed scripts — if your script will not run on a corporate machine, ask IT about that policy.
命令行运行
cscript //nologo hello.vbs| 参数 | 含义 |
|---|---|
| 去横幅 | 不打印版本信息 |
| 批处理模式 | 屏蔽弹窗与输入 |
| 超时 | 最多运行 30 秒 |
| 作业 | 运行 .wsf 里指定的作业 |
双击运行
双击 .vbs 时用 wscript,WScript.Echo 变成弹窗,WScript.StdOut 不可用。想双击也能看到控制台输出,就在脚本里判断宿主,或干脆用 .wsf 文件指定 cscript。
<!-- run.wsf:强制用 cscript 运行 -->
<job id="main">
<script language="VBScript" src="hello.vbs"/>
</job>在 ASP 里用
<%
Dim total, i
total = 0
For i = 1 To 10
total = total + i
Next
Response.Write "1 到 10 的和是 " & total
%>1 到 10 的和是 55ASP 里没有 WScript 对象,输出用 Response.Write,读取输入用 Request。同一份语言核心,宿主不同、可用对象也不同。
调试手段
' 1. 打印中间值
WScript.Echo "i = " & i & ", total = " & total
' 2. 断言式检查
If total < 0 Then
WScript.Echo "异常:total 不应为负"
WScript.Quit 1
End If
' 3. 出错时停止并报告
On Error Resume Next
DoSomething
If Err.Number <> 0 Then
WScript.Echo "错误 " & Err.Number & ":" & Err.Description
WScript.Quit 1
End IfWScript.Quit 1 返回非零退出码,调用方(批处理、任务计划)能据此判断失败。只 Echo 不退出,脚本看起来成功了,实际什么都没做。
常见运行问题
| 现象 | 原因 |
|---|---|
| 没有输出 | 用 wscript 跑了,StdOut 不可用 |
| 对象必需 | Set 漏了,或对象为 Nothing |
| 权限不足 | 写系统目录、注册表需要管理员 |
| 脚本被拦 | 组策略或杀软禁止 WSH |
| 中文乱码 | 文件编码与代码页不一致 |
.vbs 文件必须存成 ANSI(GBK) 或 UTF-16 LE,存成 UTF-8 会让中文字符串乱码。这是 VBScript 与现在工具链最大的摩擦点——现代编辑器默认存 UTF-8。安全性
WSH 能读写文件、改注册表、启动进程,因此常被恶意脚本利用。企业环境通常通过组策略禁用 WSH,或限制只能运行签名脚本——如果你的脚本在公司电脑上跑不起来,先问 IT 是不是这条策略。