|
Prev: Data type mismatch in criteria expression
Next: Error
From: lee on 12 Jun 2008 22:41 Richard this is a great script and saved my butt last weekend. I need to run it on about 1000 pc's next week and would like to get a text output of the machines as it goes through the list of pc's. (I thought I could add that part myself but haven't been successful.) Any quick and efficient ideas? "Richard Mueller" wrote: > Nice work catching the errors. I put that together too fast. > > Richard > > "Rob" <Rob(a)discussions.microsoft.com> wrote in message > news:3A26881B-09BC-4ED2-AE08-9A3F7CE24C87(a)microsoft.com... > > Richard, > > You are a diamond - that worked a treat. > > The tip of removing the "on error resume next" was great by itself as I > > could see where things were failing- for starters I had the path of the OU > > slightly jumbled... > > Also managed to fix a couple of errors in the script you gave me: > > > > line 52 needed a space: On Error GoTo 0 > > line 60 needed to change objTargetOU to objOU as was giving an error > > saying > > objTargetOU wasn't defined (which of course it wasn't) > > > > Thanks also for the recommendations re books, now just need to find an > > extra > > few hours in the day! > > Cheers > > Rob > > >
From: ThatsIT.net.au on 13 Jun 2008 11:41 this will loop though all the computers in active directory Const ADS_SCOPE_SUBTREE = 2 dim fso:Set fso = CreateObject("Scripting.FileSystemObject") Set results = fso.CreateTextFile("computers.txt", True) dim objRootDSE:Set objRootDSE = GetObject("LDAP://rootDSE") dim strDomain:strDomain = "LDAP://" & objRootDSE.Get("defaultNamingContext") Set oConn = CreateObject("ADODB.Connection") Set oCommand = CreateObject("ADODB.Command") oConn.Provider = "ADsDSOObject" oConn.Open "Active Directory Provider" Set oCommand.ActiveConnection = oConn oCommand.CommandText = _ "Select Name, Location from '"& strDomain &"' " _ & "Where objectClass='computer'" oCommand.Properties("Page Size") = 1000 oCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE Set oRS = oCommand.Execute oRS.MoveFirst Do Until oRS.EOF results.WriteLine oRS.Fields("Name").Value oRS.MoveNext Loop "lee" <lee(a)discussions.microsoft.com> wrote in message news:096052A1-4AF9-4696-8B62-AF4F44DF671C(a)microsoft.com... > Richard this is a great script and saved my butt last weekend. > > I need to run it on about 1000 pc's next week and would like to get a > text output of the machines as it goes through the list of pc's. (I > thought I > could add that part myself but haven't been successful.) > Any quick and efficient ideas? > > "Richard Mueller" wrote: > >> Nice work catching the errors. I put that together too fast. >> >> Richard >> >> "Rob" <Rob(a)discussions.microsoft.com> wrote in message >> news:3A26881B-09BC-4ED2-AE08-9A3F7CE24C87(a)microsoft.com... >> > Richard, >> > You are a diamond - that worked a treat. >> > The tip of removing the "on error resume next" was great by itself as >> > I >> > could see where things were failing- for starters I had the path of the >> > OU >> > slightly jumbled... >> > Also managed to fix a couple of errors in the script you gave me: >> > >> > line 52 needed a space: On Error GoTo 0 >> > line 60 needed to change objTargetOU to objOU as was giving an error >> > saying >> > objTargetOU wasn't defined (which of course it wasn't) >> > >> > Thanks also for the recommendations re books, now just need to find an >> > extra >> > few hours in the day! >> > Cheers >> > Rob >> >> >>
From: Richard Mueller [MVP] on 13 Jun 2008 11:42 The easiest way to have the script document what happens (to a text file) is to use Wscript.Echo statements, run the script at a command prompt using cscript, and redirect the output to a text file. The script already echos when a computer is not found, so you can add a Wscript.Echo statement where the script moves computers. If the script is saved in the file MoveComputers.vbs you can use a command similar to below at a command prompt: cscript //nologo MoveComputers.vbs > report.txt The "//nologo" parameter suppresses logo information. It takes more work to use the FileSystemObject to create and write to a log file, but this offers more flexibility. You can also both echo to the console and write to the file, so you can monitor progress as the script runs (and not worry if it is hung up), while still having a log for later. The version below (corrected for the mistakes found earlier), both echos to the console and writes to a file. No need to redirect the output. The name and path of the files are hard coded: =========== Option Explicit Dim strFile, strTargetOU, objFSO, objFile, objOU Dim objRootDSE, strDNSDomain, objTrans, strNetBIOSDomain Dim strComputer, strComputerDN, objComputer Dim strLog, objLog Const ADS_NAME_INITTYPE_DOMAIN = 1 Const ADS_NAME_TYPE_NT4 = 3 Const ADS_NAME_TYPE_1179 = 1 Const ForAppending = 8 Const ForReading = 1 Const OpenAsASCII = 0 Const CreateIfNotExist = True ' Specify text file of computer NetBIOS names. strFile = "c:\scripts\Computers.txt" ' Specify log file. strLog = "c:\scripts\MoveComputers.log" ' Specify target OU to move computer objects into. strTargetOU = "ou=Detention,ou=West,dc=MyDomain,dc=com" ' Open the file for read access. Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFile, ForReading) ' Open the log file for appending. Set objLog = objFSO.OpentTextFile(strlog, _ ForAppending, CreateIfNotExist, OpenAsASCII) ' Write to the log file. objLog.WriteLine "MoveComputers.vbs log file" objLog.WriteLine "Started: " & CStr(Now()) ' Bind to the target OU. Set objOU = GetObject("LDAP://" & strTargetOU) ' Determine DNS domain name from RootDSE object. Set objRootDSE = GetObject("LDAP://RootDSE") strDNSDomain = objRootDSE.Get("DefaultNamingContext") ' Use the NameTranslate object to find the NetBIOS domain name from ' the DNS domain name. Set objTrans = CreateObject("NameTranslate") objTrans.Init ADS_NAME_TYPE_NT4, strDNSDomain objTrans.Set ADS_NAME_TYPE_1179, strDNSDomain strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4) ' Remove trailing backslash. strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1) ' Use the NameTranslate object to convert computer NetBIOS ' names to Distinguished Names. objTrans.Init ADS_NAME_INITTYPE_DOMAIN, strNetBIOSDomain ' Read lines from the file. Do Until objFile.AtEndOfStream strComputer = Trim(objFile.ReadLine) If (strComputer <> "") Then ' Convert NetBIOS name to DN. ' NetBIOS name must have "$" appended to end. ' Trap error if computer not found. On Error Resume Next objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain _ & "\" & strComputer & "$" If (Err.Number <> 0) Then On Error GoTo 0 Wscript.Echo "Computer not found: " & strComputer objLog.WriteLine strComputer & " not found" Else On Error GoTo 0 strComputerDN = objTrans.Get(ADS_NAME_TYPE_1179) ' Bind to the computer object. Set objComputer = GetObject("LDAP://" & strComputerDN) ' Move the computer object to the target OU. objOU.MoveHere objComputer.AdsPath, vbNullString Wscript.Echo strComputer & " moved" objLog.WriteLine strComputer & " moved" End If End If Loop objLog.WriteLine "Finished: " & CStr(Now()) objFile.Close objLog.Close Wscript.Echo "Done" -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net -- "lee" <lee(a)discussions.microsoft.com> wrote in message news:096052A1-4AF9-4696-8B62-AF4F44DF671C(a)microsoft.com... > Richard this is a great script and saved my butt last weekend. > > I need to run it on about 1000 pc's next week and would like to get a > text output of the machines as it goes through the list of pc's. (I > thought I > could add that part myself but haven't been successful.) > Any quick and efficient ideas? > > "Richard Mueller" wrote: > >> Nice work catching the errors. I put that together too fast. >> >> Richard >> >> "Rob" <Rob(a)discussions.microsoft.com> wrote in message >> news:3A26881B-09BC-4ED2-AE08-9A3F7CE24C87(a)microsoft.com... >> > Richard, >> > You are a diamond - that worked a treat. >> > The tip of removing the "on error resume next" was great by itself as >> > I >> > could see where things were failing- for starters I had the path of the >> > OU >> > slightly jumbled... >> > Also managed to fix a couple of errors in the script you gave me: >> > >> > line 52 needed a space: On Error GoTo 0 >> > line 60 needed to change objTargetOU to objOU as was giving an error >> > saying >> > objTargetOU wasn't defined (which of course it wasn't) >> > >> > Thanks also for the recommendations re books, now just need to find an >> > extra >> > few hours in the day! >> > Cheers >> > Rob >> >> >>
From: ThatsIT.net.au on 13 Jun 2008 12:05 Ok looks like I misread you post, "ThatsIT.net.au" <me(a)work> wrote in message news:FFE8237C-40EA-467A-AF0E-F41C29BF86C0(a)microsoft.com... > this will loop though all the computers in active directory > > Const ADS_SCOPE_SUBTREE = 2 > dim fso:Set fso = CreateObject("Scripting.FileSystemObject") > Set results = fso.CreateTextFile("computers.txt", True) > dim objRootDSE:Set objRootDSE = GetObject("LDAP://rootDSE") > dim strDomain:strDomain = "LDAP://" & > objRootDSE.Get("defaultNamingContext") > Set oConn = CreateObject("ADODB.Connection") > Set oCommand = CreateObject("ADODB.Command") > oConn.Provider = "ADsDSOObject" > oConn.Open "Active Directory Provider" > > Set oCommand.ActiveConnection = oConn > oCommand.CommandText = _ > "Select Name, Location from '"& strDomain &"' " _ > & "Where objectClass='computer'" > > oCommand.Properties("Page Size") = 1000 > oCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE > Set oRS = oCommand.Execute > oRS.MoveFirst > > Do Until oRS.EOF > results.WriteLine oRS.Fields("Name").Value > oRS.MoveNext > Loop > > > > "lee" <lee(a)discussions.microsoft.com> wrote in message > news:096052A1-4AF9-4696-8B62-AF4F44DF671C(a)microsoft.com... >> Richard this is a great script and saved my butt last weekend. >> >> I need to run it on about 1000 pc's next week and would like to get >> a >> text output of the machines as it goes through the list of pc's. (I >> thought I >> could add that part myself but haven't been successful.) >> Any quick and efficient ideas? >> >> "Richard Mueller" wrote: >> >>> Nice work catching the errors. I put that together too fast. >>> >>> Richard >>> >>> "Rob" <Rob(a)discussions.microsoft.com> wrote in message >>> news:3A26881B-09BC-4ED2-AE08-9A3F7CE24C87(a)microsoft.com... >>> > Richard, >>> > You are a diamond - that worked a treat. >>> > The tip of removing the "on error resume next" was great by itself as >>> > I >>> > could see where things were failing- for starters I had the path of >>> > the OU >>> > slightly jumbled... >>> > Also managed to fix a couple of errors in the script you gave me: >>> > >>> > line 52 needed a space: On Error GoTo 0 >>> > line 60 needed to change objTargetOU to objOU as was giving an error >>> > saying >>> > objTargetOU wasn't defined (which of course it wasn't) >>> > >>> > Thanks also for the recommendations re books, now just need to find an >>> > extra >>> > few hours in the day! >>> > Cheers >>> > Rob >>> >>> >>> >
From: lee on 16 Jun 2008 14:46
Richard, I tried running the script usicng Cscript and got the following error: C:\Lee\moveou.vbs(30, 1) Microsoft VBScript runtime error: Object doesn't support this property or method: 'OpentTextFile' I tried creating the Movedcomputer.log file but got the same error. I have only changed the file locations and the OU path to suit our environment. "Richard Mueller [MVP]" wrote: > The easiest way to have the script document what happens (to a text file) is > to use Wscript.Echo statements, run the script at a command prompt using > cscript, and redirect the output to a text file. The script already echos > when a computer is not found, so you can add a Wscript.Echo statement where > the script moves computers. If the script is saved in the file > MoveComputers.vbs you can use a command similar to below at a command > prompt: > > cscript //nologo MoveComputers.vbs > report.txt > > The "//nologo" parameter suppresses logo information. It takes more work to > use the FileSystemObject to create and write to a log file, but this offers > more flexibility. You can also both echo to the console and write to the > file, so you can monitor progress as the script runs (and not worry if it is > hung up), while still having a log for later. The version below (corrected > for the mistakes found earlier), both echos to the console and writes to a > file. No need to redirect the output. The name and path of the files are > hard coded: > =========== > Option Explicit > > Dim strFile, strTargetOU, objFSO, objFile, objOU > Dim objRootDSE, strDNSDomain, objTrans, strNetBIOSDomain > Dim strComputer, strComputerDN, objComputer > Dim strLog, objLog > > Const ADS_NAME_INITTYPE_DOMAIN = 1 > Const ADS_NAME_TYPE_NT4 = 3 > Const ADS_NAME_TYPE_1179 = 1 > Const ForAppending = 8 > Const ForReading = 1 > Const OpenAsASCII = 0 > Const CreateIfNotExist = True > > ' Specify text file of computer NetBIOS names. > strFile = "c:\scripts\Computers.txt" > > ' Specify log file. > strLog = "c:\scripts\MoveComputers.log" > > ' Specify target OU to move computer objects into. > strTargetOU = "ou=Detention,ou=West,dc=MyDomain,dc=com" > > ' Open the file for read access. > Set objFSO = CreateObject("Scripting.FileSystemObject") > Set objFile = objFSO.OpenTextFile(strFile, ForReading) > > ' Open the log file for appending. > Set objLog = objFSO.OpentTextFile(strlog, _ > ForAppending, CreateIfNotExist, OpenAsASCII) > ' Write to the log file. > objLog.WriteLine "MoveComputers.vbs log file" > objLog.WriteLine "Started: " & CStr(Now()) > > ' Bind to the target OU. > Set objOU = GetObject("LDAP://" & strTargetOU) > > ' Determine DNS domain name from RootDSE object. > Set objRootDSE = GetObject("LDAP://RootDSE") > strDNSDomain = objRootDSE.Get("DefaultNamingContext") > > ' Use the NameTranslate object to find the NetBIOS domain name from > ' the DNS domain name. > Set objTrans = CreateObject("NameTranslate") > objTrans.Init ADS_NAME_TYPE_NT4, strDNSDomain > objTrans.Set ADS_NAME_TYPE_1179, strDNSDomain > strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4) > ' Remove trailing backslash. > strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1) > > ' Use the NameTranslate object to convert computer NetBIOS > ' names to Distinguished Names. > objTrans.Init ADS_NAME_INITTYPE_DOMAIN, strNetBIOSDomain > > ' Read lines from the file. > Do Until objFile.AtEndOfStream > strComputer = Trim(objFile.ReadLine) > If (strComputer <> "") Then > ' Convert NetBIOS name to DN. > ' NetBIOS name must have "$" appended to end. > ' Trap error if computer not found. > On Error Resume Next > objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain _ > & "\" & strComputer & "$" > If (Err.Number <> 0) Then > On Error GoTo 0 > Wscript.Echo "Computer not found: " & strComputer > objLog.WriteLine strComputer & " not found" > Else > On Error GoTo 0 > strComputerDN = objTrans.Get(ADS_NAME_TYPE_1179) > ' Bind to the computer object. > Set objComputer = GetObject("LDAP://" & strComputerDN) > ' Move the computer object to the target OU. > objOU.MoveHere objComputer.AdsPath, vbNullString > Wscript.Echo strComputer & " moved" > objLog.WriteLine strComputer & " moved" > End If > End If > Loop > > objLog.WriteLine "Finished: " & CStr(Now()) > objFile.Close > objLog.Close > Wscript.Echo "Done" > > -- > Richard Mueller > MVP Directory Services > Hilltop Lab - http://www.rlmueller.net > -- > "lee" <lee(a)discussions.microsoft.com> wrote in message > news:096052A1-4AF9-4696-8B62-AF4F44DF671C(a)microsoft.com... > > Richard this is a great script and saved my butt last weekend. > > > > I need to run it on about 1000 pc's next week and would like to get a > > text output of the machines as it goes through the list of pc's. (I > > thought I > > could add that part myself but haven't been successful.) > > Any quick and efficient ideas? > > > > "Richard Mueller" wrote: > > > >> Nice work catching the errors. I put that together too fast. > >> > >> Richard > >> > >> "Rob" <Rob(a)discussions.microsoft.com> wrote in message > >> news:3A26881B-09BC-4ED2-AE08-9A3F7CE24C87(a)microsoft.com... > >> > Richard, > >> > You are a diamond - that worked a treat. > >> > The tip of removing the "on error resume next" was great by itself as > >> > I > >> > could see where things were failing- for starters I had the path of the > >> > OU > >> > slightly jumbled... > >> > Also managed to fix a couple of errors in the script you gave me: > >> > > >> > line 52 needed a space: On Error GoTo 0 > >> > line 60 needed to change objTargetOU to objOU as was giving an error > >> > saying > >> > objTargetOU wasn't defined (which of course it wasn't) > >> > > >> > Thanks also for the recommendations re books, now just need to find an > >> > extra > >> > few hours in the day! > >> > Cheers > >> > Rob > >> > >> > >> > > > |