Login | |
|
 |
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)
|
|
| |
|
|
|
 |
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??
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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 = "<LDAP://dc=corp-nj, dc=mrsassociates, dc=com>;(&(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)
|
|
| |
|
|
|
|
|