将Base 36添加到Base 10转换

时间:2016-05-17 14:36:51

标签: vbscript base

我正在尝试构建用于将数字转换为指定基数的this previous post

在我的情况下,我有一个查找序列号的vbs脚本,并将字母数字字符(基数36)输出到txt文件。

我想在txt文件中添加序列号的基数10值。所以我得到这样的输出:

Serial Number:  ABC1234
Service Code:   22453156048

这是我的出发点。这个脚本运行并给我一个带序列号的txt文件。

On Error Resume Next

' Initialize WSH objects
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Shell = WScript.CreateObject("WScript.Shell")
Set WNetwork = WScript.CreateObject("WScript.Network")

' Initialize WMI
Set WMI = GetObject("winmgmts:\\.\root\cimv2")

' Find client machine name
Set ComputerInfo = WMI.ExecQuery("Select * from Win32_ComputerSystem")
For Each temp In Computerinfo
  WorkstationName = WNetwork.ComputerName
Next

Set Bios = WMI.ExecQuery("Select * from Win32_BIOS")
For Each temp In Bios
  SerN = temp.SerialNumber
Next

' Create Logfiles
Dim sDateTimeStamp
sDateTimeStamp = cStr(Year(now())) & _
                 Pad(cStr(Month(now())),2) & _
                 Pad(cStr(Day(now())),2) & _
                 Pad(cStr(Hour(now())),2) & _
                 Pad(cStr(Minute(now())),2) & _
                 Pad(cStr(Second(now())),2)

Function Pad(CStr2Pad, ReqStrLen)
  Dim Num2Pad

  Pad = CStr2Pad
  If Len(CStr2Pad) < ReqStrLen Then
    Num2Pad = String((ReqStrlen - Len(CStr2Pad)), "0")
    Pad = Num2Pad & CStr2Pad 
  End If
End Function

Set LogFile = FSO.CreateTextFile("c:\logs\" & WorkstationName & "_" & sDateTimeStamp & ".txt")

Set EnviromentVariables = Shell.Environment("PROCESS")
LogFile.Writeline("Serial Number:" & space(2) & SerN)

但是当我尝试从链接的帖子添加脚本时,我没有得到任何输出文件。

On Error Resume Next

' Initialize WSH objects
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Shell = WScript.CreateObject("WScript.Shell")
Set WNetwork = WScript.CreateObject("WScript.Network")

' Initialize WMI
Set WMI = GetObject("winmgmts:\\.\root\cimv2")

' Find client machine name
Set ComputerInfo = WMI.ExecQuery("Select * from Win32_ComputerSystem")
For Each temp In Computerinfo
  WorkstationName = WNetwork.ComputerName
Next

Set Bios = WMI.ExecQuery("Select * from Win32_BIOS")
For Each temp In Bios
  SerN = temp.SerialNumber

  Function ToBase(ByVal n, b)
    b = 10
    n = SerN
    If b < 2 Or b > 36 Then Exit Function
    Const SYMBOLS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Do
      ToBase = Mid(SYMBOLS, n Mod b + 1, 1) & ToBase
      n = Int(n / b)
    Loop While n > 0
  End Function

  SrvCode = ToBase(SerN,10)
Next

' Create Logfiles
Dim sDateTimeStamp
sDateTimeStamp = cStr(Year(now())) & _
                 Pad(cStr(Month(now())),2) & _
                 Pad(cStr(Day(now())),2) & _
                 Pad(cStr(Hour(now())),2) & _
                 Pad(cStr(Minute(now())),2) & _
                 Pad(cStr(Second(now())),2)

Function Pad(CStr2Pad, ReqStrLen)
  Dim Num2Pad

  Pad = CStr2Pad
  If Len(CStr2Pad) < ReqStrLen Then
    Num2Pad = String((ReqStrlen - Len(CStr2Pad)), "0")
    Pad = Num2Pad & CStr2Pad 
  End If
End Function

Set LogFile = FSO.CreateTextFile("c:\logs\" & WorkstationName & "_" & sDateTimeStamp & ".txt")

Set EnviromentVariables = Shell.Environment("PROCESS")
LogFile.Writeline("Serial Number:" & space(2) & SerN)
LogFile.Writeline("Service Code:" & space(3) & SrvCode)

2 个答案:

答案 0 :(得分:0)

VBScript中不允许嵌套函数定义。这应该会引发语法错误,尽管全局On Error Resume Next(你不应该首先使用它)。

将函数定义放在全局范围内(循环外),例如通过改变这个:

Set Bios = WMI.ExecQuery("Select * from Win32_BIOS")
For Each temp In Bios
  SerN = temp.SerialNumber

  Function ToBase(ByVal n, b)
    b = 10
    n = SerN
    If b < 2 Or b > 36 Then Exit Function
    Const SYMBOLS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Do
      ToBase = Mid(SYMBOLS, n Mod b + 1, 1) & ToBase
      n = Int(n / b)
    Loop While n > 0
  End Function

  SrvCode = ToBase(SerN,10)
Next

进入这个:

Function ToBase(ByVal n, b)
  If b < 2 Or b > 36 Then Exit Function
  Const SYMBOLS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  Do
    ToBase = Mid(SYMBOLS, n Mod b + 1, 1) & ToBase
    n = Int(n / b)
  Loop While n > 0
End Function

Set Bios = WMI.ExecQuery("Select * from Win32_BIOS")
For Each temp In Bios
  SerN = temp.SerialNumber
  SrvCode = ToBase(SerN,10)
Next

请务必删除行b = 10n = SerN。他们搞砸了传递给参数bn的值。

答案 1 :(得分:0)

信用&gt;&gt; http://www.4guysfromrolla.com/webtech/032400-1.shtml

我无法使用ToBase函数运行计算。但是通过一些谷歌搜索,我找到了上面的链接,我能够使用引用的函数来获得所需的输出。现在,这允许我添加到现有脚本以在远程查找Dell计算机服务标签时获取快速服务代码。

所以这是给出预期输出的最终工作脚本。

' Initialize WSH objects
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Shell = WScript.CreateObject("WScript.Shell")
Set WNetwork = WScript.CreateObject("WScript.Network")

' Initialize WMI
Set WMI = GetObject("winmgmts:\\.\root\cimv2")

' Find client machine name
Set ComputerInfo = WMI.ExecQuery("Select * from Win32_ComputerSystem")
For Each temp In Computerinfo
  WorkstationName = WNetwork.ComputerName
Next

Function baseN2dec(value, inBase)
  'Converts any base to base 10

  Dim strValue, i, x, y

  strValue = StrReverse(CStr(UCase(value)))

  For i = 0 To Len(strValue)-1
    x = Mid(strValue, i+1, 1)
    If Not isNumeric(x) Then
      y = y + ((Asc(x) - 65) + 10) * (inBase ^ i)
    Else
      y = y + ((inBase ^ i) * CInt(x))
    End If
  Next

  baseN2dec = y
End Function

Set Bios = WMI.ExecQuery("Select * from Win32_BIOS")
For Each temp In Bios                                
  SerN = temp.SerialNumber                            
  SrvCd = baseN2dec(SerN, 36)
Next

' Create Logfiles
Dim sDateTimeStamp
sDateTimeStamp = cStr(Year(now())) & _
                 Pad(cStr(Month(now())),2) & _
                 Pad(cStr(Day(now())),2) & _
                 Pad(cStr(Hour(now())),2) & _
                 Pad(cStr(Minute(now())),2) & _
                 Pad(cStr(Second(now())),2)

Function Pad(CStr2Pad, ReqStrLen)
  Dim Num2Pad

  Pad = CStr2Pad
  If Len(CStr2Pad) < ReqStrLen Then
    Num2Pad = String((ReqStrlen - Len(CStr2Pad)), "0")
    Pad = Num2Pad & CStr2Pad
  End If
End Function

Set LogFile = FSO.CreateTextFile("c:\logs\" & WorkstationName & "_" & sDateTimeStamp & ".txt")

Set EnviromentVariables = Shell.Environment("PROCESS")
LogFile.Writeline("Serial Number:" & space(2) & SerN)
LogFile.Writeline("Service Code:" & space(3) & SrvCd)