WMI and Purebasic

Just starting out? Need help? Post your questions and find answers here.
sabre
User
User
Posts: 11
Joined: Mon Apr 26, 2004 11:41 pm

WMI and Purebasic

Post by sabre »

Hi Newbie question is it possible to use Purebasic to access and use WMI (Windows Management Instrumentation)? I can develop vbscript files pretty easy to do this but wanted to know if purebasic is able to accomplish the same.


Example vbscript WMI code can you conver the following code to purebasic?

Code: Select all

On Error Resume Next
Dim strComputer
Dim objWMIService
Dim propValue
Dim SWBemlocator
Dim UserName
Dim Password
Dim colItems

strComputer = "."
UserName = ""
Passord = ""
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"\root\CIMV2",UserName,Password)
Set colItems = objWMIService.ExecQuery("Select * from Win32_StartupCommand",,48)
For Each objItem in colItems
	WScript.Echo "Caption: " & objItem.Caption
	WScript.Echo "Command: " & objItem.Command
	WScript.Echo "Description: " & objItem.Description
	WScript.Echo "Location: " & objItem.Location
	WScript.Echo "Name: " & objItem.Name
	WScript.Echo "SettingID: " & objItem.SettingID
	WScript.Echo "User: " & objItem.User
Next
Thanks in advance for all the help
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post by Beach »

You could call the script from PB and use the output. Or you could create the script file from within PB, use it, then delete it. This is how I would do that:

Code: Select all

quote.s = Chr(34)
cr.s = Chr(10)

vbs$ = "On Error Resume Next " + cr
vbs$ + "Dim strComputer " + cr
vbs$ + "Dim objWMIService" + cr
vbs$ + "Dim propValue " + cr 
vbs$ + "Dim SWBemlocator " + cr
vbs$ + "Dim UserName " + cr
vbs$ + "Dim Password " + cr
vbs$ + "Dim colItems " + cr
vbs$ + "Dim objFS, objFile, LineData" + cr
vbs$ + "Set objFS = CreateObject(" + quote + "Scripting.fileSystemObject" + quote + ")" + cr
vbs$ + "Set objFile = objFS.CreateTextFile(" + quote + "wmi_info.txt" + quote + ", TRUE)" + cr
vbs$ + "strComputer = " + quote + "." + quote + cr
vbs$ + "UserName = " + quote + quote + cr
vbs$ + "Passord = " + quote + quote + cr
vbs$ + "Set SWBemlocator = CreateObject(" + quote + "WbemScripting.SWbemLocator" + quote + ") " + cr
vbs$ + "Set objWMIService = SWBemlocator.ConnectServer(strComputer," + quote + "\root\CIMV2" + quote + ",UserName,Password) " + cr
vbs$ + "Set colItems = objWMIService.ExecQuery(" + quote + "Select * from Win32_StartupCommand" + quote + ",,48) " + cr
vbs$ + "For Each objItem in colItems " + cr
vbs$ + "  LineData = objItem.Caption & " + quote + "|" + quote + "_" + cr
vbs$ + "               & objItem.Command & " + quote + "|" + quote + "_" + cr
vbs$ + "               & objItem.Description & " + quote + "|" + quote + "_" + cr
vbs$ + "               & objItem.Location & " + quote + "|" + quote + "_" + cr
vbs$ + "               & objItem.Name & " + quote + "|" + quote + "_" + cr
vbs$ + "               & objItem.SettingID & " + quote + "|" + quote + "_" + cr
vbs$ + "               & objItem.User" + cr
vbs$ + "  objFile.WriteLine(LineData)" + cr
vbs$ + "Next" + cr
vbs$ + "objFile.Close" + cr
vbs$ + "Set objFS = Nothing"

vbfilename.s = "temp.vbs"
fhnd = CreateFile(#PB_Any,vbfilename)
WriteString(vbs$)
CloseFile(fhnd)

RunProgram(vbfilename,"","",1)

DeleteFile(vbfilename)

If FileSize("wmi_info.txt") > 0
  fhnd = ReadFile(#PB_Any,"wmi_info.txt")
  While Eof(fhnd) = #False
    Debug ReadString() 
  Wend
  CloseFile(fhnd)
EndIf
User avatar
bingo
Enthusiast
Enthusiast
Posts: 210
Joined: Fri Apr 02, 2004 12:21 pm
Location: germany/thueringen
Contact:

Post by bingo »

["1:0>1"]
sabre
User
User
Posts: 11
Joined: Mon Apr 26, 2004 11:41 pm

Thanks

Post by sabre »

Thanks for all the help
Post Reply