获取该vbs文件所在的目录
'获取该vbs文件所在的目录
Function getCurrentDir()
getCurrentDir = createObject("Scripting.FileSystemObject").GetFolder(".").Path
End Function
运行cmd
Set oshell = createobject("WSCript.shell")
oshell.run "cmd"
run函数说明:
WScript.Shell是WshShell对象的ProgID,创建WshShell对象可以运行程序、操作注册表、创建快捷方式、访问系统文件夹、管理环境变量。
该对象有一个run方法。
Run 方法创建一个新的进程,该进程以 intWindowStyle 窗口样式执行 strCommand。
语法
WshShell.Run (strCommand, [intWindowStyle], [blnWaitOnReturn])
参数
- strCommand
在 strCommand 参数内部的环境变量被自动扩展。 - intWindowStyle
这是为新进程在 STARTUPINFO 结构内设置的 wShowWindow 元素的值。其意义与 ShowWindow 中的 nCmdShow 参数相同,可取以下值之一。名称 值 含义
SW_HIDE
0 隐藏窗口并激活另一窗口。
SW_MINIMIZE
6 最小化指定窗口并激活按 Z 序排序的下一个顶层窗口。
SW_RESTORE
9 激活并显示窗口。若窗口是最小化或最大化,则恢复到原来的大小和位置。在还原应用程序的最小化窗口时,应指定该标志。
SW_SHOW
5 以当前大小和位置激活并显示窗口。
SW_SHOWMAXIMIZED
3 激活窗口并以最大化显示该窗口。
SW_SHOWMINIMIZED
2 激活窗口并以最小化显示该窗口。
SW_SHOWMINNOACTIVE
7 最小化显示窗口。活动窗口保持活动。
SW_SHOWNA
8 以当前状态显示窗口。活动窗口保持活动。
SW_SHOWNOACTIVATE
4 按窗口最近的大小和位置显示。活动窗口保持活动。
SW_SHOWNORMAL
1 激活并显示一个窗口。若窗口是最小化或最大化,则恢复到其原来的大小和位置。 - blnWaitOnReturn
如果未指定 blnWaitOnReturn 或其值为 FALSE,则该方法立即返回到脚本继续执行而不等待进程结束。
若 blnWaitOnReturn 设为 TRUE,则 Run 方法返回由应用程序返回的任何错误代码。如果未指定 blnWaitOnReturn 或其值为 FALSE,则 Run 返回错误代码 0(zero)。
Set WshShell=Wscript.CreateObject(“Wscript.Shell”)
WshShell.Run “notepad.exe”
保存为notepad.vbs文件,双击会打开notepad。
以管理员方式运行cmd
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "cmd", "", "", "runas", 1
方法说明:
ShellExecute method
Run a script or application in the Windows Shell.
Syntax
.ShellExecute “application”, “parameters”, “dir”, “verb”, window
.ShellExecute ‘some program.exe’, ‘“some parameters with spaces”’, , “runas”, 1
Key
- application
The file to execute (required) - parameters
Arguments for the executable - dir
Working directory - verb
The operation to execute (runas/open/edit/print) - window
View mode application window (normal=1, hide=0, 2=Min, 3=max, 4=restore, 5=current, 7=min/inactive, 10=default)
Note that double " and single ’ quotes that can be used to delimit paths with spaces.
The runas verb is undocumented but can be used to elevate permissions. When a script is run with elevated permissions several aspects of the user environment may change: The current directory, the current TEMP folder and any mapped drives will be disconnected.
runas will fail if you are running in WOW64 (a 32 bit process on 64 bit windows) for example %systemroot%\syswow64\cmd.exe …
The ShellExecute method is a member of the IShellDispatch2 object.
Examples
Run a batch script with elevated permissions, flag=runas:
Set objShell = CreateObject(“Shell.Application”)
objShell.ShellExecute “E:\demo\batchScript.cmd”, “”, “”, “runas”, 1
Run a VBScript with elevated permissions, flag=runas:
Set objShell = CreateObject(“Shell.Application”)
objShell.ShellExecute “cscript”, “E:\demo\vbscript.vbs”, “”, “runas”, 1
“If you don’t execute your ideas, they die” ~ Roger Von Oech
发送Http请求
Get请求
Function sendHttpGet(strUrl)
Dim strA
Dim http
Set http = CreateObject("Msxml2.ServerXMLHTTP")
'strA = http.open("GET", "http://www.baidu.com", False)
strA = http.open("GET", strUrl, False)
http.send
'MsgBox http.Status
'MsgBox http.responsetext
sendHttpGet = http.responsetext
End Function
Post请求
Function sendHttpPost(posturl,params)
Dim http
Set http = CreateObject("MSXML2.ServerXMLHTTP")
http.Open "POST",posturl,False,"",""
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.Send(params)
If http.Status = "200" Then
sendHttpPost = http.responseText
Else
sendHttpPost = http.Status & "<br />" & http.responseText
End If
Set http = nothing
End Function
' 使用
ret = sendHttpPost("http://127.0.0.1:9090/", "{hello=123}")
MsgBox ret
可以这样设置超时
Public Const xmlDNSTimeout = 10000 '解析 DNS 的超时时间,单位:毫秒
Public Const xmlCONTimeout = 10000 '建立连接的超时时间,单位:毫秒
Public Const xmlSNDTimeout = 30000 '发送数据的超时时间,单位:毫秒
Public Const xmlRCVTimeout = 30000 '接收数据的超时时间,单位:毫秒
...
set http=Server.createobject("Msxml2.ServerXMLHTTP")
http.setTimeouts xmlDNSTimeout, xmlCONTimeout, xmlSNDTimeout, xmlRCVTimeout