Solved

Windows Logoff GPO script/batch file to clear my documents and desktop, leaving shortcuts intact.

Posted on 2011-02-16
2
1,585 Views
Last Modified: 2012-05-11
Hi,

I am currently looking for a logoff script or batch file that i can place in our group policy for certain AD accounts. I need to clear the desktop and my documents of these accounts at each logoff, however i must ensure that it does NOT DELETE shortcut/link LNK files on the desktop.

Many thanks in advance

0
Comment
Question by:barryscott007
2 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34923653
Hi there, try this to delete all items from Desktop and My Documents, without deleting .lnk files from the Desktop.

Please test it on a test system first.

Regards,

Rob.
' THIS LINE MUST BE PLACED IN THE MAIN CODE TO ALLOW THE DICTIONARY
' OBJECT TO REMAIN PERSISTENT FOR THE ISMEMBEROFGROUP FUNCTION
Dim objMemberships

Set objNetwork = CreateObject("WScript.Network")
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
Set objShell = CreateObject("WScript.Shell")
Set objShellApp = CreateObject("Shell.Application")

Const DESKTOP = &H10&
Const MY_DOCUMENTS = &H5&

strGroup = "Test Group"
If IsMemberOfGroup(objUser, strGroup) = True Then

	strFolder = objShellApp.Namespace(DESKTOP).Self.Path
	' DeleteFiles accepts a certain file extension of files to NOT delete
	DeleteFiles strFolder, ".lnk"

	strFolder = strFolder = objShellApp.Namespace(MY_DOCUMENTS).Self.Path
	' DeleteFiles accepts a certain file extension of files to NOT delete
	DeleteFiles strFolder, ""

Else
	'WScript.Echo "You are not a member of " & strGroup
End If

Sub DeleteFiles(strFolderPath, strExcludeExtension)
	For Each objSubFolder In objFSO.GetFolder(strFolderPath).SubFolders
		DeleteFiles objSubFolder.Path, strExcludeExtension
	Next
	For Each objFile In objFSO.GetFolder(strFolderPath).Files
		If strExcludeExtension <> "" Then
			If Right(LCase(objFile.Name), Len(strExcludeExtension)) <> LCase(strExcludeExtension) Then objFile.Delete True
		Else
			objFile.Delete
		End If
	Next
	objFSO.DeleteFolder strFolderPath
End Sub

Function IsMemberOfGroup(objADUser, strGroupCN)
	If IsEmpty(objMemberships) = True Then
		Set objMemberships = CreateObject("Scripting.Dictionary")
		objMemberships.Add LCase("ALL"), 0
		If IsNull(objADUser.MemberOf) = False Then
			If TypeName(objADUser.MemberOf) = "String" Then
				objMemberships.Add LCase(Mid(Split(objADUser.MemberOf, ",")(0), 4)), 0
			Else
				For Each strGroupName In objADUser.MemberOf
					objMemberships.Add LCase(Mid(Split(strGroupName, ",")(0), 4)), 0
				Next
			End If
		End If
	End If
	If objMemberships.Exists(LCase(strGroupCN)) = True Then
		IsMemberOfGroup = True
	Else
		IsMemberOfGroup = False
	End If
End Function

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

[b]Ok so now I will show you how to add a user name to the description at login. [/b] First connect to your DC (Domain Controller / Active Directory Server) SET PERMISSIONS FOR SCRIPT TO UPDATE COMPUTER DESCRIPTION TO USERNAME 1. Open Active …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now