Jump to content

Recommended Posts

Posted

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 Function

the below scripts will ping a ip range and the other creates a simple text file

but i need to combine them

Option Explicit

Dim strIPAddress, objShell, objFSO, strTemp, strTempFile

Dim strSubNet, intStart, intEnd, strResult, k

Set 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 = 0

intEnd = 255

' Check all possible addresses.

For k = intStart To intEnd

strIPAddress = 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 & " " & strResult

End If

Next

Function 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, strResults

Const OpenAsDefault = -2

Const FailIfNotExist = 0

Const ForReading = 1

If (intPings = "") Then

intPings = 2

End If

If (intTO = "") Then

intTO = 750

End 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.ReadAll

objFile.Close

' Check for response.

Select Case InStr(strResults, "TTL=")

Case 0

' No response.

PingIP = False

Case Else

' Host responded to ping.

PingIP = True

End Select

End Function

Function 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, intIndex2

Const OpenAsDefault = -2

Const FailIfNotExist = 0

Const ForReading = 1

If (intPings = "") Then

intPings = 2

End If

If (intTO = "") Then

intTO = 750

End 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.ReadAll

objFile.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) Then

PingMachine = Mid(strResults, intIndex1 + 8, _

intIndex2 - intIndex1 - 8)

Else

PingMachine = "<unknown>"

End If

End Select

End Function

that code looks really good i just need to combine it with this:

Option Explicit

Dim objFSO, objFolder, objShell, objTextFile, objFile

Dim strDirectory, strFile, strText

strDirectory = "j:\DATA_1\scripts\test"

strFile = "\answer.txt"

strText = "Book Another Holiday " & date() &" "& Time()

' Create the File System Object

Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the strDirectory folder exists

If objFSO.FolderExists(strDirectory) Then

Set objFolder = objFSO.GetFolder(strDirectory)

Else

Set objFolder = objFSO.CreateFolder(strDirectory)

'WScript.Echo "Just created " & strDirectory

End If

If objFSO.FileExists(strDirectory & strFile) Then

Set objFolder = objFSO.GetFolder(strDirectory)

Else

Set objFile = objFSO.CreateTextFile(strDirectory & strFile)

'Wscript.Echo "Just created " & strDirectory & strFile

End If

set objFile = nothing

set objFolder = nothing

' OpenTextFile Method needs a Const value

' ForAppending = 8 ForReading = 1, ForWriting = 2

Const ForAppending = 8

Set objTextFile = objFSO.OpenTextFile _

(strDirectory & strFile, ForAppending, True)

' Writes strText every time you run this VBScript

objTextFile.WriteLine(strText)

objTextFile.Close

' Bonus or cosmetic section to launch explorer to check file

If err.number = vbEmpty then

Set objShell = CreateObject("WScript.Shell")

objShell.run ("Explorer" &" " & strDirectory & "\" )

Else WScript.echo "VBScript Error: " & err.number

End If

WScript.Quit

this should then get me a list of computer names in a text file.


Posted

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 it

and 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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...