1

I am creating a script to download an update file ([server_path]\local.swf) from a remote server using vbscript. the script will be run through a windows command line and will accept parameters. one of these parameters is the new_hash, it contains the hash of the file to be downloaded. This hash will be used to compare with hash of the file that is already in my pc (c:\mcgfiles\avp\local.swf). If the same, log comments and do nothing. If not then download the local.swf and place it in a temporary folder c:\mcgfiles\avp-temp. Since the vbscript will be accessing an sftp server to dowload the file, I made some delays in the script and check if the file has been downloaded before proceeding to the next step. If the files is already in place, I will create a hash.txt in the same folder c:\mcgfiles\avp-temp, read it then compare to the hash passed through the script. Error occurs when running the script:

vbscript runtime error object variable not set when using object.writeline

here is my script:

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 [email protected] -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

Thanks in advance

1 Answer 1

1

It looks like it may be dependent on the parameters, but it seems that it is possible for the script to attempt to write to the objLogProgram after it has been closed

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

and later

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
    ' ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.