#173065 - 2007-01-22 11:13 PM
Writing to Access file...
|
wreded
Fresh Scripter
Registered: 2003-11-06
Posts: 28
Loc: Montana, USA
|
i thought i had this down, now i'm not so sure. Short, sweet code to check one thing on lots of computers and put what it found in an Access database. "Borrowed" code from Rich Weber's inventory script to handle putting 3 fields in a database, substituting my database file name for his. The problem is that nothing ever gets put in the bucket. Watching the directory i can see that the file is opened, but nothing is ever there when i check.
$=setoption("wrapateol","on")
Dim $DBpath, $DBFile
;$DBpath = @Scriptdir ;Modify this to represent your database location if using an access database.
$DBpath = "F:\New Folder"
$DBfile = $DBpath +"\Computers.mdb"
CLS
At (5,1) "Asset Audit Script Processing..."
Sleep 2
CLS
Break ON CLS
If Exist($DBpath) = 0 ;Abort if no Database exists when using an access database.
? "IT Assets Database Not Found. Aborting..."
Sleep 3
Exit 1
Else
Main($DBFile)
EndIf
Function Main($DBFile)
Dim $CNstring, $CMDtxt, $cn, $cmd, $rs
;*** CONFIGURE DATABASE CONNECTION STRING ***
$CNstring="Driver={Microsoft Access Driver (*.mdb)}; DBQ=$DBFile" ;Next line for sql server
;$CNstring = "Driver={SQL Server};Server=MyServer;database=MyDatabase;uid=UserName;pwd=password"
$CMDtxt = "select * from Branded where computername = '@WKSTA'"
$cn = CreateObject ("ADODB.Connection")
$cmd = CreateObject ("ADODB.Command")
$rs = CreateObject ("ADODB.RecordSet")
$cn.connectionstring = $CNstring
$cn.Open
$cmd.activeconnection = $cn
QueryLogon($cn, $cmd, $rs, $CNstring, $CMDtxt, $DBfile)
$cn.close
$x=MessageBox("Done!","Data Collection", 4096)
Exit 1
EndFunction
;*** COLLECT WORKSTATION ASSET INFORMATION
;Queries Logon information, updates COMPUTERS table.
Function QueryLogon($cn, $cmd, $rs, $CNstring, $CMDtxt, $DBfile)
Dim $Search1, $Search2, $Branded, $FindString, $Found, $CMDtxt, $CompName, $ModifyDate, $cmd, $rs
? "Querying Logon Information"
$rs.cursortype = 3
$rs.locktype = 3
$rs.activecommand = $cmd
$cmd.commandtext = $CMDtxt $rs.Open ($cmd)
$ComputerName = @WKSTA
$ModifyDate = @DATE
;**** Set Variables ****
$CompName = @WKSTA
$Search1 = "c:\windows\system\oeminfo.ini"
$Search2 = "c:\windows\system32\oeminfo.ini"
$Branded = "blank"
$FindString = "Your text here"
$Found = "blank"
$ModifyDate = @DATE
;Check for file existence and check for the $FindString
$Branded = IIF(ReadProfileString($Search1, "Support Information", "Line3"), "True", "False")
$Branded = IIF(ReadProfileString($Search2, "Support Information", "Line3"), "True", "False")
If $Branded = "True"
$Found = ReadProfileString($Search1, "Support Information", "Line3")
$Found = ReadProfileString($Search2, "Support Information", "Line3")
Else
$Found = "False"
Endif
? $CompName
;? $Search1
;? $Search2
? $Branded
? $FindString
? $Found
? $ModifyDate
Get $x
If $rs.eof = -1
$rs.addnew
EndIf
$rs.fields.item("COMPNAME").value = $CompName
$rs.fields.item("Branded").value = $Branded
$rs.fields.item("MODIFYDATE").value = $ModifyDate
$rs.update
$rs.Close
Exit @error
EndFunction
Anyone see what i can't? Thanks for any assistance (all logic/programming errors are mine, i'm a brute force programmer).
Thanks, Dave
|
|
Top
|
|
|
|
#173071 - 2007-01-23 12:01 AM
Re: Writing to Access file...
[Re: NTDOC]
|
wreded
Fresh Scripter
Registered: 2003-11-06
Posts: 28
Loc: Montana, USA
|
Let's see; KiXtart = 4.5 MS Access = 2003
i don't have rights to create a login script and, though i've mentioned it to our admin, i wanna have it working in test mode (i.e. writing to a database) before i release it to him. Given that i guess it's a remote admin script.
Inventory Script Help by Gn0stik, 12 Dec 05, Post 154171
i've used his code in a real inventory and it worked like a charm. i added a couple of things that are specific to our environment and left out things that we didn't care about, all in all a successful inventory/audit. i wouldn't be so puzzled except that nothing gets written to the file at all though i can see the file is opened.
Thanks! Dave
|
|
Top
|
|
|
|
#173082 - 2007-01-23 04:06 AM
Re: Writing to Access file...
[Re: NTDOC]
|
wreded
Fresh Scripter
Registered: 2003-11-06
Posts: 28
Loc: Montana, USA
|
NTDOC - i'm using Ver 4.50. i know there's a new one available, but 4.50 is available on the entire network.
Shawn - i did the @ERROR thing and got this result:
DAD0
True
2007/01/22
not allowed when the object is closed.) [-2147352567/80020009]et - Operation is
Right now the entire thing, database and all, sits on a thumb drive that i take with me so i don't have to try to remember where i left off (i do some of this at home where i don't get bothered [as much]), so database locking shouldn't be an issue. By the looks of it i haven't properly opened the database for updating. Would that be a proper interpretation of this error message? If so, where and how should i open the database for writing? Thanks, Dave
|
|
Top
|
|
|
|
#173087 - 2007-01-23 05:43 AM
Re: Writing to Access file...
[Re: Les]
|
wreded
Fresh Scripter
Registered: 2003-11-06
Posts: 28
Loc: Montana, USA
|
Ok, it looks like i'm not opening the database like i thought i was. i guess i'll have to "line-by-line" it tomorrow. Man, i hate it when a computer does what i say instead of what i mean!
ERROR -2147352567 - COM exception error "update" (ADODB.Recordset - Operation is
not allowed when the object is closed.) [-2147352567/80020009]
Thanks! Dave
|
|
Top
|
|
|
|
#173118 - 2007-01-23 07:15 PM
Re: Writing to Access file...
[Re: Sealeopard]
|
wreded
Fresh Scripter
Registered: 2003-11-06
Posts: 28
Loc: Montana, USA
|
Ok, thanks for Your help. i went line by line from top to bottom and got it running. Checked it against one computer that meets specs and one that doesn't and got the right responses both times. Yeah Team! i think i wasn't paying close enough attention to the database within the database file and didn't open it properly. i also discovered that i wasn't checking for what i thought i was checking for. Final code (minus comments) is here. Thanks so much for Your help. Dave
$=setoption("wrapateol","on")
Dim $DBpath, $DBFile
;$DBpath = @Scriptdir ;Modify this to represent your database location if using an access database.
$DBPath = "F:\New Folder"
$DBfile = $DBpath + "\computers.mdb"
CLS
At (5, 1) "Checking for Branding..."
Sleep 1
CLS
Break ON CLS
If Exist($DBpath) = 0 ;Abort if no Database exists when using an access database.
? "Branding Database Not Found. Aborting..."
Sleep 1
Exit 1
Else
Main($DBFile)
EndIf
Function Main($DBFile)
Dim $CNstring, $CMDtxt, $cn, $cmd, $rs
;*** CONFIGURE DATABASE CONNECTION STRING ***
$CNstring="Driver={Microsoft Access Driver (*.mdb)}; DBQ=$DBFile" ;Next line for sql server
;$CNstring = "Driver={SQL Server};Server=MyServer;database=MyDatabase;uid=UserName;pwd=password"
$CMDtxt = "select * from Branded where compname = '@WKSTA'"
$cn = CreateObject ("ADODB.Connection")
$cmd = CreateObject ("ADODB.Command")
$rs = CreateObject ("ADODB.RecordSet")
$cn.connectionstring = $CNstring
$cn.Open
$cmd.activeconnection = $cn
QueryBranded($cn, $cmd, $rs, $CNstring, $CMDtxt, $DBfile)
$cn.close
$x=MessageBox("Done!","Branding Collection", 4096)
Exit 1
EndFunction
;*** COLLECT BRANDING INFORMATION
Function QueryBranded($cn, $cmd, $rs, $CNstring, $CMDtxt, $DBfile)
Dim $CMDtxt, $CompName, $ModifyDate, $cmd, $rs
? "Querying Branding Information"
$rs.cursortype = 3
$rs.locktype = 3
$rs.activecommand = $cmd
$cmd.commandtext = $CMDtxt $rs.Open ($cmd)
;**** Set Variables ****
$ModifyDate = @DATE
$CompName = @WKSTA
$Search1 = "c:\windows\system\oeminfo.ini"
$Search2 = "c:\windows\system32\oeminfo.ini"
$Branded = "blank"
$FindString = "Your String Here"
$Found = "blank"
$ModifyDate = @DATE
If $rs.eof = -1
$rs.addnew
EndIf
;Check for file existence and check for the $FindString
$Found = ReadProfileString($Search1, "Support Information", "Line3")
$Found = ReadProfileString($Search2, "Support Information", "Line3")
$Branded = IIF($Found = $Findstring, "True", "False")
$rs.fields.item("COMPNAME").value = $CompName
$rs.fields.item("Branded").value = $Branded
$rs.fields.item("MODIFYDATE").value = $ModifyDate
$rs.update
$rs.Close
EndFunction
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 2375 anonymous users online.
|
|
|