Solved

GPO Script

Posted on 2010-09-16
10
801 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
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 500 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
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

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

726 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