Link to home
Start Free TrialLog in
Avatar of greenasp
greenasp

asked on

GPO Script

I need help creating a GPO script to rename the outlook.nk2 (email cache file) to oldoutlook.nk2. The current script is currently set to just delete the file, however, we now have a need to save the old file first. Is it possible to have it run just once? Sorry, this is new territory for me!

Current script.....

On Error Resume Next


Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonat e}!\\.\root\cimv2")
Set WSHShell = CreateObject("WScript.Shell")


Set colProcess = oWMIService.ExecQuery("Select * from Win32_Process")
For Each oProcess in colProcess
If lCase(oProcess.Name) = "outlook.exe" Then
oProcess.Terminate()
WScript.Sleep 10000
End If
Next


userProfile = WSHShell.ExpandEnvironmentStrings("%userprofile%")
strOutlookfolder = userProfile & "\Application Data\Microsoft\Outlook\"


If oFSO.FolderExists(strOutlookfolder) Then
Set strFolder = oFSO.GetFolder(strOutlookfolder)
For Each strFile in strFolder.Files
If lCase(Right(strFile,4)) = ".nk2" then
oFSO.DeleteFile strFile
End If
Next
Else
End If
Avatar of greenasp
greenasp

ASKER

curernt script does not work either...lol
This is the path for the file that needs to be renamed %appdata%\Microsoft\Outlook
ASKER CERTIFIED SOLUTION
Avatar of Procastin8or
Procastin8or
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops... I forgot, you'll need to uncomment these lines. I didn't want to kill my outlook while testing.
For Each oProcess in colProcess
If lCase(oProcess.Name) = "outlook.exe" Then
	oProcess.Terminate()
	WScript.Sleep 10000
End If
Next

Open in new window

Also, it was easier to rename to filename.nk2.old than it was to do oldfilename.nk2.
When I run in powershell I get the following error.  I might be doing something wrong.

On Error Resume Next


Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonat e}!\\.\root\cimv2")
Set WSHShell = CreateObject("WScript.Shell")


Set colProcess = oWMIService.ExecQuery("Select * from Win32_Process")
'For Each oProcess in colProcess
'If lCase(oProcess.Name) = "outlook.exe" Then
      'oProcess.Terminate()
      'WScript.Sleep 10000
'End If
'Next


userProfile = WSHShell.ExpandEnvironmentStrings("%userprofile%")
strOutlookfolder = userProfile & "\Application Data\Microsoft\Outlook\"


If oFSO.FolderExists(strOutlookfolder) Then
      Set strFolder = oFSO.GetFolder(strOutlookfolder)
      For Each strFile in strFolder.Files
            If lCase(Right(strFile,4)) = ".nk2" then
                  strFileNew = strFile & ".old"
                  oFSO.MoveFile strFile , strFileNew
                  strFileNew = ""
            End If
      Next
      Else
End If
Unexpected token 'if' in expression or statement.
At line:11 char:2

Unexpected token 'lCase' in expression or statement.
At line:11 char:5

Unexpected token '(' in expression or statement.
At line:11 char:10

Unexpected token 'oProcess.Name' in expression or statement.
At line:11 char:11

Unexpected token ')' in expression or statement.
At line:11 char:24

Unexpected token 'if' in expression or statement.
At line:11 char:2

Unexpected token 'lCase' in expression or statement.
At line:11 char:5

Unexpected token '(' in expression or statement.
At line:11 char:10

Unexpected token 'oProcess.Name' in expression or statement.
At line:11 char:11

Unexpected token ')' in expression or statement.
At line:11 char:24
This is what I am currently running in the powershell ise .  Should I be using something else?
On Error Resume Next


Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonat e}!\\.\root\cimv2")
Set WSHShell = CreateObject("WScript.Shell")


Set colProcess = oWMIService.ExecQuery("Select * from Win32_Process")

For Each oProcess in colProcess
If lCase(oProcess.Name) = "outlook.exe" Then
	oProcess.Terminate()
	WScript.Sleep 10000
End If
Next


userProfile = WSHShell.ExpandEnvironmentStrings("%userprofile%")
strOutlookfolder = userProfile & "\Application Data\Microsoft\Outlook\"


If oFSO.FolderExists(strOutlookfolder) Then
	Set strFolder = oFSO.GetFolder(strOutlookfolder)
	For Each strFile in strFolder.Files
		If lCase(Right(strFile,4)) = ".nk2" then
			strFileNew = strFile & ".old"
			oFSO.MoveFile strFile , strFileNew
			strFileNew = ""
		End If
	Next
	Else
End If

Open in new window

Thank You!
it looks like it did not change the file name at %appdata%\microsoft\outlook
Sorry, did work! thanks!