vbscript

Return only laptop serial number in vbscript


I want to get laptop serial number only using vbscript. I 've run following command:

set oShell =WScript.CreateObject("WScript.Shell")


comspec=oShell.ExpandEnvironmentStrings("%comspec%")

answer=oShell.Exec (comspec & " /c wmic bios get serialnumber").StdOut.ReadAll()

MsgBox answer

Here answer contains:

SerialNumber
56456465jhgjhsdf456

But I dont want "SerialNumber", I just only want "56456465jhgjhsdf456", how can I fetch part of string in vbscript?


Solution

  • It's a string with every line output by the program.

    Assuming the value your looking for is always the last line of the output, split the string and take the last element.

    lines = Split(answer, vbCrLf)
    serial = lines(Ubound(lines))
    

    If you get no value in serial it's possible your output has extra newline characters at the end so you would need to do some extra processing.