We help IT Professionals succeed at work.
Get Started

GPO or logon script - Need elevate admin rights

2,867 Views
Last Modified: 2012-05-11
Hi,

We would like to execute the following script which requires admin rights in order to work. Basically the script checks the user profiles that are created locally in the workstation and deletes the documents that are older than 1 day within the profile.

We would like to use the script as part of the logon script or via gpo and I was wondering if there is any way to elevate the rights so that script runs with admin rights and does its job?

Any help?

Thank you!
On Error Resume Next

''' Set Registry Hive
Const HKEY_LOCAL_MACHINE = &H80000002

'''  This section of code creates the list of user profiles
'''
strComputer = "."
Set objRegistry=GetObject("winmgmts:\\" & _ 
    strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
objRegistry.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys
 
''' Create a loop to cycle through the profiles
'''

For Each objSubkey In arrSubkeys
  strValueName = "ProfileImagePath"
  strSubPath = strKeyPath & "\" & objSubkey
  objRegistry.GetExpandedStringValue _
    HKEY_LOCAL_MACHINE,strSubPath,strValueName,strValue
 
  strValue = strValue & "\Desktop"

''' Delete any file on desktop that is older than one day old
''' Skip the default user desktop (although this may not be necessary)

  If NOT strValue ="C:\WINDOWS\system32\config\systemprofile\Desktop" _
  Then

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FolderExists(strValue) Then
      Set Folder = objFSO.GetFolder(strValue)
      For Each File in Folder.Files
        If DateDiff("d",File.DateLastModified,Now) > 1 Then _
        objFSO.DeleteFile File
      Next
    End If

  End If

  Set objFSO = Nothing

Next

Set objRegistry = Nothing

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE