Search code examples
vbscript

Output result to a text file in vbs


Please help to correct my code of which I need to get the result to show on screen and record it to a text file.

Set WshShell = CreateObject("WScript.Shell")

MsgBox ConvertToKey(WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"))
WScript.Echo "ConvertToKey(WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"))" > C:\WindowsKey.txt

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

Solution

  • Here's your script with corrections. Note the elimination of the MsgBox line, the correction of the WScript.Echo line (with the elimination of the non-VBScript redirection code) and the addition of proper indentation:

    Set WshShell = CreateObject("WScript.Shell")
    WScript.Echo 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
    

    Now, instead of double-clicking the script, open a Cmd prompt and enter this command (change the script name to match your vbs file):

    cscript getkey.vbs>%temp%\WindowsKey.txt

    The redirection is a Cmd shell thing, not a VBScript thing.

    Also note that the above saves the file to your Temp folder. You were trying to save to a file on the root of C:. That will result in an accessed denied error.

    Alternatively, you can change the script to write directly to a file like this:

    Set WshShell = CreateObject("WScript.Shell")
    Key = ConvertToKey(WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"))
    
    Const ForWriting = 2
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Temp  = WshShell.ExpandEnvironmentStrings("%Temp%")
    Set oFile = oFSO.OpenTextFile(Temp & "\WindowsKey.txt",ForWriting,True)
    oFile.Write Key
    oFile.Close
    
    WScript.Echo Key
    
    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
    

    None of this is new and it's all covered in numerous other answers, some of which you were already pointed to. No doubt this question will be closed as a duplicate, but it seemed you needed a little extra help, so here it is.