Solved

GPO Script

Posted on 2010-09-16
10
798 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:greenasp
  • 7
  • 3
10 Comments
 
LVL 2

Author Comment

by:greenasp
Comment Utility
curernt script does not work either...lol
0
 
LVL 2

Author Comment

by:greenasp
Comment Utility
This is the path for the file that needs to be renamed %appdata%\Microsoft\Outlook
0
 
LVL 2

Accepted Solution

by:
Procastin8or earned 500 total points
Comment Utility
This worked for me. (I tested it using other files not my .nk2 files)
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

0
 
LVL 2

Expert Comment

by:Procastin8or
Comment Utility
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

0
 
LVL 2

Expert Comment

by:Procastin8or
Comment Utility
Also, it was easier to rename to filename.nk2.old than it was to do oldfilename.nk2.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 2

Author Comment

by:greenasp
Comment Utility
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
0
 
LVL 2

Author Comment

by:greenasp
Comment Utility
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

0
 
LVL 2

Author Closing Comment

by:greenasp
Comment Utility
Thank You!
0
 
LVL 2

Author Comment

by:greenasp
Comment Utility
it looks like it did not change the file name at %appdata%\microsoft\outlook
0
 
LVL 2

Author Comment

by:greenasp
Comment Utility
Sorry, did work! thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

17 Experts available now in Live!

Get 1:1 Help Now