Lansweeper logo
Home Download Features Demo Buy now Help Support forum
 
    Most requested support articles:
  Lansweeper troubleshooting guide.
  The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
  WMI Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
  How to configure the windows firewall using group policies.
  Support:  
 support@lansweeper.com  
Skype: Lansweeper  
  Mo-Fri 9h-17h CET  
Welcome Guest Search | Active Topics | Log In | Register

2 Pages12>
Untag as favorite
Cleaning COMPUTERS not in Active Directory
robinb
#1 Posted : Thursday, January 29, 2009 8:28:27 PM

Rank: Premium user

Groups: Member, Premium Users
Posts: 4
Location: Canada
Here's a little VBSCRIPT that verify each Computer in LanSweeper DB against its presence in Active Directory. If the computer is not in AD, it is remove from Lansweeper DB.
This Script must be run under an account with the correct rights on the DB. There is no error trapping nor validation in the script.
Here's the code:

Code:

Dim objCNSQL
Dim strSQLDELETE
Dim strSQLQUERY
Dim objRSSQL
Dim SQLName
Const ADS_SCOPE_SUBTREE = 2
strSQLQUERY="select * from tblcomputers"

' **************** AD Connection ****************************
'
Set objCNAD = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objCNAD.Provider = "ADsDSOObject"
objCNAD.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objCNAD
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

' **************** LAnsweeper connection ****************************
' * Replace SERVERNAME by your SQL Server Instance
'
Set objCNSQL=CreateObject("ADODB.Connection")
objCNSQL.Open "Provider=SQLOLEDB.1;Persist Security Info=False;Integrated Security=SSPI;Server=SERVERNAME\sqlexpress;Database=Lansweeper32"


' **************** REading Lansweeper ****************************
'
set objRSSQL=objCNSQL.execute(strSQLQUERY)


' ******************************************************************
' *     For each record in tblcomputers
' *       If computername exist in AD, do noting
' *       Else, delete the record in the DB
' *
' *  REplace COMPANY, COM by your DOMAIN
'
objRSSQL.MoveFirst
Do Until objRSSQL.EOF
SQLName=objRSSQL("Computername")
   
    objCommand.CommandText = "SELECT Name FROM 'LDAP://dc=COMPANY,dc=COM' WHERE objectCategory='computer' AND Name='" & SQLName & "'"
    Set objRSAD = objCommand.Execute
    On Error Resume Next
     objRSAD.MoveFirst
    if(err.number<>0) then
      Wscript.Echo "DELETING: " & SQLName
      strSQLDELETE="Delete from tblcomputers where ComputerName='" & SQLName & "'"
      objCNSQL.execute(strSQLDELETE)
    else
      Wscript.Echo "Keep: " & SQLName
    end if

    objRSSQL.MoveNext
Loop

objCNSQL.Close
File Attachment(s):
CleanLansweeper.vbs.txt (2kb) downloaded 130 time(s).
Cobra7
#2 Posted : Thursday, January 29, 2009 10:48:42 PM

Rank: Premium user

Groups: Member, Premium Users
Posts: 147
Location: Omaha
Thanks a ton, this helped a lot.

Only 1 request: I had to hit OK about 1800 times, any chance we could get an option at the start for an auto confirm and then just get a count at the end with how many were deleted? If you don't have the time I understand :)

This is exactly what I was looking for, so thanks.
Lansweeper
#3 Posted : Friday, January 30, 2009 9:15:19 AM

Rank: Administration

Groups: Administration, Premium Users
Posts: 10,378
I've made this thread a sticky
taeratrin
#4 Posted : Friday, January 30, 2009 2:45:48 PM

Rank: Premium user

Groups: Premium Users
Posts: 214
If you don't mind, I could make this an executable and provide the error-checking. I could also probably reoslve Cobra7's problem in the process. Let me know if you want me to do this.
robinb
#5 Posted : Friday, January 30, 2009 6:44:14 PM

Rank: Premium user

Groups: Member, Premium Users
Posts: 4
Location: Canada
Call the script using CSCRIPT.EXE in a command line to avoid popup messages. You can aslo pipe the result in a text file using ">" to keep a trace.
Cobra7
#6 Posted : Tuesday, February 03, 2009 10:08:41 PM

Rank: Premium user

Groups: Member, Premium Users
Posts: 147
Location: Omaha
I have very little experence with VBS programming myself, I'm just someone who understands scripting and can understand and modify most code I come across.

So what do you mean by "call the script"? As in just rename it?
robinb
#7 Posted : Tuesday, February 10, 2009 5:39:54 PM

Rank: Premium user

Groups: Member, Premium Users
Posts: 4
Location: Canada
I mean Execute the script from a command line like this one:

CSCRIPT cleanlansweeper.vbs
lhawk
#8 Posted : Tuesday, February 10, 2009 8:18:01 PM


Rank: Premium user

Groups: Member, Premium Users
Posts: 31
Location: WI
Thanks for the script. We have our LS database on a seperate server. Should I run this script from the application server or from the DB server? Or does it even matter?

Thanks again.
robinb
#9 Posted : Monday, February 16, 2009 8:57:40 PM

Rank: Premium user

Groups: Member, Premium Users
Posts: 4
Location: Canada
No matter, you must just use an account with R/W access to the DB
pdumbleton
#10 Posted : Wednesday, February 18, 2009 3:26:00 PM

Rank: Premium user

Groups: Member, Premium Users
Posts: 19
Location: Zeeland, MI
Nice script thank you. But will this not leave orphaned entries in all the other tables.
DGoodwin
#11 Posted : Thursday, February 19, 2009 2:37:04 PM

Rank: Premium user

Groups: Premium Users, Member
Posts: 49
Location: NC, USA
I just looked in a couple of my tables where Computername = what we removed via the tool, and I do not see any reference to the removed in the following tables (tbl_software, tbl_OperatingSystem, tbl_ComputerSystem, tbl_Users). It look as if it does remove all data. Maybe LS can clarify if removing a machine from tbl_computers will strip out that machine from the rest of the tables?
Lansweeper
#12 Posted : Thursday, February 19, 2009 7:14:31 PM

Rank: Administration

Groups: Administration, Premium Users
Posts: 10,378
DGoodwin wrote:
I just looked in a couple of my tables where Computername = what we removed via the tool, and I do not see any reference to the removed in the following tables (tbl_software, tbl_OperatingSystem, tbl_ComputerSystem, tbl_Users). It look as if it does remove all data. Maybe LS can clarify if removing a machine from tbl_computers will strip out that machine from the rest of the tables?

Yes, when you delete a computer from tblcomputers it is cascaded to the other tables.
Geno
#13 Posted : Wednesday, March 11, 2009 6:52:59 PM
Rank: Freeware Member

Groups: Member
Posts: 21
Hi,

I tested this out, and indeed it works.
But only on the domain where you are running it.
It deletes all the computers that are on a different domain.
I'm sure a line could be changed to make sure it doesn't delete the other computers, but I have yet had the time to look at it.
If/when I find it, I will post it here. Unless someone changes it before me.

Regards,
BullGates
#14 Posted : Friday, March 13, 2009 2:23:12 PM

Rank: Premium user

Groups: Member, Premium Users
Posts: 205
Location: PT
I'm getting an error while trying to download the script, can someone repost it?

"Error: Resource has been moved or is unavailable. Please contact the forum admin."
rmorgan
#15 Posted : Saturday, June 27, 2009 12:25:39 AM

Rank: Premium user

Groups: Member, Premium Users
Posts: 12
Location: Derver, Colorado USA
Nope, I can't seam to get this to work.

I am getting the following Script Error:

Quote:
Script: C:\LSShare\CleanLansweeper.vbs
Line: 43
Char: 5
Error: Table does not exist
Code: 80040E37
Source: Provider


I think my problems are the following:

Quote:
' ******************************************************************
' * For each record in tblcomputers
' * If computername exist in AD, do noting
' * Else, delete the record in the DB
' *
' * REplace COMPANY, COM by your DOMAIN
'
objRSSQL.MoveFirst
Do Until objRSSQL.EOF
SQLName=objRSSQL("Computername")

objCommand.CommandText = "SELECT Name FROM 'LDAP://dc=COMPANY,dc=COM' WHERE


I am not sure what is ment by "Replace COMPANY, COM by your DOMAIN"

Would it be:
"SELECT Name FROM 'LDAP://dc=MYCompanyName,dc=MYDomainName' WHERE

Help!!!Brick wall
Users browsing this topic
Guest
2 Pages12>
Untag as favorite
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Contact: E-mail Lansweeper - Skype : Lansweeper
Copyright 2004 - 2011 © Hemoco bvba