Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


I am Stuck!

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,43680
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> I am Stuck!
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 I am Stuck! - 2/23/2007 10:41:35 AM   
  twilliamsen

 

Posts: 195
Score: 0
Joined: 1/18/2007
Status: offline
I am trying to write a script that does the following...

Take all the PCs in the active directory and add the next number to the name based on the location of the PC. 

I.e.  the last PC is MRSOH1001 and want the new pc to be named MRSOH1002

Then join the newly named PC to the domain.

Here is what I have so far



' **********************************************************************************
' This script will Disable the Windows Firewall *
' Determine what location the new PC will be located based off of the IP address *
' Queries the Active Directory to find out which name to name the computer. *
' Renames the PC *
' Joins the Computer to the domain. *
' Removes unnecessary Windows Components and software *
' Once completed, it executes another batch file to execute installs of software *
' Written by Todd Williamsen *
' Date: February 23, 2007 *
' **********************************************************************************
' Disable the firewall

Dim
strLocation, strIPAddress, arrIP

Set
objShell = CreateObject("Wscript.Shell")
objShell.Run("\\192.168.5.8\support\tools\scripts\disableFW.bat")

' get IP address of the machine

strComputer = "."

Set
objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set
IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")


For
Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
strIPAddress= IPConfig.IPAddress(i)



' Determine which site the computer is at

arrIP = Split(strIPAddress, ".")

Select
Case arrIP(0)

Case
"10"
strLocation = "MRSSJ"

Case
"172"
strLocation = "MRSAZ"

Case
"192"

If
arrIp(2) = "1" then

strLocation = "MRSSJ"

Else

strLocation = "MRSOH"

End
If
Case
Else

strLocation = "UNKNOWN"

'Error handling here

End
Select

Next

End If
Next

WScript.Echo strlocation
' just verifies the site name
'connection to Active Directory and Enumerates the computers Container

Const
ADS_SCOPE_SUBTREE = 2

Set
objConnection = CreateObject("ADODB.Connection")

Set
objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set
objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
"SELECT Name, Location FROM 'LDAP://DC=fabrikam,DC=com' " _
& "WHERE objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") =
False
Set
objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do
Until objRecordSet.EOF
Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value

objRecordSet.MoveNext

Loop

'change the computer name
Set WshNetwork = WScript.CreateObject("WScript.Network")

        WScript.Echo "Computer Name = " & WshNetwork.ComputerName
???? lost..  only thing I found was for a computer name already on the domain...

' join computer to domain

'join computer to domain
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const ACCT_DELETE = 4
Const WIN9X_UPGRADE = 16
Const DOMAIN_JOIN_IF_JOINED = 32
Const JOIN_UNSECURE = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET = 256
Const INSTALL_INVOCATION = 262144

strDomain = "FABRIKAM"
strPassword = "ls4k5ywA"
strUser = "shenalan"

Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName

Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
   strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
       strComputer & "'")

ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
   strPassword, strDomain & "\" & strUser, NULL, _
       JOIN_DOMAIN + ACCT_CREATE)



 
 
Post #: 1
 
 RE: I am Stuck! - 2/23/2007 11:38:06 AM   
  Parabellum


Posts: 222
Score: 0
Joined: 11/12/2006
From: UK
Status: offline
well firstly... as per ebgreens sugestiobns... in the previos post... i would seperate the code into functions or sub routines...

eg..

disableFirewall()
getIPAddress()
getSite()
changeName()
joinDomain()


is there something particular your stuck with??

(in reply to twilliamsen)
 
 
Post #: 2
 
 RE: I am Stuck! - 2/23/2007 10:32:56 PM   
  twilliamsen

 

Posts: 195
Score: 0
Joined: 1/18/2007
Status: offline
I got the site based on the IP, that works...  But I need to take that information and figure out which computers begin with the site based on IP and find the last one in Active Directory

(in reply to Parabellum)
 
 
Post #: 3
 
 RE: I am Stuck! - 2/24/2007 5:45:38 AM   
  dm_4ever


Posts: 2359
Score: 36
Joined: 6/29/2006
From: Orange County, California
Status: offline
Well now that you have the site name and know what the computer names start with. i.e.  MRSSJ, MRSAZ, etc. you should modify your AD query to search for computer names that start with that. 

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to twilliamsen)
 
 
Post #: 4
 
 RE: I am Stuck! - 2/26/2007 2:24:59 AM   
  twilliamsen

 

Posts: 195
Score: 0
Joined: 1/18/2007
Status: offline
Ok..I cannot get it to find the last computer name in the search...  Find the last one in the site name...  hmm

This is what I have to find it...


Set
objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set
objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = "<LDAP://dc=corp-nj, dc=mrsassociates, dc=com>;(&(objectCategory=Computer)(objectClass=Computer));ADsPath;subtree"

Set
objRecordSet = objCommand.Execute
f = 100

for
r = 0 to objRecordset.recordcount - 1
strADsPath = objRecordset.Fields("ADsPath")
Set objComputer = GetObject(strADsPath)
Set objShell = CreateObject("WScript.Shell")
strTotalLength=
len(objComputer.name)
strLength=strTotalLength - 3
strComputername=Mid(objComputer.name,4,strLength)
If instr(strADsPath, strLocation) > 0 Then
 


End If



(in reply to dm_4ever)
 
 
Post #: 5
 
 RE: I am Stuck! - 2/26/2007 3:31:33 AM   
  dm_4ever


Posts: 2359
Score: 36
Joined: 6/29/2006
From: Orange County, California
Status: offline
I was thinking of a search more like this.
"<LDAP://dc=corp-nj, dc=mrsassociates, dc=com>;(&(cn=MRSSJ*)(objectClass=Computer));cn;subtree" 
It should return computer names starting with MRSSJ and then it is up to you to get the number values and find out the next one. 

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to twilliamsen)
 
 
Post #: 6
 
 RE: I am Stuck! - 2/26/2007 4:40:40 AM   
  twilliamsen

 

Posts: 195
Score: 0
Joined: 1/18/2007
Status: offline
What about this?  Since the site name is dynamic and the script should run at each site.

"<LDAP://dc=corp-nj, dc=mrsassociates, dc=com>;(&(cn=strLocation*)(objectClass=Computer));cn;subtree

(in reply to dm_4ever)
 
 
Post #: 7
 
 RE: I am Stuck! - 2/26/2007 4:42:50 AM   
  dm_4ever


Posts: 2359
Score: 36
Joined: 6/29/2006
From: Orange County, California
Status: offline
For dynamic it should look more like this:
"<LDAP://dc=corp-nj, dc=mrsassociates, dc=com>;(&(cn=" & strLocation & "*)(objectClass=Computer));cn;subtree"

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to twilliamsen)
 
 
Post #: 8
 
 RE: I am Stuck! - 2/26/2007 4:52:20 AM   
  twilliamsen

 

Posts: 195
Score: 0
Joined: 1/18/2007
Status: offline
Ok..

This is what it looks like so far, is it doing what it is suppose to?


' Disable the firewall

Dim
strLocation, strIPAddress, arrIP

Set
objShell = CreateObject("Wscript.Shell")
objShell.Run("\\192.168.5.8\support\tools\scripts\disableFW.bat")

' get IP address of the machine

strComputer = "."

Set
objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set
IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")


For
Each IPConfig in IPConfigSet

If
Not IsNull(IPConfig.IPAddress) Then

For
i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
strIPAddress= IPConfig.IPAddress(i)



' Determine which site the computer is at

arrIP = Split(strIPAddress, ".")

Select
Case arrIP(0)

Case
"10"
strLocation = "MRSSJ"

Case
"172"
strLocation = "MRSAZ"

Case
"192"

If
arrIp(2) = "1" then

strLocation = "MRSSJ"

Else

strLocation = "MRSOH"

End
If
Case
Else

strLocation = "UNKNOWN"

'Error handling here

End
Select
Next
End
If
Next

WScript.Echo strlocation
' just verifies the site name
'connection to Active Directory and Enumerates the computers Container

Set
objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set
objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = "&lt;LDAP://dc=corp-nj, dc=mrsassociates, dc=com&gt;;(&(cn=" & strLocation & "*)(objectClass=Computer));cn;subtree"

Set
objRecordSet = objCommand.Execute
f = 100

for
r = 0 to objRecordset.recordcount - 1
strADsPath = objRecordset.Fields("ADsPath")
Set objComputer = GetObject(strADsPath)
Set objShell = CreateObject("WScript.Shell")
strTotalLength=
len(objComputer.name)
strLength=strTotalLength - 3
strComputername=Mid(objComputer.name,4,strLength)
If instr(strADsPath,strLocation) > 0 Then


End If

objRecordset.MoveNext

' The following loop pauses the script for 1 minute to allow
' the first 100 processes to terminate,

end if
Next
Loop

'join computer to domain

Const
JOIN_DOMAIN = 1

Const
ACCT_CREATE = 2

Const
ACCT_DELETE = 4

Const
WIN9X_UPGRADE = 16

Const
DOMAIN_JOIN_IF_JOINED = 32

Const
JOIN_UNSECURE = 64

Const
MACHINE_PASSWORD_PASSED = 128

Const
DEFERRED_SPN_SET = 256

Const
INSTALL_INVOCATION = 262144

strDomain = "DOMAIN-NAME"
strPassword = "ls4k5ywA"
' dummy data

strUser = "shenalan"
' dummy data



Set
objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName


Set
objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
strComputer & "'")

ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
strPassword, strDomain & "\" & strUser,
NULL, _
JOIN_DOMAIN + ACCT_CREATE)

(in reply to dm_4ever)
 
 
Post #: 9
 
 RE: I am Stuck! - 2/27/2007 8:51:11 AM   
  twilliamsen

 

Posts: 195
Score: 0
Joined: 1/18/2007
Status: offline
anyone?

(in reply to twilliamsen)
 
 
Post #: 10
 
 RE: I am Stuck! - 2/27/2007 8:58:09 AM   
  dm_4ever


Posts: 2359
Score: 36
Joined: 6/29/2006
From: Orange County, California
Status: offline
I have a few things to take care of and if no one has posted by then, I'll take a look. 

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to twilliamsen)
 
 
Post #: 11
 
 RE: I am Stuck! - 2/27/2007 10:46:20 AM   
  twilliamsen

 

Posts: 195
Score: 0
Joined: 1/18/2007
Status: offline
No problem...

I got thrown into this task and I am going to need it soon as I have 200 machines to roll out :)

(in reply to dm_4ever)
 
 
Post #: 12
 
 RE: I am Stuck! - 2/27/2007 12:28:48 PM   
  dm_4ever


Posts: 2359
Score: 36
Joined: 6/29/2006
From: Orange County, California
Status: offline
You should consider putting specific tasks into their own functions as it has been suggested by others already.  The following may or may not work...can't really test it. 


      

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to twilliamsen)
 
 
Post #: 13
 
 RE: I am Stuck! - 3/2/2007 6:32:16 AM   
  twilliamsen

 

Posts: 195
Score: 0
Joined: 1/18/2007
Status: offline
I actually got a chance finally to test this..  Well, it errors out at line 51, "no such table" 

I assume the AD query is looking for a cn of strComputer correct and of course that does not exist.

First off it needs the query fixed,  if you can help me figure that out, then I think we should be good! 

We are definately on the right path!

(in reply to dm_4ever)
 
 
Post #: 14
 
 RE: I am Stuck! - 3/2/2007 7:25:50 AM   
  dm_4ever


Posts: 2359
Score: 36
Joined: 6/29/2006
From: Orange County, California
Status: offline
You should defininitely be able to get the CN value as long as this is correct:  LDAP://dc=corp-nj, dc=mrsassociates, dc=com and the strLocation being passed is correct.
Try something simple in a seperate .vbs like this:


      

If this works, then just verify your script is passing a correct strLocation value.  The example above is slightly different from the previous one in that we are trying to get this information "dc=corp-nj, dc=mrsassociates, dc=com" dynamically.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to twilliamsen)
 
 
Post #: 15
 
 RE: I am Stuck! - 3/2/2007 10:07:13 AM   
  twilliamsen

 

Posts: 195
Score: 0
Joined: 1/18/2007
Status: offline
thanks dm.. I have been slammed.  I will try it on monday.. MILLER TIME!

(in reply to dm_4ever)
 
 
Post #: 16
 
 RE: I am Stuck! - 3/6/2007 6:28:41 AM   
  twilliamsen

 

Posts: 195
Score: 0
Joined: 1/18/2007
Status: offline
Ok, your test echos back every computer in that location...  That is cool.  I am going to play with later today and see how it pans out.  First I need to set up some virtual T-1s and connect them to our PBX. 

(in reply to twilliamsen)
 
 
Post #: 17
 
 RE: I am Stuck! - 3/6/2007 6:40:36 AM   
  twilliamsen

 

Posts: 195
Score: 0
Joined: 1/18/2007
Status: offline
Ok, After staring at it abit...

It does rename the computer, it renamed mine, but I am looking for a naming convention of

MRSOH0140

This script will do this

MRSOH141

So How do I add a zero?

What if it is MRSOH1000

I don't want it to add another zero




(in reply to twilliamsen)
 
 
Post #: 18
 
 RE: I am Stuck! - 3/6/2007 7:05:25 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
So the numeric portion of the name should always be 4 digits padded with 0 on the left?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to twilliamsen)
 
 
Post #: 19
 
 RE: I am Stuck! - 3/6/2007 8:03:43 AM   
  twilliamsen

 

Posts: 195
Score: 0
Joined: 1/18/2007
Status: offline
Yes.

(in reply to ebgreen)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> I am Stuck! Page: [1] 2   next >   >>
Jump to: