1

スクリプトの実行中に表示されるエラーを修正する方法について質問があります。%COMPUTERNAME% 環境変数の使用方法に関係していると確信しています。

私のスクリプトが行うことは、いくつかのファイルをローカルで圧縮し、robocopy を使用してそれらをマウントまたは共有ドライブにコピーし、ファイル サイズが同じかどうかを確認し、同じ場合は元のコンピューター上のファイルを削除することです。プロセスのいずれかのステップでエラーが発生すると、スクリプトは終了します。

最終的な宛先パスに「%COMPUTERNAME%」を追加しなければ、スクリプトは完全に正常に動作します。(圧縮ファイルが最終的に配置される場所)圧縮ファイルは、元のホストの名前で独自のフォルダーに配置する必要があります。これは、このスクリプトがすべて同じ場所に移動する多くの異なるマシンで実行されるためです。

したがって、基本的には次のようにする必要があります。

E:\LocalHostName\TestZip.zip

これで、スクリプトは、zip ファイルがコピーされているときにフォルダーを正常に構築します。ファイル サイズのチェックが開始されると、問題が発生します。「FileToBeCompare2」行で「ファイルが見つかりません」というエラーが表示されます。%COMPUTERNAME% 環境変数を認識していないため、エラーが発生する理由は理解できますが、この問題に対処する方法がわかりません。

また、エラーが発生した場合に「スクリプト中にエラーが発生しました」などのテキスト ファイルが出力フォルダーに生成される機能を追加しようとしています。

事前にご協力いただきありがとうございます。スクリプトは以下にあります。

'-------------------------------------------------------------------------------------------
'This script is used to zip files locally, copy them to a new location, verify that the
'files were copied correctly, and then delete the files from the original source.
'In it's current state it is being used as a means to zip event files and move them
'to a central location.

'Run with administrator priveleges.

'-----------------------------------------------------------------------------------------------------
Option Explicit

Dim sDirectoryPath, sLocalDestinationPath, sFinalDestinationPath, sOutputFilename, Shell, sFileExt, sFilePrefix

Set Shell = WScript.CreateObject("WScript.Shell")

'Specify Directory Path where files to be zipped are located
'Specify local destination for zipped files
'Specify final destination path for zippped files
'Specify file extension name to look for
'Specify prefix of filename to look for

sDirectoryPath = "C:\Testscripts\"
sLocalDestinationPath = "C:\ScriptOutput\"
sFinalDestinationPath = "E:\CopyTestFolder\" & sOutputFilename & "\" 
sFileExt = ".evtx"
sFilePrefix = "Archive*"
sOutputFilename = shell.ExpandEnvironmentStrings("%COMPUTERNAME%") 'Environment variables needed for grabbing hostname

Dim ZipCommand, RobocopyCommand, RunCommand, filesys, filetext
Dim d : d = Date() 
Dim dateStr : dateStr = Year(d) & "-" & Right("00" & Month(d), 2) & "-" & Right("00" &     Day(d), 2) 'Date String
Dim t : t = Time()
Dim timeStr: timeStr = Hour(t) & "-" & Right("00" & Minute(t), 2) & "-" & Right("00" & Second(t), 2) 'Time String
Dim FullFileName

FullFileName = sOutputFilename & "-" & dateStr & "-" & timeStr & ".zip "

'Following command runs 7-zip and grabs the files to be zipped from your set sDirectoryPath, zips them into set sLocalDestinationPath
'and names the file with the localhost name and date/time

ZipCommand = """C:\Program Files\7-zip\7z.exe"" a " & sLocalDestinationPath & FullFileName & sDirectoryPath & sFilePrefix & sFileExt

RunCommand = Shell.Run(ZipCommand,0,true)

if err.Number <> 0 then
    WScript.Echo "An error has occurred during the zip process, re-run Script."     WScript.Quit
end if

Wscript.Sleep 2000

'The following command creates a folder named after the host computer where the files are being copied from 

Dim newfolder, newfolderpath, filesys2

newfolderpath = "E:\CopyTestFolder\" & sOutputFilename & "\"
set filesys2 = CreateObject("Scripting.FileSystemObject")
If Not filesys2.FolderExists(newfolderpath) Then
    Set newfolder = filesys2.CreateFolder(newfolderpath)
End If

'Following command runs Robocopy from command line, moves files from your set sLocalDestinationPath to your set sFinalDestinationPath       

WScript.Echo "Robocopy.exe " & sLocalDestinationPath & " " & sFinalDestinationPath  
RobocopyCommand = "Robocopy.exe " & sLocalDestinationPath & " " & sFinalDestinationPath 
RunCommand = Shell.Run(RobocopyCommand,0,true)

if err.Number <> 0 then
    WScript.Echo "An error has occured copying the files, re-run Script."
    WScript.Quit
end if

Dim fso, FileToBeCompared1, FileToBeCompared2

Set fso = CreateObject("Scripting.FileSystemObject")

'Setting the Local file to be compared
Set FileToBeCompared1 = fso.GetFile(sLocalDestinationPath & FullFileName) 
WScript.echo sFinalDestinationPath & FullFileName

'Setting the file copied to final destination to be compared
Set FileToBeCompared2 = fso.GetFile(sFinalDestinationPath & FullFileName)       

If FileToBeCompared1.size = FileToBeCompared2.size then
    fso.DeleteFile("C:\Testscripts\Archive*.evtx") 'This will be the path where events are being Archived to. (Non restricted path)
    fso.DeleteFolder("C:\ScriptOutput") 'This deletes the archive folder that 7-zip builds each time this script is run
else
    WScript.Echo "File sizes do not match, File was not fully copied, Re run script."   
    WScript.Quit
end if
4

1 に答える 1

5

fso.GetFile()は自動的に展開されないため、次%COMPUTERNAME%のように変更sFinalDestinationPathして使用しますsOutputFilename

sOutputFilename = shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
sFinalDestinationPath = "E:\CopyTestFolder\" & sOutputFilename & "\"
于 2012-07-23T23:09:32.043 に答える