Updating Default Profile Script

From Wiki

Jump to:navigation, search

Creator: IT Support Staff @ HCHS

Script Language: VBS

Type: Start-up

Forum topic Link: http://www.edugeek.net/forums/wiki-announcements/17391-updating-default-profile-script.html

This script is handy for administrators who use the local profile method. This is a start-up script that automatically updates the default profile based on the computers location field of the computer in Active Directory. Once it has checked the location field on the computer account it will then look for a folder called the same in the specified share. If there is it will then compare the modified dates if the one on the server is slightly newer, then it will update the client.

<vb>on error resume next 'Checks that the Default Profile is the same on the PC as that set on the server 'Updates to most current version if required by checking dateLastModified on ntuser.dat file 'Takes folder name from 'Location' field in computer account in Active Directory 'If folder does not exist in \\servername\profiles folder, script will create folder and copy Default User folder into it

Dim filepath, datelocal, dateserver, entry, array, computername, profilepath, file, profilematch, checksize1, checksize2

Set filesys = CreateObject("Scripting.FileSystemObject") Set objShell = CreateObject ("WScript.Shell") Set objFSO = CreateObject ("Scripting.FileSystemObject") Set objNetwork = CreateObject("Wscript.Network")

sComputer = lcase(objNetwork.computername) IF left(sComputer,6) = "hc-lap" THEN wscript.quit

serverpath = "\\servername\profiles\"

dateremote = "" datelocal = ""

call ProfilePathcheck

IF NOT objfso.FolderExists(profilepath) THEN objfso.CreateFolder(profilepath)

call checkprofile

IF profilematch = "FALSE" then call updateprofile IF profilematch = "TRUE" then call profileok IF computername <> sComputer then call noentry

wscript.quit

sub checkprofile

'check date on local ntuser.dat

IF objFSO.FileExists("C:\Documents and Settings\Default User\NTUSER.DAT") THEN datelocal = objFSO.GetFile("C:\Documents and Settings\Default User\NTUSER.DAT").dateLastModified END IF

IF datelocal = "" then datelocal = 0

'check date on remote IF objFSO.FileExists(PROFILEPATH & "\Default User\NTUSER.DAT") THEN dateremote = objFSO.GetFile(PROFILEPATH & "\Default User\NTUSER.DAT").dateLastModified END IF IF dateremote = "" then dateremote = 0

IF datelocal = dateremote THEN profilematch = "TRUE" ELSE profilematch = "FALSE"

end sub

sub updateprofile IF datelocal>dateremote THEN CALL pctoserver IF dateremote>datelocal THEN CALL servertopc end sub

sub servertopc ' copy from server to temp location on local PC objFSO.CreateFolder ("C:\Documents and Settings\Default User temp\") objFSO.CopyFolder (profilepath & "\Default User"), "C:\Documents and Settings\Default User temp\" ' Delete existing default user files

Set objFolder = objFSO.GetFolder ("C:\Documents and Settings\Default User\") For Each objSubFolder in objFolder.SubFolders objFSO.DeleteFolder objSubFolder.Path, TRUE NEXT

' copy from temp location to Default User

objFSO.CopyFolder "C:\Documents and Settings\Default User temp\Default User\*.*", "C:\Documents and Settings\Default User\" objFSO.CopyFile "C:\Documents and Settings\Default User temp\Default User\*.*", "C:\Documents and Settings\Default User\"

' delete temp folder

Set objFolder = objFSO.GetFolder ("C:\Documents and Settings\Default User temp\") For Each objSubFolder in objFolder.SubFolders objFSO.DeleteFolder objSubFolder.Path, TRUE NEXT objFSO.DeleteFile("C:\Documents and Settings\Default User temp\*.*"), DeleteReadOnly objFSO.DeleteFolder("C:\Documents and Settings\Default User temp"), DeleteReadOnly

'Delete Administrator profile folder

Set objFolder = objFSO.GetFolder ("C:\Documents and Settings\administrator\") For Each objSubFolder in objFolder.SubFolders objFSO.DeleteFolder objSubFolder.Path, TRUE NEXT objFSO.DeleteFile("C:\Documents and Settings\administrator\*.*"), TRUE objFSO.DeleteFolder("C:\Documents and Settings\administrator") call checksize end sub

sub pctoserver objShell.Popup "Updating Default Profile on server for " & computername & VBCR & "Please wait", 3 ' copy from server to temp location on local PC IF NOT objfso.folderexists(profilepath & "\Default User") THEN objfso.CreateFolder(profilepath & "\Default User") objFSO.CreateFolder (profilepath & "\Default User temp\") objFSO.CopyFolder ("C:\Documents and Settings\Default User"), (profilepath & "\Default User temp\") ' Delete existing default user files Set objFolder = objFSO.GetFolder(profilepath & "\Default User")

For Each objSubFolder in objFolder.SubFolders objFSO.DeleteFolder objSubFolder.Path, TRUE NEXT

objFSO.DeleteFile(profilepath & "\Default User\*.*"), TRUE

' copy from temp location to Default User

objFSO.CopyFolder profilepath & "\Default User temp\Default User\*.*", profilepath & "\Default User\" objFSO.CopyFile profilepath & "\Default User temp\Default User\*.*", profilepath & "\Default User\"

' delete temp folder

Set objFolder = objFSO.GetFolder (profilepath & "\Default User temp\") For Each objSubFolder in objFolder.SubFolders objFSO.DeleteFolder objSubFolder.Path, TRUE NEXT objFSO.DeleteFile(profilepath & "\Default User temp\*.*"), DeleteReadOnly objFSO.DeleteFolder(profilepath & "\Default User temp"), DeleteReadOnly

call checksize end sub

sub profileok

call checksize end sub

sub noentry wscript.quit

end sub sub checksize 'Check folder on localPC is same size as folder on server in case file copy error has occurred checksize1 = objFSO.GetFolder("C:\Documents and Settings\Default User").size checksize2 = objFSO.GetFolder(PROFILEPATH & "\Default User").Size

'wscript.echo "Local Default User Size - " & checksize1 & vbcr & "Remote Default User Size - " & checksize2 end sub


SUB ProfilePathcheck

'retrieves the location attribute for a computer account in Active Directory. on error resume next

Dim computer, objcomputer, Location, Locationarray DIM objNetwork DIM description DIM ou

' Get the computerName of PC set objNetwork = createobject("Wscript.Network")

' Call function to find LDAP account from computer name

ou = getOUByComputerName(scomputer)

Set objComputer = GetObject("LDAP://" & ou)

objProperty = objComputer.Get("Location")

If objProperty = "" Then

  wscript.quit

Else

   profilepath = serverpath & objProperty
   objProperty = Null

End If

End SUB

function getOUByComputerName(byval scomputer) ' *** Function to find ou/container of computer object from computer name ***

DIM namingContext, ldapFilter, ou DIM cn, cmd, rs DIM objRootDSE

' Bind to the RootDSE to get the default naming context for ' the domain. e.g. dc=HCHS,dc=co,dc=uk set objRootDSE = getobject("LDAP://RootDSE") namingContext = objRootDSE.Get("defaultNamingContext") set objRootDSE = nothing

' Construct an ldap filter to search for a computer object ' anywhere in the domain with a name of the value specified. ldapFilter = "<LDAP://" & namingContext & _

	">;(&(objectCategory=Computer)(name=" & scomputer & "))" & _

";distinguishedName;subtree"

' Standard ADO code to query database set cn = createobject("ADODB.Connection") set cmd = createobject("ADODB.Command")

cn.open "Provider=ADsDSOObject;" cmd.activeconnection = cn cmd.commandtext = ldapFilter

set rs = cmd.execute

if rs.eof <> true and rs.bof <> true then ou = rs(0) getOUByComputerName = ou

end if

rs.close cn.close

end function </vb>