无法通过Visual Basic复制文件

时间:2019-03-04 14:05:23

标签: vbscript outlook copying gpo

好的,所以我有这段代码(在下面发布),用于复制Outlook PST文件,并且与完整位置文件路径一起使用时,它运行得很好。我添加了一种在第一行中运行%UserProfile%的方法,因为这需要在GPO的域上下文中运行,并且单独进行是不可行的。这会运行并关闭Outlook,并在适当的时间重新打开它,除非有一件事情不对劲。它不再复制适当的文件。我回显了最初的%userprofile%部分,它以驱动器号\ users \ userprofile的形式正确读取。我不确定这在哪里破裂或如何识别。任何帮助将不胜感激

'===================BEGIN MODIFY====================================
Set objShell = CreateObject("WScript.Shell")
userProfilePath = objShell.ExpandEnvironmentStrings("%UserProfile%")

'Set the amount of pst-files you want to copy. Start counting at 0!
ReDim pst(1)

'Define the location of each pst-file to backup. Increase the counter!
pst(0) = "%UserProfile%\AppData\Local\Microsoft\Outlook\PST\Outlook Data File - mike.pst"
pst(1) = "%UserProfile%\AppData\Local\Microsoft\Outlook\PST\Archive.pst"

'Define your backup location
BackupPath = "%UserProfile%\Documents\Outlook Backups\"

'Keep old backups? TRUE/FALSE
KeepHistory = FALSE

'Maximum time in milliseconds for Outlook to close on its own
delay = 30000 'It is not recommended to set this below 8000

'Start Outlook again afterwards? TRUE/FALSE
start = TRUE

'===================STOP MODIFY====================================

'Close Outlook
Call CloseOutlook(delay)

'Outlook is closed, so we can start the backup
Call BackupPST(pst, BackupPath, KeepHistory)

'Open Outlook again when desired.
If start = TRUE Then
  Call OpenOutlook()
End If


Sub CloseOutlook(delay)
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

  'If Outlook is running, let it quit on its own.
  For Each Process in objWMIService.InstancesOf("Win32_Process")
    If StrComp(Process.Name,"OUTLOOK.EXE",vbTextCompare) = 0 Then
      Set objOutlook = CreateObject("Outlook.Application")
      objOutlook.Quit
      WScript.Sleep delay
      Exit For
    End If
  Next

  'Make sure Outlook is closed and otherwise force it.
  Set colProcessList = objWMIService.ExecQuery _
  ("Select * from Win32_Process Where Name = 'Outlook.exe'")
  For Each objProcess in colProcessList
    objProcess.Terminate()
  Next
  Set objWMIService = Nothing
  Set objOutlook = Nothing
  set colProcessList = Nothing
End Sub


Sub BackupPST(pst, BackupPath, KeepHistory)
  Set fso = CreateObject("Scripting.FileSystemObject")

  If KeepHistory = True Then
    ArchiveFolder = Year(Now) & "-" & Month(Now) & "-" & Day(Now)
    BackupPath = BackupPath & ArchiveFolder & "\"
  End If

  For Each pstPath in pst
    If fso.FileExists(pstPath) Then
      fso.CopyFile pstPath, BackupPath, True
    End If
  Next
  Set fso = Nothing
End Sub


Sub OpenOutlook()
  Set objShell = CreateObject("WScript.Shell")
  objShell.Run "Outlook.exe"
End Sub

1 个答案:

答案 0 :(得分:0)

执行userProfilePath = objShell.ExpandEnvironmentStrings("%UserProfile%")时,将%UserProfile%的路径放入名为userProfilePath的变量中,但不使用此变量。

我认为您的代码应该可以更好地与类似以下内容一起工作:

'Define the location of each pst-file to backup. Increase the counter!
pst(0) = userProfilePath+"\AppData\Local\Microsoft\Outlook\PST\Outlook Data File - mike.pst"
pst(1) = userProfilePath+"\AppData\Local\Microsoft\Outlook\PST\Archive.pst"

'Define your backup location
BackupPath = userProfilePath"\Documents\Outlook Backups\"

代替

'Define the location of each pst-file to backup. Increase the counter!
pst(0) = "%UserProfile%\AppData\Local\Microsoft\Outlook\PST\Outlook Data File - mike.pst"
pst(1) = "%UserProfile%\AppData\Local\Microsoft\Outlook\PST\Archive.pst"

'Define your backup location
BackupPath = "%UserProfile%\Documents\Outlook Backups\"