【问题标题】:VBS Script - Need to change msgbox to echoVBS 脚本 - 需要将 msgbox 更改为 echo
【发布时间】:2015-04-15 23:30:24
【问题描述】:

我很难将此脚本转换为 echo 而不是 msgbox 输出。

我需要输出到 C:\key.log 中的文本文件,而不是带有密钥的消息框。

有人知道怎么做吗?谢谢!

Set WshShell = CreateObject("WScript.Shell")
MsgBox ConvertToKey(WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"))

Function ConvertToKey(Key)
Const KeyOffset = 52
i = 28
Chars = "BCDFGHJKMPQRTVWXY2346789"
Do
Cur = 0
x = 14
Do
Cur = Cur * 256
Cur = Key(x + KeyOffset) + Cur
Key(x + KeyOffset) = (Cur \ 24) And 255
Cur = Cur Mod 24
x = x -1
Loop While x >= 0
i = i -1
KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput
If (((29 - i) Mod 6) = 0) And (i <> -1) Then
i = i -1
KeyOutput = "-" & KeyOutput
End If
Loop While i >= 0
ConvertToKey = KeyOutput
End Function

【问题讨论】:

    标签: vbscript output echo msgbox


    【解决方案1】:

    试试这样:

    Option Explicit
    Dim WshShell,RegKey,WindowsKey,LogFile,fso
    Set WshShell = CreateObject("WScript.Shell")
    Set fso = CreateObject("Scripting.FileSystemObject")
    RegKey = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"
    WindowsKey = ConvertToKey(WshShell.RegRead(RegKey))
    LogFile = "c:\key.log"
    if fso.FileExists(LogFile) Then 
        fso.DeleteFile LogFile
    end If
    MsgBox WindowsKey,VbInformation,WindowsKey 
    Call WriteLog(WindowsKey,LogFile)
    WshShell.Run LogFile
    '***********************************************************************************************************
    Function ConvertToKey(Key)
        Const KeyOffset = 52
        Dim i,Chars,Cur,x,KeyOutput
        i = 28
        Chars = "BCDFGHJKMPQRTVWXY2346789"
        Do
            Cur = 0
            x = 14
            Do
                Cur = Cur * 256
                Cur = Key(x + KeyOffset) + Cur
                Key(x + KeyOffset) = (Cur \ 24) And 255
                Cur = Cur Mod 24
                x = x -1
            Loop While x >= 0
            i = i -1
            KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput
            If (((29 - i) Mod 6) = 0) And (i <> -1) Then
                i = i -1
                KeyOutput = "-" & KeyOutput
            End If
        Loop While i >= 0
        ConvertToKey = KeyOutput
    End Function
    '*************************************************************************************************************
    Sub WriteLog(strText,LogFile)
        Dim fs,ts 
        Const ForAppending = 8
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
        ts.WriteLine strText
        ts.Close
    End Sub
    '**************************************************************************************************************
    

    【讨论】:

    • 这太棒了,我最终使用了这个脚本,因为它在为用户提取 Windows 密钥后打开了文本文件。谢谢!
    【解决方案2】:

    下面的代码将 echo 语句替换为对 FileSystemObject (FSO) 的调用。此对象可以添加、移动、更改、创建或删除 Web 服务器或桌面上的文件夹(目录)和文件。我们特别感兴趣的方法是CreateTextFile

    DIM fso, NewsFile, WshShell
    
    Set WshShell = CreateObject("WScript.Shell")
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set write2File = fso.CreateTextFile("C:\key.log", True)
    write2File.WriteLine(ConvertToKey(WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId")))
    write2File.Close
    
    Function ConvertToKey(Key)
    Const KeyOffset = 52
    i = 28
    Chars = "BCDFGHJKMPQRTVWXY2346789"
    Do
    Cur = 0
    x = 14
    Do
    Cur = Cur * 256
    Cur = Key(x + KeyOffset) + Cur
    Key(x + KeyOffset) = (Cur \ 24) And 255
    Cur = Cur Mod 24
    x = x -1
    Loop While x >= 0
    i = i -1
    KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput
    If (((29 - i) Mod 6) = 0) And (i <> -1) Then
    i = i -1
    KeyOutput = "-" & KeyOutput
    End If
    Loop While i >= 0
    ConvertToKey = KeyOutput
    End Function
    

    【讨论】:

    • 运行此 .VBS 脚本时出现以下错误。 - 字符:1 错误:所需对象:“WshShell”代码:800A01A8 来源:Microsoft VBScript 运行时错误
    • 他忘了添加这一行:Set WshShell = CreateObject("WScript.Shell"),所以如果你添加这一行,它应该可以工作;)
    • @WorkSmarter 你应该得到我的 +1 ;)
    • @WorkSmarter 我将此脚本用作在后台工作的辅助选项,非常感谢!
    【解决方案3】:
    Wscript.echo "message"
    

    使用 wscript 运行时输出一个 msgbox,并在命令提示符下使用 cscript 运行时回显到 STDOUT。

    cmd /c cscript //nologo c:\somepath\somefile.vbs > file.txt
    

    【讨论】:

    • 所以只需要将msgbox替换成wscript.echo并正确启动即可。
    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    相关资源
    最近更新 更多