使用object.writeline时未设置vbscript运行时错误对象变量

时间:2014-08-09 03:53:14

标签: vbscript

我正在创建一个脚本,使用vbscript从远程服务器下载更新文件([server_path] \ local.swf)。该脚本将通过Windows命令行运行并接受参数。其中一个参数是new_hash,它包含要下载的文件的哈希值。此哈希将用于与我的pc中已存在的文件的哈希值进行比较(c:\ mcgfiles \ avp \ local.swf)。如果相同,请记录注释并且不执行任何操作。如果没有,则下载local.swf并将其放在临时文件夹c:\ mcgfiles \ avp-temp中。由于vbscript将访问sftp服务器以下载文件,因此我在脚本中做了一些延迟并检查文件是否已下载,然后再继续下一步。如果文件已经存在,我将在同一文件夹c:\ mcgfiles \ avp-temp中创建一个hash.txt,读取它然后与通过脚本传递的哈希进行比较。运行脚本时出错:

使用object.writeline

时未设置vbscript运行时错误对象变量

这是我的剧本:

Option Explicit
Dim Arg, hostname, new_hash, file_version
Dim obj, objLogActivity, objLogStatus, objLogProgram, objCurrHashFile, shellSFTP, WshShell
Dim tmp, datestamp, datestamp2, logCommand, current_hash, comparison, updateAVP, logfile, commandfile, retryLocalSWF
Dim tokenGenHash, retryHashTXT, tokenReadHash, objDownHashFile, downloaded_hash
Set Arg = WScript.Arguments
hostname = Arg(0)
new_hash = Arg(1)
file_version = Arg(2)
tmp=now()
datestamp = DatePart("yyyy",tmp) & "-" & Right("00" & DatePart("m",tmp),2) & "-" & Right("00" & DatePart("d",tmp),2)
datestamp2 = DatePart("yyyy",tmp)&Right("00" & DatePart("m",tmp),2)&Right("00" & DatePart("d",tmp),2)&Right("00" & DatePart("H",tmp),2)&Right("00" & DatePart("n",tmp),2)&Right("00" & DatePart("s",tmp),2)

Set obj = CreateObject("Scripting.FileSystemObject")
Set objLogActivity = obj.CreateTextFile("C:\mcgfiles\logs\activity\activity_"& hostname &"_"&datestamp&".log")
objLogActivity.Close
Set objLogStatus = obj.CreateTextFile("C:\mcgfiles\logs\status\status_"& hostname &"_"&datestamp&".log")
objLogStatus.Close
If obj.FileExists("C:\mcgfiles\logs\program\program_"&hostname&"_"&datestamp&".log") then
    logCommand = 8
Else
    logCommand = 2
    Set objLogProgram = obj.CreateTextFile("C:\mcgfiles\logs\program\program_"&hostname&"_"&datestamp&".log")
    objLogProgram.Close
End If

Set objLogActivity = obj.OpenTextFile("C:\mcgfiles\logs\activity\activity_"& hostname &"_"&datestamp&".log",2)
Set objLogStatus = obj.OpenTextFile("C:\mcgfiles\logs\status\status_"& hostname &"_"&datestamp&".log",2)
Set objLogProgram = obj.OpenTextFile("C:\mcgfiles\logs\program\program_"&hostname&"_"&datestamp&".log",logCommand)

Set objCurrHashFile = obj.OpenTextFile("C:\mcgfiles\avp\hash.txt",1)
current_hash = objCurrHashFile.ReadLine
objCurrHashFile.Close

comparison = StrComp(new_hash, current_hash)
If comparison = 0 Then
    objLogProgram.WriteLine datestamp2&vbTab&Wscript.ScriptName&vbTab&"INFO"&vbTab&"UPDATE STOPPED. AVP FILE IN LOCAL IS UPDATED."
    objLogProgram.Close
    objLogActivity.Write datestamp2&vbTab&Wscript.ScriptName&vbTab&"INFO"&vbTab&"AVP IN LOCAL IS CURRENTLY UPDATED."
    objLogActivity.Close
    objLogStatus.Write "8"&vbTab&datestamp2
    objLogStatus.Close
    updateAVP = False
Else
    logfile = "c:\mcgfiles\logs\program\updateAVPSFTPLogs_"&datestamp&".log"
    commandfile = "c:\scripts\updateAVPSFTPCommands.txt"
    Set shellSFTP=createobject("wscript.shell")
    shellSFTP.run "cmd /k c:\scripts\psftp -i c:\scripts\rsa_id.ppk root@192.168.22.144 -be -b "&commandfile&" >"&logfile,0
    Set shellSFTP = Nothing
    updateAVP = True
End If

If updateAVP = True Then
    objLogProgram.WriteLine datestamp2&vbTab&Wscript.ScriptName&vbTab&"INFO"&vbTab&"OPENING SFTP FOR AVP FILE TRANSFER"
    objLogProgram.Close
    retryLocalSWF = 3
    Do While retryLocalSWF > 0
        If obj.FileExists("C:\mcgfiles\avp-temp\local.swf") Then
            tokenGenHash = True 
            retryLocalSWF = retryLocalSWF - 3
        Else
            Wscript.Sleep 10000
            tokenGenHash = False
            retryLocalSWF = retryLocalSWF - 1           
        End If
    Loop
End If
If tokenGenHash = True Then
    objLogProgram.WriteLine datestamp2&vbTab&Wscript.ScriptName&vbTab&"SUCCESS"&vbTab&"AVP SUCCESSFULLY DOWNLOADED. GENERATING HASH FOR VALIDITY"
    objLogProgram.Close
    Set WshShell= Wscript.CreateObject("Wscript.Shell")
    WshShell.Run "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe $someFilePath = 'C:\mcgfiles\avp-temp\local.swf';$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider;$md5hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($someFilePath)));$newmd5hash = $md5hash -replace '-','';$newmd5hash.ToLower() | Out-File 'C:\mcgfiles\avp-temp\hash.txt' -Encoding Default; return $newmd5hash.ToLower()",0
    retryHashTXT = 0
    Do While retryHashTXT < 3
        If obj.FileExists("C:\mcgfiles\avp-temp\hash.txt") Then
            tokenReadHash = True
            retryHashTXT = retryHashTXT + 3
        Else
            Wscript.Sleep 10000
            tokenReadHash = False
            retryHashTXT = retryHashTXT + 1
        End If
    Loop        
Else
    objLogProgram.WriteLine datestamp2&vbTab&Wscript.ScriptName&vbTab&"ERROR"&vbTab&"AVP WAS NOT TRANSFERRED PROPERLY."
    objLogProgram.Close
    objLogActivity.WriteLine datestamp2&vbTab&Wscript.ScriptName&vbTab&"ERROR"&vbTab&"AVP WAS NOT TRANSFERRED PROPERLY."
    objLogActivity.Close
    objLogStatus.Write "9"&vbTab&datestamp2
    objLogStatus.Close
End If

If tokenReadHash = True Then
    Set objDownHashFile = obj.OpenTextFile("C:\mcgfiles\avp-temp\hash.txt",1)
    downloaded_hash = objDownHashFile.ReadLine
    objDownHashFile.close

End If

提前致谢

1 个答案:

答案 0 :(得分:1)

看起来它可能依赖于参数,但似乎脚本可能在关闭后尝试写入objLogProgram

If comparison = 0 Then
    objLogProgram.WriteLine datestamp2&vbTab&Wscript.ScriptName&vbTab&"INFO"&vbTab&"UPDATE STOPPED. AVP FILE IN LOCAL IS UPDATED."
    objLogProgram.Close

以后

If tokenGenHash = True Then
    objLogProgram.WriteLine datestamp2&vbTab&Wscript.ScriptName&vbTab&"SUCCESS"&vbTab&"AVP SUCCESSFULLY DOWNLOADED. GENERATING HASH FOR VALIDITY"
    objLogProgram.Close
    ' ...
Else
    objLogProgram.WriteLine datestamp2&vbTab&Wscript.ScriptName&vbTab&"ERROR"&vbTab&"AVP WAS NOT TRANSFERRED PROPERLY."
    objLogProgram.Close
    ' ...