Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1865
  • Last Modified:

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

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
barryscott007
Asked:
barryscott007
1 Solution
 
RobSampsonCommented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now