VBScript环境变量 [英] VBScript Environment variables

查看:208
本文介绍了VBScript环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,关于如何修复运行我的脚本时看到的错误。我很确定它与我使用%COMPUTERNAME%环境变量的方式有关。



我的脚本是在本地拉扯一些文件,然后使用robocopy将其复制到已安装或共享的驱动器,然后检查文件大小是否相同,如果是,则删除原始计算机上的文件。如果此过程中的任何步骤产生错误,它将退出脚本。



如果我不在%COMPUTERNAME%中添加到最终的脚本,则脚本工作正常目的地路径。 (压缩文件最终将在哪里)我需要将压缩文件放入自己的文件夹中,并将其与源自其的主机的名称一起放置,因为该脚本将在许多不同的机器上运行,所有这些都将进入相同的位置。 / p>

所以基本上需要看起来像这样:



E:\LocalHostName\TestZip.zip



现在,当复制压缩文件时,脚本将构建文件夹,只要文件大小检查启动,就会出现问题。我收到FileToBeCompared2行错误File not found。我明白为什么错误正在生成,因为它不会识别%COMPUTERNAME%环境变量,但我不知道如何解决这个问题。



我我还会尝试添加一些功能,如果在文本文件中出现错误,文本文件就会出现在输出文件夹中,例如脚本中出现错误。



谢谢你提前的帮助。脚本如下:

 '------------------- -------------------------------------------------- ---------------------- 
'此脚本用于本地压缩文件,将其复制到新位置,验证
'文件被正确复制,然后从原始来源删除文件。
'在当前的状态下,它被用来压缩事件文件并将它们移动到中央位置。

'运行与管理员priveleges。

'----------------------------------------- -------------------------------------------------- ----------
选项显式

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

设置Shell = WScript .CreateObject(WScript.Shell)

'指定要压缩文件的目录路径位于
'指定压缩文件的本地目标
'指定zippped的最终目标路径文件
'指定文件扩展名以寻找
'指定文件名前缀寻找

sDirectoryPath =C:\Testscripts\
sLocalDestinationPath = C:\ScriptOutput\
sFinalDestinationPath =E:\CopyTestFolder\& sOutputFilename& \
sFileExt =.evtx
sFilePrefix =Archive *
sOutputFilename = shell.ExpandEnvironmentStrings(%COMPUTERNAME%)'获取主机名$ b $所需的环境变量b
Dim ZipCommand,RobocopyCommand,RunCommand,filesys,filetext
Dim d:d = Date()
Dim dateStr:dateStr = Year(d)& - &对(00&((d),2)& - &右(00& Day(d),2)'Date String
Dim t:t = Time()
Dim timeStr:timeStr = Hour(t)& - &右(00&分(t),2)& - &右(00& Second(t),2)'时间字符串
Dim FullFileName

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

'以下命令运行7-zip并从要设置的sDirectoryPath中抓取要压缩的文件,将它们压缩到set sLocalDestinationPath
'中,并使用localhost名称命名文件,日期/时间

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

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

如果err.Number<> 0然后
WScript.Echo在zip过程中发生错误,重新运行脚本。 WScript.Quit
end if

Wscript.Sleep 2000

'以下命令创建一个名为主机的文件夹,其中从$ b复制文件
$ b Dim newfolder,newfolderpath,filesys2

newfolderpath =E:\CopyTestFolder\& sOutputFilename& \
set filesys2 = CreateObject(Scripting.FileSystemObject)
如果不是filesys2.FolderExists(newfolderpath)然后
设置newfolder = filesys2.CreateFolder(newfolderpath)
结束如果

'以下命令从命令行运行Robocopy,将文件从您的set sLocalDestinationPath移动到您的集合sFinalDestinationPath

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

如果err.Number<> 0然后
WScript.Echo发生错误复制文件,重新运行脚本。
WScript.Quit
end if

Dim fso,FileToBeCompared1,FileToBeCompared2

设置fso = CreateObject(Scripting.FileSystemObject)

'设置要比较的本地文件
设置FileToBeCompared1 = fso.GetFile(sLocalDestinationPath& FullFileName)
WScript.echo sFinalDestinationPath& FullFileName

'将文件复制到要比较的最终目的地
设置FileToBeCompared2 = fso.GetFile(sFinalDestinationPath& FullFileName)

如果FileToBeCompared1.size = FileToBeCompared2 .size然后
fso.DeleteFile(C:\Testscripts\Archive * .evtx)'这将是存档事件的路径。 (非限制性路径)
fso.DeleteFolder(C:\ScriptOutput)'这将删除每次运行此脚本时7-zip生成的存档文件夹
else
WScript.Echo 文件大小不匹配,文件未完全复制,重新运行脚本。
WScript.Quit
end if


解决方案

p>因为 fso.GetFile()不会自动展开%COMPUTERNAME%,修改 sFinalDestinationPath 使用 sOutputFilename 这样:

  sOutputFilename = shell.ExpandEnvironmentStrings(%COMPUTERNAME%)
sFinalDestinationPath =E:\CopyTestFolder\& sOutputFilename& \


I have a question regarding how I should go about fixing an error that I am seeing when running my script. I am pretty sure it has to do with the way in which I am using the %COMPUTERNAME% environment variable.

What my script does is it zips up some files locally, then copies them using robocopy to a mounted or shared drive, then checks to see if the file sizes are the same, and if they are then it deletes the files on the original computer. If any step in the process produces an error it exits the script.

Now the script works perfectly fine if I do not add in the "%COMPUTERNAME%" to the final destination path. (Where the zipped files will eventually be) I need the zipped files to be placed into their own folders with the name of the host from which it originated, because this script will be run on many different machines all going to the same location.

So basically it needs to look something like this:

E:\LocalHostName\TestZip.zip

Now the script will build the folder just fine when the zipped files are being copied over, the problem occurs once the file size check starts. I am getting the error of "File not found" for the line "FileToBeCompared2". I understand why the error is being produced, because it is not recogizing the %COMPUTERNAME% environment variable, but I do not know how to go about addressing this issue.

I am also going to try to add in some functionality where if an error occurs a text file with something like "An error occured during the script" is produced in the output folder.

Thank you for all your help in advance. The script is found below:

'-------------------------------------------------------------------------------------------
'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

解决方案

Because fso.GetFile() will not automatically expand %COMPUTERNAME%, modify sFinalDestinationPath to use sOutputFilename like this:

sOutputFilename = shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
sFinalDestinationPath = "E:\CopyTestFolder\" & sOutputFilename & "\"

这篇关于VBScript环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆