Charlie Kaiser
2004-02-17 05:51:19 UTC
I'm relatively new to scripting, but I've been hitting the books pretty
hard. :-)
I'm working on an enterprise script, and have a problem that I can't seem to
find a solution for. Here's what I want the script to do...
1. Connect to AD and enumerate all computers, creating a textfile with all
the computer names.
2. Create a folder structure, with a separate folder named for each
computer. (root folder\serverA, rootfolder\serverB, etc...)
3. Read the textfile created in step 1, then connect to each computer in the
file, and search for all files on the computer with a particular extension.
4. Copy all the files found in step 3 to the appropriately named folder in
the folder structure created in step 2.
5. Move on to the next computer and repeat step 4.
6. Log the activity.
So far, I've been able to accomplish step 1 and 2 easily enough, and step 3
kinda works too. Step 4 is where I'm stuck. I'm trying to use the fileCopy
method, but I'm having trouble passing a variable based on the readline from
the textfile as a source location, as well as passing a variable based on
the computername as part of the destination path.
All the documentation I've been reading uses filenames and paths for the
fileCopy method. Am I looking at the wrong method to do this or do I just
not know how to get the variables right? I get a variety of errors,
depending on how I set up the script line that copies the files, but it's
always in the same spot. I'm NOT getting permission errors; It's syntax or
file not found errors.
I've attached the working code below; this will enumerate all the computers
and create the folder structure:
'Code follows:
Const ADS_SCOPE_SUBTREE = 2
Dim objOU
Dim objFileSystem
Dim objComputernames
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objComputernames = objFileSystem.createTextFile("C:\computernames.txt",
TRUE)
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 FROM 'LDAP://DC=eccad,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
objComputernames.WriteLine objRecordset.Fields(0).Value
objRecordSet.MoveNext
Loop
Wscript.Echo "Computers Enumerated"
Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\computernames.txt", ForReading)
i = 0
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
objDictionary.Add i, strNextLine
i = i + 1
Loop
For Each objItem in objDictionary
StrComputer = objDictionary.Item(objItem)
Set objFolder = objFSO2.CreateFolder( "C:\eccad computers\" &
StrComputer)
Next
Wscript.Echo "Folders Created"
'End of code
I've also been able to use the following code to successfully search a
computer for files with a particular extension, but so far have only been
able to make it wscript.echo the filename. I cannot figure out how to take
that information and pass it to the fileCopy method.
'code follows:
sComputer = "modoc.eccad.com"
wscript.echo "Checking: " & sComputer
set oFSO=createobject("scripting.filesystemobject")
Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Directory",,48)
For Each objItem in colItems
'objItem will be c:\folder\folder\file
'needs converting to \\scomputer\c$\folder etc
sDrive=left(objItem.name,1)
sPath=mid(objItem.name,3) ' start after C:
sFolder="\\" & sComputer & "\" & sDrive & "$" & sPath
set oFolder=oFso.GetFolder(sFolder)
for each oFile in oFolder.files
sExt=lcase("." & oFso.GetExtensionName(sFolder & oFile.name))
If instr("|.mdb|",sExt) and sExt<>"." Then
wscript.echo sFolder & oFile.name
'previous line gets me the file path and name, but I can't
'figure out how to write the next line to perform the copy action.
end if
next
Next
'End of code
Can anyone help guide me to a way to copy the files? I'll worry about
running this against more than one computer once I get past the file copy
hurdle. :-)
Thanks!
Charlie Kaiser
***@spamtrapessexcreditspamtrap.com
(remove spamtrap for valid email address)
hard. :-)
I'm working on an enterprise script, and have a problem that I can't seem to
find a solution for. Here's what I want the script to do...
1. Connect to AD and enumerate all computers, creating a textfile with all
the computer names.
2. Create a folder structure, with a separate folder named for each
computer. (root folder\serverA, rootfolder\serverB, etc...)
3. Read the textfile created in step 1, then connect to each computer in the
file, and search for all files on the computer with a particular extension.
4. Copy all the files found in step 3 to the appropriately named folder in
the folder structure created in step 2.
5. Move on to the next computer and repeat step 4.
6. Log the activity.
So far, I've been able to accomplish step 1 and 2 easily enough, and step 3
kinda works too. Step 4 is where I'm stuck. I'm trying to use the fileCopy
method, but I'm having trouble passing a variable based on the readline from
the textfile as a source location, as well as passing a variable based on
the computername as part of the destination path.
All the documentation I've been reading uses filenames and paths for the
fileCopy method. Am I looking at the wrong method to do this or do I just
not know how to get the variables right? I get a variety of errors,
depending on how I set up the script line that copies the files, but it's
always in the same spot. I'm NOT getting permission errors; It's syntax or
file not found errors.
I've attached the working code below; this will enumerate all the computers
and create the folder structure:
'Code follows:
Const ADS_SCOPE_SUBTREE = 2
Dim objOU
Dim objFileSystem
Dim objComputernames
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objComputernames = objFileSystem.createTextFile("C:\computernames.txt",
TRUE)
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 FROM 'LDAP://DC=eccad,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
objComputernames.WriteLine objRecordset.Fields(0).Value
objRecordSet.MoveNext
Loop
Wscript.Echo "Computers Enumerated"
Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\computernames.txt", ForReading)
i = 0
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
objDictionary.Add i, strNextLine
i = i + 1
Loop
For Each objItem in objDictionary
StrComputer = objDictionary.Item(objItem)
Set objFolder = objFSO2.CreateFolder( "C:\eccad computers\" &
StrComputer)
Next
Wscript.Echo "Folders Created"
'End of code
I've also been able to use the following code to successfully search a
computer for files with a particular extension, but so far have only been
able to make it wscript.echo the filename. I cannot figure out how to take
that information and pass it to the fileCopy method.
'code follows:
sComputer = "modoc.eccad.com"
wscript.echo "Checking: " & sComputer
set oFSO=createobject("scripting.filesystemobject")
Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Directory",,48)
For Each objItem in colItems
'objItem will be c:\folder\folder\file
'needs converting to \\scomputer\c$\folder etc
sDrive=left(objItem.name,1)
sPath=mid(objItem.name,3) ' start after C:
sFolder="\\" & sComputer & "\" & sDrive & "$" & sPath
set oFolder=oFso.GetFolder(sFolder)
for each oFile in oFolder.files
sExt=lcase("." & oFso.GetExtensionName(sFolder & oFile.name))
If instr("|.mdb|",sExt) and sExt<>"." Then
wscript.echo sFolder & oFile.name
'previous line gets me the file path and name, but I can't
'figure out how to write the next line to perform the copy action.
end if
next
Next
'End of code
Can anyone help guide me to a way to copy the files? I'll worry about
running this against more than one computer once I get past the file copy
hurdle. :-)
Thanks!
Charlie Kaiser
***@spamtrapessexcreditspamtrap.com
(remove spamtrap for valid email address)