benaw Posted December 15, 2008 Posted December 15, 2008 hey guys,i am trying to write a script and am a bit new to this.what i want to do is ping a range of computers resolve the computer name export that to a text file.then from that i would like to write a script similar to http://www.microsoft.com/technet/scriptcen...nario1-vbs.mspx which i'll use to make the computers logon as administrator and run another script, i want that script to copy user profile to a network share.is this script a little too ambitious?? what i want is a script that copys everything from each computer other than the folders i know about and store the folders by computer name from, all from an ip range.the only way i know of to copy a folder in vbs and omit sub folders is to copy the entire folder to a temporary folder then delete the ones i don't want like admin default, from the doc's & settings etc, but i'm not going to be able to do this because obviously i'm gonna get files in use error. because of this i did some research into robocopy to see if i could use that to omit known unwanted folders i don't know if that will work yet, one of the problems is i don't know the user name for each machine.i really don't know if this can work or if there is a better way please let me know what you think, can this be done?this script does the reverse of what i want as far as the docs and settings. it deletes the user which is the part i want.This part deletes all user profiles except for the ones listed as> exceptions.> Can probably help you to copy all but some profiles for the backups.>>>> '----------------------------- Remove Normal User Profile> ------------------------------------>'---------------------------------------------------------------------------------------------> Private Function RemoveUserProfile(strWksName)> Dim objFS> Dim strProfilePath> Dim objProfileFolder> Dim objSubFolder> Dim strExceptions> Dim arrExceptions> Dim intIndex> Dim bolDelete> Dim strLog>> On Error Resume Next> strExceptions = "Administrator,All Users,Default> User,Localservice,NetworkService"> arrExceptions = Split(strExceptions,",")>> Set objFS = Wscript.CreateObject("Scripting.FileSystemObject")> strProfilePath = "\\" & strWksName & "\D$\Documents and Settings"> If objFS.FolderExists(strProfilePath) Then> Set objProfileFolder = objFS.GetFolder(strProfilePath)> For Each objSubFolder in objProfileFolder.SubFolders> bolDelete = True> For intIndex = 0 To UBound(arrExceptions)> If UCase(objSubFolder.Name) = UCase(arrExceptions(intIndex)) Then> bolDelete = False> Exit For> End If> Next>> If bolDelete Then> Wscript.Echo "Delete user profile of " & objSubFolder.Name> strLog = strLog & vbCrLf & "Delete user profile of " &> objSubFolder.Name> objSubFolder.Delete True> End If> Next> End if> RemoveUserProfile = strLog> End Functionthe below scripts will ping a ip range and the other creates a simple text filebut i need to combine themOption ExplicitDim strIPAddress, objShell, objFSO, strTemp, strTempFileDim strSubNet, intStart, intEnd, strResult, kSet objFSO = CreateObject("Scripting.FileSystemObject")Set objShell = CreateObject("Wscript.Shell")' Specify temporary file to save ping results.strTemp = objShell.ExpandEnvironmentStrings("%TEMP%")strTempFile = strTemp & "\RunResult.tmp"' Specify the subnet to check and the starting and ending addresses.strSubNet = "10.10.5."intStart = 0intEnd = 255' Check all possible addresses.For k = intStart To intEndstrIPAddress = strSubNet & CStr(k)' Check if the addresses responds to a ping.If (PingIP(strIPAddress, 1, 100) = True) Then' Ping again to resolve the host name.strResult = PingMachine(strIPAddress, 1, 100)Wscript.Echo strIPAddress & " " & strResultEnd IfNextFunction PingIP(strHost, intPings, intTO)' Returns True if IP Address can be pinged, False otherwise.' Based on a program by Alex Angelopoulos and Torgeir Bakken.' Variables objFSO, objShell, and strTempFile have global scope' and must be declared in the main program.Dim objFile, strResultsConst OpenAsDefault = -2Const FailIfNotExist = 0Const ForReading = 1If (intPings = "") ThenintPings = 2End IfIf (intTO = "") ThenintTO = 750End If' Ping the machine and pipe results to temporary file.objShell.Run "%comspec% /c ping -n " & intPings & " -w " & intTO _& " " & strHost & ">" & strTempFile, 0, True' Read the file.Set objFile = objFSO.OpenTextFile(strTempFile, ForReading, _FailIfNotExist, OpenAsDefault)strResults = objFile.ReadAllobjFile.Close' Check for response.Select Case InStr(strResults, "TTL=")Case 0' No response.PingIP = FalseCase Else' Host responded to ping.PingIP = TrueEnd SelectEnd FunctionFunction PingMachine(strHost, intPings, intTO)' Returns host name if IP Address can be pinged.' Based on a program by Alex Angelopoulos and Torgeir Bakken.' Variables objFSO, objShell, and strTempFile have global scope' and must be declared in the main program.Dim objFile, strResults, intIndex1, intIndex2Const OpenAsDefault = -2Const FailIfNotExist = 0Const ForReading = 1If (intPings = "") ThenintPings = 2End IfIf (intTO = "") ThenintTO = 750End If' Ping the machine and pipe results to temporary file.objShell.Run "%comspec% /c ping -a -n " & intPings & " -w " & intTO _& " " & strHost & ">" & strTempFile, 0, True' Read the file.Set objFile = objFSO.OpenTextFile(strTempFile, ForReading, _FailIfNotExist, OpenAsDefault)strResults = objFile.ReadAllobjFile.Close' Check for response.Select Case InStr(strResults, "TTL=")Case 0' No response.PingMachine = ""Case Else' Host responded to ping. Parse for host name.intIndex1 = InStr(strResults, "Pinging ")intIndex2 = InStr(intIndex1, strResults, " [")If (intIndex1 > 0) And (intIndex2 > intIndex1) ThenPingMachine = Mid(strResults, intIndex1 + 8, _intIndex2 - intIndex1 - 8)ElsePingMachine = "<unknown>"End IfEnd SelectEnd Functionthat code looks really good i just need to combine it with this:Option ExplicitDim objFSO, objFolder, objShell, objTextFile, objFileDim strDirectory, strFile, strTextstrDirectory = "j:\DATA_1\scripts\test"strFile = "\answer.txt"strText = "Book Another Holiday " & date() &" "& Time()' Create the File System ObjectSet objFSO = CreateObject("Scripting.FileSystemObject")' Check that the strDirectory folder existsIf objFSO.FolderExists(strDirectory) Then Set objFolder = objFSO.GetFolder(strDirectory)Else Set objFolder = objFSO.CreateFolder(strDirectory) 'WScript.Echo "Just created " & strDirectoryEnd IfIf objFSO.FileExists(strDirectory & strFile) Then Set objFolder = objFSO.GetFolder(strDirectory)Else Set objFile = objFSO.CreateTextFile(strDirectory & strFile) 'Wscript.Echo "Just created " & strDirectory & strFileEnd Ifset objFile = nothingset objFolder = nothing' OpenTextFile Method needs a Const value' ForAppending = 8 ForReading = 1, ForWriting = 2Const ForAppending = 8Set objTextFile = objFSO.OpenTextFile _(strDirectory & strFile, ForAppending, True)' Writes strText every time you run this VBScriptobjTextFile.WriteLine(strText)objTextFile.Close' Bonus or cosmetic section to launch explorer to check fileIf err.number = vbEmpty then Set objShell = CreateObject("WScript.Shell") objShell.run ("Explorer" &" " & strDirectory & "\" )Else WScript.echo "VBScript Error: " & err.numberEnd IfWScript.Quitthis should then get me a list of computer names in a text file.
benaw Posted December 15, 2008 Author Posted December 15, 2008 sry that post was a bit of a mess there must be a better way of doing this what i need to do is backup all the user data on each machine, i do not have access to the domain controller but i have the local admin password for each machine. i'm going to look into robocopy today see if it will follow a little script that lists all the computer names i want to target and copy folders omitting certain sub folders.firstly is this possible with robocopy? if so i'll keep looking into itand secondly could you give me advise on combining those two scripts so that i can get a list of all the computer names in a text file i know it's all there i just haven't been able to make sense of it yet.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now