?
Solved

GPO Script

Posted on 2010-09-16
10
Medium Priority
?
803 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 2

Author Comment

by:greenasp
ID: 33694520
curernt script does not work either...lol
0
 
LVL 2

Author Comment

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

Accepted Solution

by:
Procastin8or earned 2000 total points
ID: 33695633
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 2

Expert Comment

by:Procastin8or
ID: 33695649
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
ID: 33695665
Also, it was easier to rename to filename.nk2.old than it was to do oldfilename.nk2.
0
 
LVL 2

Author Comment

by:greenasp
ID: 33695686
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
ID: 33695822
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
ID: 33696086
Thank You!
0
 
LVL 2

Author Comment

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

Author Comment

by:greenasp
ID: 33698844
Sorry, did work! thanks!
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

752 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