Skip to Content
author's profile photo Former Member
Former Member

Computer Info gathering

I am looking to see if it's possible to call certain Command Prompt functions through PowerBuilder code. For example, I can open a Command Prompt and type in wmic DISKDRIVE get SerialNumber to get the computer's serial numbers. How can I do this via Power Script?

Thanks,

Rick

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Mar 27, 2015 at 02:40 PM

    Here is some code that will get the serial number from a hard drive.

    OLEObject ole_wsh

    Any la_result

    String ls_SQL, ls_code

    ls_SQL = "Select * from Win32_LogicalDisk Where DeviceID = 'C:'"

    // define VBScript

    ls_code = 'Function rtnDisk()~r~n' &

    + 'Set objWMI = GetObject("winmgmts:\\.\root\cimv2")~r~n' &

    + 'Set colItems = objWMI.ExecQuery("'+ls_SQL+'")~r~n' &

    + 'For Each objItem in colItems~r~n' &

    + ' rtnDisk = objItem.VolumeSerialNumber~r~n' &

    + 'Next~r~n' &

    + 'End Function'

    ole_wsh = CREATE OLEObject

    ole_wsh.ConnectToNewObject("MSScriptControl.ScriptControl")

    // set the VBScript

    ole_wsh.Language = "vbscript"

    ole_wsh.AddCode(ls_code)

    // run the function

    la_result = ole_wsh.Eval("rtnDisk")

    ole_wsh.DisconnectObject()

    DESTROY ole_wsh

    MessageBox("Serial Number", String(la_result))

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 26, 2015 at 07:15 PM

    Hi Rick;

    1) use the ShellExecuteEx ( ) API

    2) Use PB's RUN ( ) method

    3) Use MS-Windows PowerShell API (Run & Wait)

    etc

    Either way you choose to perform the DOS Command from the above list, I think the key would be to redirect the result to a file and read the results from there. 😉

    Example:

    a) CMD wmic DISKDRIVE get SerialNumber >serialno.txt

    b) Use PB's FileReadEX ( ) method to read "serialno.txt" content & parse out the information you need.

    HTH

    Regards ... Chris

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 26, 2015 at 09:41 PM

    Depending on your requirements, you could also look for a windows API that you could call to return the information that you need. You could also take a look at the GetEnvironment function in PB if you can get the information you need from there. For me, calling a DOS command is the last resort.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 26, 2015 at 09:45 PM

    OLE scripting will give you better results than scraping a command prompt.

    Generally I would look at using the WbemScripting.SWbemLocator oleobject, to access the same WMI properties as your command in the prompt is accessing and displaying.

    For your specific example you could use FileSystemObject e.g

    oleobject fso, drive string ls_serialnumber

    fso = Create OleObject

    fso.ConnectToNewObject("Scripting.FileSystemObject")

    drive = fso.GetDrive('C:')

    ls_serialnumber = string(drive.SerialNumber)

    messagebox ( 'Serial Number of C:', ls_serialnumber )

    Destroy fso

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.