返回列表 发帖

[原创] vbs版cmd

'By zqz0012005@www.bathome.cn
'Version: 0.3     2008-06-18
On Error Resume Next
host = WScript.FullName
f0 = WScript.ScriptFullName
dp0 = Left( f0, InstrRev(f0,"\") )
if LCase( right(host, len(host)-InStrRev(host,"\")) ) = LCase("wscript.exe") then
    CreateObject("WScript.Shell").run "cmd /d /ccolor f9&title vbs-cmd" &_
        "&cscript //logo """ & f0 & chr(34)
    WScript.Quit
end if
set ws = CreateObject("WScript.Shell")
set wn = CreateObject("WScript.Network")
set fso = CreateObject("Scripting.FileSystemObject")
set dict = CreateObject("Scripting.Dictionary")
set sh = CreateObject("Shell.Application")
set wmi = GetObject("winmgmts:{(debug, security, shutdown)}!\\.\root\cimv2")
'set colps = wmi.instancesOf("win32_process")
'set colps = GetObject("winmgmts:\\.\root\cimv2:win32_process").instances_
set stdin = WScript.stdin
set stdout = WScript.stdout
stdout.writeline "可以直接运行vbs语句,要运行cmd命令,请在命令前加'cmd '(cmd空格)"
do
    stdout.writeBlankLines(1)
    stdout.write ws.CurrentDirectory & vbCrLf & "# "
    s=Trim(stdin.readLine)
    Select Case 0
        Case StrComp(s,"exit",1) wsh.quit
        Case StrComp(left(s,2),"cd",1)
            if mid(s,3)="" then
                wsh.echo ws.CurrentDirectory
            elseif mid(s,3,1)=" " then
                ws.CurrentDirectory = LTrim(mid(s,3))
            else
                execStatementBlock
            end if
        Case StrComp(left(s,4),"cmd ",1)
            set oexec=ws.exec( "cmd /d /q /c" & LTrim(mid(s,4)) & "&exit" )
            'oexec.stdin.writeLine vbCrLf & "exit"
            stdout.write oexec.stdout.readall & oexec.stderr.readall
            set oexec=nothing
        Case StrComp(left(s,5),"wmic ",1)
            set oexec=ws.exec("cmd /d /q /c echo exit|" & s)
            'oexec.stdin.writeLine vbCrLf & "exit"
            stdout.write oexec.stdout.readall & oexec.stderr.readall
            set oexec=nothing
        Case StrComp(s,"help",1) call help()
        Case StrComp(s,"") 'do nothing
        Case Else
            LastCode = execStatementBlock()
        'Case Else execute s
        'Case Else wsh.echo "[" & s & "]"
    End Select
    showErrMsg
loop
function execStatementBlock()
dim i, t
ReDim a(0)
a(0) = s
stdout.write "More? "
t = Trim(stdin.readLine)
do until t=""
i = i + 1
ReDim Preserve a(i)
a(i) = t
stdout.write "More? "
t = Trim(stdin.readLine)
loop
execute Join(a, ":")
execStatementBlock = Join(a, vbCrLf)
end function
sub help()
stdout.writeline vbCrLf & "可以直接运行vbs语句,在More? 提示符后直接回车以完成输入。"
stdout.writeline "要运行cmd命令,请在命令前加'cmd '(cmd空格)。wmic类似。" & vbCrLf
stdout.writeLine "显示环境变量:printenv"
stdout.writeLine "f0 = " & f0
stdout.writeLine "dp0 = " & dp0
stdout.writeLine "常用对象:"
stdout.writeLine "ws = CreateObject(""WScript.Shell"")"
stdout.writeLine "wn = CreateObject(""WScript.Network"")"
stdout.writeLine "fso = CreateObject(""Scripting.FileSystemObject"")"
stdout.writeLine "dict = CreateObject(""Scripting.Dictionary"")"
stdout.writeLine "sh = CreateObject(""Shell.Application"")"
stdout.writeLine "wmi = GetObject(""winmgmts:{(debug, security, shutdown)}!\\.\root\cimv2"")"
end sub
sub showErrMsg()
if err.number<>0 then
    wsh.echo err.source & "[errno=" & err.number & "]: " & err.description
    err.clear
end if
end sub
sub calc(expression)
wsh.echo eval(expression)
end sub
sub ps()
dim colProc, proc
set colProc = wmi.ExecQuery("select * from Win32_Process")
for each proc in colProc
    wsh.echo proc.name
    wsh.echo proc.ExecutablePath
    wsh.echo proc.CommandLine
    wsh.echo ""
next
end sub
sub printenv()
dim envType, envset, env, i
envType = Array("system","process","user","Volatile")
for i=0 to UBound(envType)
  wsh.echo vbCrLf & envType(i) & "类型的环境变量:"
  set envset = ws.Environment(envType(i))
  for each env in envset
    wsh.echo env
  next
next
end sub
sub env()
    printenv()
end sub
sub dir()
end sub
'To be continued to complete and improveCOPY
命令行参考:hh.exe ntcmds.chm::/ntcmds.htm
求助者请拿出诚心,别人才愿意奉献热心!
把查看手册形成条件反射!

回复 4楼 的帖子

就是这个功能啊,核心都是vbs+cscript.exe

cmd命令是次要功能,因为有些命令无法获取标准流 或 用vbs函数代替进行模拟,
而且cmd命令失去交互能力了,被exec所劫持。
命令行参考:hh.exe ntcmds.chm::/ntcmds.htm
求助者请拿出诚心,别人才愿意奉献热心!
把查看手册形成条件反射!

TOP

有想过 cmd版vbs(就cscript模式吧)
可以允许 将vbs的函数像命令一样输入。
[url=][/url]

TOP

的确比较难搞,我花了很多时间才搞定运行不带参数的 comp 之类的命令,但是对于不带参数的 wmic 之类的命令就没办法了,不知道有没有什么好的办法?

TOP

谢谢版主,可以说明一下用途与一般cmd之差异?或给使用范例 VBS不熟?
# help

可以直接运行vbs语句,在More? 提示符后直接回车以完成输入。
要运行cmd命令,请在命令前加'cmd '(cmd空格)。wmic类似。

显示环境变量:printenv
f0 = D:\scripts\TEST\0000\20100621A\vbs-cmd\vbs-cmd.vbs
dp0 = D:\scripts\TEST\0000\20100621A\vbs-cmd\
常用对象:
ws = CreateObject("WScript.Shell")
wn = CreateObject("WScript.Network")
fso = CreateObject("Scripting.FileSystemObject")
dict = CreateObject("Scripting.Dictionary")
sh = CreateObject("Shell.Application")
wmi = GetObject("winmgmts:{(debug, security, shutdown)}!\\.\root\cimv2")

TOP

返回列表