on 03-26-2015 5:34 PM
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
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))
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
25 | |
12 | |
9 | |
6 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.