asdfbbq Posted September 7, 2012 Share Posted September 7, 2012 Just a slight bit of background: I am calling an AutoIt exe from an AD Group Policy that calls an installer program and does an unattended silent install as my users sign on. My script logs the progress, copies the installer's logs to a central file server, and sends out an email letting me know whether the deployment was successful or not. Works like a dream when you run it as a standalone exe, but when it gets called via GPO it goes nuts. Normally it gets run twice, once by SYSTEM and once by the user. One fails and sends a failure email while the other keeps working in the background. Not too big of a deal until I get the success email from the second instance saying "here is a link to the log file" and I find that the logs have not been generated. Again everything works fine when run as a standalone. I'm sure there are a million things logically wrong with this code, but I haven't coded anything in 10 years and this is my first bout with AutoIt. If you're bored I would love some pointers. expandcollapse popup; ############################################################################## ; #### SCRIPT START #### ; ############################################################################## ; ############################################################################## ; #### Include some files #### ; ############################################################################## #include <INet.au3> #include <File.au3> ; ############################################################################## ; #### Files included #### ; ############################################################################## ; ############################################################################## ; #### Declare variables #### ; ############################################################################## Global $rnd = Random (6000,18000,1) Global $date = @MON & "/" & @MDAY & "/" & @YEAR Global $fdate = @YEAR & "-" & @MON & "-" & @MDAY Global $galaxyLocal = "C:\ProgramData\Commvault Systems\Galaxy\LogFiles\GalaxyInstallerLog.txt" Global $galaxyNew Global $logtime = $fdate & ": == : " & @HOUR & ":" & @MIN & ":" & @SEC & " : == :" Global $logtext[11] Global $basedir = "\\fs.int.<company>.com\public\cvdeploy\log\" Global $logdir = $basedir & @ComputerName Global $logfile = $logdir & "\cvdeploy_" & @ComputerName & ".log" Global $installed Global $xBody[4] Global $xSubj[2] Global $Body Global $Subject Global $loghead[3] Global $logthis Global $logopen = $loghead[0] & $loghead[1] & $loghead[0] Global $logclose = $loghead[0] & $loghead[1] & $loghead[2] & $loghead[0] Global $oMyRet[2] Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") Global $OU = $CmdLineRaw If $OU = "tx2" Then $srv = "\\txwfp02.int.<company>.com" ElseIf $OU = "ca2" Then $srv = "\\cawfp02.int.<company>.com" ElseIf $OU = "hm" Then $srv = "\\fs.int.<company>.com" Else $srv = "\\" & $OU & "wfp01.int.<company>.com" EndIf $logtext[0] = " CVDEPLOY started." $logtext[1] = " No existing install found. Beginning install." $logtext[2] = " Installation folder found. Launching QInstaller to verify installation." $logtext[3] = " Generating Email." $logtext[4] = " Detected 64-bit architecture. Launching 64-bit Install" $logtext[5] = " Detected 32-bit architecture. Launching 32-bit Install" $logtext[6] = " Install Complete!" $logtext[7] = " Install Failed!" $logtext[8] = " Email send failure: " $logtext[9] = " Installation verified. Exiting." $logtext[10] = " GalaxyInstallLog has been copied to: " & $logdir $loghead[0] = "==========================================================" & @CRLF $loghead[1] = " CVDEPLOY log file created on: " & $date & @CRLF $loghead[2] = " END CVDEPLOY LOG FILE " $galaxyNew = $logdir & "\galaxy_" & @ComputerName & ".log" $xBody[0] = "The CommVault installation has been invoked with argument: " & $OU & @CRLF & "By user: " @UserName & @CRLF "From machine: " & @ComputerName & @CRLF $xBody[1] = "Install Success! Please check the log file located at: " & $logfile & " for further information, and verify that this machine exists in the CommCell Console." & @CRLF $xBody[2] = "Install Failure! Please check the log file located at: " & $logfile & " for further information, and verify that this machine exists in the CommCell Console." & @CRLF $xBody[3] = "A copy of the galaxy log can be found at: " & $galaxyNew & "." & @CRLF $xSubj[0] = "Verify Successful CVDEPLOY of: " & @ComputerName $xSubj[1] = "CVDEPLOY Failure on: " & @ComputerName $galaxyNew = $logdir & "\galaxy_" & @ComputerName & ".log" $logopen = $loghead[0] & $loghead[1] & $loghead[0] $logclose = $loghead[0] & $loghead[2] & $loghead[0] $rootPath64 = "\public\cvdeploy\cv_dvd_full\WinX64\QInstaller.exe /play " $playPath64 = "\public\cvdeploy\cv_dvd_full\WinX64\cvinstall.ini" $fullPath64 = $srv & $rootPath64 & $srv & $playPath64 $rootPath32 = "\public\cvdeploy\cv_dvd_full\Win32\QInstaller.exe /play " $playPath32 = "\public\cvdeploy\cv_dvd_full\Win32\cvinstall_32.ini" $fullPath32 = $srv & $rootPath32 & $srv & $playPath32 $SmtpServer = "mnrelay01.int.<company>.com" $FromName = "Deployment Notifications" $FromAddress = "noreply@<company>.com" $ToAddress = "smellyadmin@<company>.com" $IPPort = 25 $ssl = 0 $AttachFiles = "" $CcAddress = "" $BccAddress = "" $Importance = "Normal" $Username = "" $Password = "" ; ############################################################################## ; #### Variables declared #### ; ############################################################################## ; ############################################################################## ; #### Create install function #### ; ############################################################################## Func Install() If @OSArch = "X64" Then $logthis = $logtime & $logtext[4] xlog($logthis) RunWait($fullPath64) Sleep(1000) Else $logthis = $logtime & $logtext[5] xlog($logthis) RunWait($fullPath32) Sleep(1000) EndIf EndFunc ; ############################################################################## ; #### End install function #### ; ############################################################################## ; ############################################################################## ; #### Install check function #### ; ############################################################################## Func instCheck () If FileExists ( "C:\Program Files\CommVault") Then $installed = 1 ProcessClose ( "cvdeploy.exe" ) Else openLog() $logthis = $logtime & $logtext[0] xlog($logthis) $logthis = $logtime & $logtext[1] xlog($logthis) EndIf EndFunc ; ############################################################################## ; #### End install check function #### ; ############################################################################## ; ############################################################################## ; #### Install reCheck function #### ; ############################################################################## Func reCheck () If FileExists ( "C:\Program Files\CommVault") Then $logthis = $logtime & $logtext[6] xlog($logthis) $Subject = $xSubj[0] $Body = $xBody[0] & $xBody[1] & $xbody[3] closeLog() Else $logthis = $logtime & $logtext[7] xlog($logthis) $Subject = $xSubj[1] $Body = $xBody[0] & $xBody[2] closeLog() EndIf EndFunc ; ############################################################################## ; #### End install reCheck function #### ; ############################################################################## ; ############################################################################## ; #### Log write function #### ; ############################################################################## Func xLog ($logthis) FileOpen ( $logfile, 1 ) FileWrite ( $logfile, $logthis & @CRLF ) FileClose ( $logfile ) EndFunc ; ############################################################################## ; #### End log write function #### ; ############################################################################## ; ############################################################################## ; #### Log open function #### ; ############################################################################## Func openLog() $logthis = $logopen xLog($logthis) EndFunc ; ############################################################################## ; #### End log open function #### ; ############################################################################## ; ############################################################################## ; #### Log close function #### ; ############################################################################## Func closeLog() $logthis = @CRLF & $logclose xLog($logthis) EndFunc ; ############################################################################## ; #### End log close function #### ; ############################################################################## ; ############################################################################## ; #### Galaxy insert function #### ; ############################################################################## Func galaxyLog() #RequireAdmin FileCopy ( $galaxyLocal, $galaxyNew ) $logthis = $logtime & $logtext[10] xLog($logthis) EndFunc ; ############################################################################## ; #### End galaxy insert function #### ; ############################################################################## ; ############################################################################## ; #### Email generate function #### ; ############################################################################## Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance="Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0) Local $objEmail = ObjCreate("CDO.Message") $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>' $objEmail.To = $s_ToAddress Local $i_Error = 0 Local $i_Error_desciption = "" If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress $objEmail.Subject = $s_Subject If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then $objEmail.HTMLBody = $as_Body Else $objEmail.Textbody = $as_Body & @CRLF EndIf If $s_AttachFiles <> "" Then Local $S_Files2Attach = StringSplit($s_AttachFiles, ";") For $x = 1 To $S_Files2Attach[0] $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x]) ConsoleWrite('@@ Debug(62) : $S_Files2Attach = ' & $S_Files2Attach & @LF & '>Error code: ' & @error & @LF) ;### Debug Console If FileExists($S_Files2Attach[$x]) Then $objEmail.AddAttachment ($S_Files2Attach[$x]) Else ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF) SetError(1) Return 0 EndIf Next EndIf $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer If Number($IPPort) = 0 then $IPPort = 25 $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort ;Authenticated SMTP If $s_Username <> "" Then $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password EndIf If $ssl Then $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True EndIf ;Update settings $objEmail.Configuration.Fields.Update ; Set Email Importance Switch $s_Importance Case "High" $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "High" Case "Normal" $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Normal" Case "Low" $objEmail.Fields.Item ("urn:schemas:mailheader:Importance") = "Low" EndSwitch $objEmail.Fields.Update ; Sent the Message $objEmail.Send If @error Then SetError(2) Return $oMyRet[1] EndIf $objEmail="" EndFunc ;==>_INetSmtpMailCom ; ############################################################################## ; #### End email generate function #### ; ############################################################################## ; ############################################################################## ; #### Error tracking function #### ; ############################################################################## Func MyErrFunc() $HexNumber = Hex($oMyError.number, 8) $oMyRet[0] = $HexNumber $oMyRet[1] = StringStripWS($oMyError.description, 3) ConsoleWrite("### COM Error ! Number: " & $HexNumber & " ScriptLine: " & $oMyError.scriptline & " Description:" & $oMyRet[1] & @LF) SetError(1); something to check for when this function returns Return EndFunc ; ############################################################################## ; #### End error tracking function #### ; ############################################################################## ; ############################################################################## ; #### Do the work #### ; ############################################################################## Sleep($rnd) If DirGetSize($logdir) = -1 Then DirCreate($logdir) EndIf instCheck() If $installed = 1 Then Exit 0 Else Install () EndIf galaxyLog() reCheck() $rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl) If @error Then $logthis = $logtime & $logtext[8] xLog($logthis & @CRLF & $logtime & "Error sending message", "Error code:" & @error & " Description:" & $rc) EndIf Exit 0 ; ############################################################################## ; #### Work done. Let's go home #### ; ############################################################################## ; ############################################################################## ; #### SCRIPT END #### ; ############################################################################## Feel free to rip it to shreds. It's my first and I don't want to start with bad habits. Link to comment Share on other sites More sharing options...
Mechaflash Posted September 7, 2012 Share Posted September 7, 2012 This washing machine is a front-loader. Spoiler “Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.” Link to comment Share on other sites More sharing options...
BrewManNH Posted September 7, 2012 Share Posted September 7, 2012 The #RequireAdmin may be causing part of the problem, it's going to get invoked and the user won't see any prompts if there are any. Also, if the folder that the log file is being generated on isn't accessible by the user, then the log file probably won't be created. It's hard to say what's causing the issues without actually having it running at the location on a computer there, and with a normal user. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
asdfbbq Posted September 7, 2012 Author Share Posted September 7, 2012 See I thought the same thing, but I have tested this OVER and OVER and sometimes it works, sometimes it doesn't. It doesn't seem like there's any rhyme or reason to it, so I figured I must be going about it in the wrong way. I'd like to do what I normally do and just blame Windows, but my boss didn't really like that answer when I pitched it to him... Link to comment Share on other sites More sharing options...
BrewManNH Posted September 7, 2012 Share Posted September 7, 2012 Where is the connection to the program located in GP? When the user logs in, or when the computer boots up? If it's done when the computer boots up, you can get rid of the #RequireAdmin, make sure everything is accessible to the script when the computer boots up, and run it from the StartUp script location. StartUp scripts/programs already have elevated access levels set, and there's no worries about whether or not the user has the correct rights. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
Colyn1337 Posted September 7, 2012 Share Posted September 7, 2012 (edited) expandcollapse popup#cs ----------------------------------------------------------------------------------------------------------------- Author: Colyn1337 Script Function: This script tests an account's access to various objects (files and folders). This is best used with runas operations to make sure a service account or local account can or cannot access certain objects. The file Objects2Check.txt must be in the same directory of the script and contain the UNC path to the files to check. Non UNC paths can be used, just be sure they're actually valid. Additionally, the routine reports on admin status of the runas user. #ce ----------------------------------------------------------------------------------------------------------------- #include <Array.au3> #include <File.au3> Global Const $Timestamp = @MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " Global Const $Log = @ScriptDir & "AccountInfo.log" Global Const $Objects2Check = @ScriptDir & "Objects2Check.txt" Global $aObjects[1], $aObjectsFound[1], $aObjectsMissing[1] Global $x, $y, $counterA, $counterB ; Get account name ConsoleWrite("My account name is " & @UserName & @LF) FileWriteLine($Log, $Timestamp & "My account name is " & @UserName) If IsAdmin() Then FileWriteLine($Log, $Timestamp & "I have admin.") Else FileWriteLine($Log, $Timestamp & "I don't have admin.") EndIf ; Test rights to objects If FileExists($Objects2Check) Then _FileReadToArray($Objects2Check, $aObjects) For $x = 1 To $aObjects[0] If FileExists($aObjects[$x]) Then ConsoleWrite("FOUND " & $aObjects[$x] & @LF) FileWriteLine($Log, $Timestamp & "FOUND " & $aObjects[$x]) Else ConsoleWrite("MISSING! " & $aObjects[$x] & @LF) FileWriteLine($Log, $Timestamp & "MISSING! " & $aObjects[$x]) EndIf Next Else ConsoleWrite($Objects2Check & " not found!" & @LF) FileWriteLine($Log, $Timestamp & $Objects2Check & " not found!") EndIf Here's a script I use for stuff like this where I need to know what account is running an action, and if it has rights to files/folders... EDIT: Added back in the #includes...... really annoying that the site strips that out when using the autoit code block..... Edited September 7, 2012 by Colyn1337 Link to comment Share on other sites More sharing options...
Blue_Drache Posted September 7, 2012 Share Posted September 7, 2012 Try creating the log file in the user's temp folder and then email it from there. *shrug* See if you can work with the permissions you have. Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache Link to comment Share on other sites More sharing options...
asdfbbq Posted September 7, 2012 Author Share Posted September 7, 2012 had to go with the email attachment option. thanks for the help Link to comment Share on other sites More sharing options...
asdfbbq Posted September 10, 2012 Author Share Posted September 10, 2012 Well I thought this fixed my problem, but it turns out I can't copy the log file I'm writing to because of this error:Description:The process cannot access the file because it is being used by another process.Before I try to attach it I am using FileClose($logfile) but that doesn't work, and I'm not sure what to try next. Any suggestions? Link to comment Share on other sites More sharing options...
Blue_Drache Posted September 10, 2012 Share Posted September 10, 2012 Antivirus may be looking at it after you close it. Suggestions: FileClose it as soon as possible, do a few other cleanup things before trying to attach. Put a short sleep after the close. Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache Link to comment Share on other sites More sharing options...
asdfbbq Posted September 10, 2012 Author Share Posted September 10, 2012 FileClose didn't work, but FileCopy did. Just logged everything as .txt, then copied to .log before attaching. Messy, but it works Link to comment Share on other sites More sharing options...
Colyn1337 Posted September 10, 2012 Share Posted September 10, 2012 FileClose didn't work, but FileCopy did. Just logged everything as .txt, then copied to .log before attaching. Messy, but it worksIf there's an opportunity, see if you can use ProcMon to get a feel for what's grabbing and locking the handle for the file. Assuming your interested in root cause. Link to comment Share on other sites More sharing options...
Blue_Drache Posted September 10, 2012 Share Posted September 10, 2012 FileClose didn't work, but FileCopy did. Just logged everything as .txt, then copied to .log before attaching. Messy, but it worksHey ... a kludge is a kludge. It may not ever be elegant, but it always works, and that's usually what matters. Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now