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.Fi leSystemOb ject")
Set oWMIService = GetObject("winmgmts:{imper sonationLe vel=impers onat e}!\\.\root\cimv2")
Set WSHShell = CreateObject("WScript.Shel l")
Set colProcess = oWMIService.ExecQuery("Sel ect * 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.ExpandEnvironment Strings("% userprofil e%")
strOutlookfolder = userProfile & "\Application Data\Microsoft\Outlook\"
If oFSO.FolderExists(strOutlo okfolder) Then
Set strFolder = oFSO.GetFolder(strOutlookf older)
For Each strFile in strFolder.Files
If lCase(Right(strFile,4)) = ".nk2" then
oFSO.DeleteFile strFile
End If
Next
Else
End If
Current script.....
On Error Resume Next
Set oFSO = CreateObject("Scripting.Fi
Set oWMIService = GetObject("winmgmts:{imper
Set WSHShell = CreateObject("WScript.Shel
Set colProcess = oWMIService.ExecQuery("Sel
For Each oProcess in colProcess
If lCase(oProcess.Name) = "outlook.exe" Then
oProcess.Terminate()
WScript.Sleep 10000
End If
Next
userProfile = WSHShell.ExpandEnvironment
strOutlookfolder = userProfile & "\Application Data\Microsoft\Outlook\"
If oFSO.FolderExists(strOutlo
Set strFolder = oFSO.GetFolder(strOutlookf
For Each strFile in strFolder.Files
If lCase(Right(strFile,4)) = ".nk2" then
oFSO.DeleteFile strFile
End If
Next
Else
End If
ASKER
This is the path for the file that needs to be renamed %appdata%\Microsoft\Outloo k
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Also, it was easier to rename to filename.nk2.old than it was to do oldfilename.nk2.
ASKER
When I run in powershell I get the following error. I might be doing something wrong.
On Error Resume Next
Set oFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set oWMIService = GetObject("winmgmts:{imper sonationLe vel=impers onat e}!\\.\root\cimv2")
Set WSHShell = CreateObject("WScript.Shel l")
Set colProcess = oWMIService.ExecQuery("Sel ect * 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.ExpandEnvironment Strings("% userprofil e%")
strOutlookfolder = userProfile & "\Application Data\Microsoft\Outlook\"
If oFSO.FolderExists(strOutlo okfolder) Then
Set strFolder = oFSO.GetFolder(strOutlookf older)
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
On Error Resume Next
Set oFSO = CreateObject("Scripting.Fi
Set oWMIService = GetObject("winmgmts:{imper
Set WSHShell = CreateObject("WScript.Shel
Set colProcess = oWMIService.ExecQuery("Sel
'For Each oProcess in colProcess
'If lCase(oProcess.Name) = "outlook.exe" Then
'oProcess.Terminate()
'WScript.Sleep 10000
'End If
'Next
userProfile = WSHShell.ExpandEnvironment
strOutlookfolder = userProfile & "\Application Data\Microsoft\Outlook\"
If oFSO.FolderExists(strOutlo
Set strFolder = oFSO.GetFolder(strOutlookf
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
ASKER
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
ASKER
Thank You!
ASKER
it looks like it did not change the file name at %appdata%\microsoft\outloo k
ASKER
Sorry, did work! thanks!
ASKER