cancel
Showing results for 
Search instead for 
Did you mean: 

Computer Info gathering

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

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))

Former Member
0 Kudos

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

oleobjectfso, drive
stringls_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

Former Member
0 Kudos

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.

Former Member
0 Kudos

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