Jump to content
Sign in to follow this  
Graywalker

Script Locks Up [reopened]

Recommended Posts

Graywalker

Okay, I am having a serious issue. I have a script that goes out and works on all 4,000 + computers in my enterprise. Sometimes, it will get to a certain computer and just sit there.... forever.... doing nothing. (sitting at 85 of 4182 right now)

I've troubleshot a bit and one computer had insufficient hard drive space available. Another, I found that even though I could "Manage" to the computer, I couldn't access the disk management (to check free drive space) - it also sat there "connecting....." forever.

My current script is locking up when attempting to either Drivespacefree(\\computername\c$) or ObjGet("WinNT://" & $strPCName & "/blahblah,Service") ... okay, I know its locking up on the ObjGet, but it may also be locking up on the Drivespacefree.

Is there ANY way to get around this problem that does not involve exiting the script and starting all over? Some way to set a connection timeout on those attempts??

It is not a problem with the script itself - 85 other computers have had no problem whatsoever. I have a custom error handler, so its not even sending back an error. Its just waiting.

Edited by Graywalker

Share this post


Link to post
Share on other sites
Graywalker

Okay, I am having a serious issue. I have a script that goes out and works on all 4,000 + computers in my enterprise. Sometimes, it will get to a certain computer and just sit there.... forever.... doing nothing. (sitting at 85 of 4182 right now)

Tried using psservice.exe (sysinternals) to query a service on the computer and it just sat there too. No error, no quitting, just "connecting" ( I suppose ).

Any ideas on how to get around a situation like this??

Share this post


Link to post
Share on other sites
PsaltyDS

Okay, I am having a serious issue. I have a script that goes out and works on all 4,000 + computers in my enterprise. Sometimes, it will get to a certain computer and just sit there.... forever.... doing nothing. (sitting at 85 of 4182 right now)

I've troubleshot a bit and one computer had insufficient hard drive space available. Another, I found that even though I could "Manage" to the computer, I couldn't access the disk management (to check free drive space) - it also sat there "connecting....." forever.

My current script is locking up when attempting to either Drivespacefree(\\computername\c$) or ObjGet("WinNT://" & $strPCName & "/blahblah,Service") ... okay, I know its locking up on the ObjGet, but it may also be locking up on the Drivespacefree.

Is there ANY way to get around this problem that does not involve exiting the script and starting all over? Some way to set a connection timeout on those attempts??

It is not a problem with the script itself - 85 other computers have had no problem whatsoever. I have a custom error handler, so its not even sending back an error. Its just waiting.

Post the actual code that does the DriveSpaceFree() query. I get 0 returned in a few milliseconds for an invalid name. I'm not sure that function works on remote admin shares, either.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Graywalker

Post the actual code that does the DriveSpaceFree() query. I get 0 returned in a few milliseconds for an invalid name. I'm not sure that function works on remote admin shares, either.

:)

ProgressSet($i, $pcc & " of " & $RecordCount & " - Checking for Running *** Services", "Working on " & $strPCName)
    $drivespace = DriveSpaceFree("\\" & $strPCName & "\c$")
    If $drivespace > 100 Then
    $qTunerService = ObjGet("WinNT://" & $strPCName & "/ANYSERVICE,Service")
    $sTunerService = IsObj($qTunerService)

85 machines process just fine before reaching this particular remote machine. I've had it where 189 machines process just fine. (edit) The machine name is Valid and responds to a ping.

PSService hangs when doing a service query on this particular remote machine. When using computer management to "manage" this particular remote machine, it connects fine, displays fine... but if you try to connect to "Services" or "Disk Management", MMC hangs too.

I am getting output like : $drivespace = 84.1796875

Again, I'm pretty sure it is nothing wrong in the code. There is something wrong on the remote computer that is causing WMI/MMC/COM requests to hang. My question is - how can I code to get around this computer (and similar)??

Edited by Graywalker

Share this post


Link to post
Share on other sites
Graywalker

Okay, I am having a serious issue. I have a script that goes out and works on all 4,000 + computers in my enterprise. Sometimes, it will get to a certain computer and just sit there.... forever.... doing nothing. (sitting at 85 of 4182 right now)

Thanks to mcarriere893 on MyItForum, I think I found the Solution.

Problem was, WMI was getting trapped in a connection attempt, the remote WMI shook hands, but wouldn't let go. So, not really wanting to re-work all of my script to use the new method, I simply used it to make a Check to see if WMI was available and behaving properly.

$Locator = ObjCreate("WbemScripting.SWbemLocator")
    $Service = $Locator.ConnectServer($strPCName,"root/cimv2","","","","",128)
    
    If IsObj($Service) Then
$drivespace = DriveSpaceFree("\\" & $strPCName & "\c$")
    
    $qTunerService = ObjGet("WinNT://" & $strPCName & "/TMKDefault,Service")
    $sTunerService = IsObj($qTunerService)

If you're having the same issue, put that code before you do anything requiring WMI connections. The 128 on the end there tells it to only wait 2min. for a successful connection, ergo, no waiting forever!! :)

( more info HERE )

BUT.... I got to 85 again... and it connected... and then SITS THERE FOR FREAKIN EVER!!!!!!!!! AAAAAAAAARRRRRRRRRGGGGGHHHHH!!!

Share this post


Link to post
Share on other sites
Graywalker

$Locator = ObjCreate("WbemScripting.SWbemLocator")
    $Service = $Locator.ConnectServer($strPCName,"root/cimv2","","","","",128)
    
    If IsObj($Service) Then 
;$drivespace = DriveSpaceFree("\\" & $strPCName & "\c$")
    
    $qTunerService = $Service.get("Win32_Service.Name='MyServiceName'")
    $sTunerService = IsObj($qTunerService)

Changed it all up... and Yep, still hangs.

Any ideas? Or am I just talking to myself (and documenting the whole process?)

Edited by Graywalker

Share this post


Link to post
Share on other sites
Graywalker

Okay, Trying This :

Global $psservice = @ComSpec & ' /c "' & @ScriptDir & '\psservice.exe"'

ProgressSet($i, $pcc & " of " & $RecordCount & " - Checking for Running BMC Services", "Working on " & $strPCName)
        
        $Locator = ObjCreate("WbemScripting.SWbemLocator")
        $Service = $Locator.ConnectServer($strPCName, "root/cimv2", "", "", "", "", 128)
        
        
        If IsObj($Service) Then
; Test to see if the WMI Service is behaving
            $psscmd = " \\" & $strPCName & " query winmgmt"
            $WMITest = Run($psservice & $psscmd, "", @SW_MINIMIZE)
            $pss_check = ProcessWaitClose($WMITest, 128)
            If $pss_check = 1 Then
                
                $drivespace = DriveSpaceFree("\\" & $strPCName & "\c$")
                
                $qTService = $Service.get("Win32_Service.Name='MYSERVICE'")
                $sTService = IsObj($qTService)
                                If $sTService = "1" Then
                    $sTService = "Service Running"
                     EndIf
                        Else
                ProcessClose($WMITest)
                $sTService = "WMI Locked Up"
            EndIf
        Else
            $sTService = "WMI Unavailable"
        EndIf

Theory : Okay, the WMI is responding. Good. Still may lock up on a query. Well, lets use an external process to check that. PSService!!

Launch PSService - using RUN so the whole script doesn't wait for it to finish, RUNWAIT doesn't have a timeout. So, get PSService to check on WMI service - winmgmt. Use ProcessWaitClose to wait for PSService to finish - but only give it 128 seconds to get done before realizing that its lockedup. And, if it IS locked up, we can politely close PSService so it's all freed up for the next computer.

Yeah, its a bit much code... but if you are dealing with 4000+ computers and it hangs up on number 2583, man... frustration. :)

So, right now, I am testing this script - going through 4185 computers pulling tons of info into one place. I'll be happy if it gets past 85 and then 186 (the two that have caused problems, but I'm pretty sure I've had those rebooted, which clears the issue for a while) and will Edit here on whether or not it actually works.

stay tuned....

{EDIT} Its past 85... past 131 ... past 200... and have encountered at least one computer whose WMI was locked up - and PASSED it!! WOOHOO!! Success. Messy, but it gets the script done!

Edited by Graywalker

Share this post


Link to post
Share on other sites
Graywalker

Global $psservice = @ComSpec & ' /c "' & @ScriptDir & '\psservice.exe"'

ProgressSet($i, $pcc & " of " & $RecordCount & " - Checking for Running BMC Services", "Working on " & $strPCName)
        
        $Locator = ObjCreate("WbemScripting.SWbemLocator")
        $Service = $Locator.ConnectServer($strPCName, "root/cimv2", "", "", "", "", 128)
That was not working consistently. In other words, I still got hangs, but nowhere near as often.

I found iSecurityFlags = "&H80" over here

So, code would be

Global $psservice = @ComSpec & ' /c "' & @ScriptDir & '\psservice.exe"'

ProgressSet($i, $pcc & " of " & $RecordCount & " - Checking for Running BMC Services", "Working on " & $strPCName)
        $iSecurityFlags = "&H80"
        $Locator = ObjCreate("WbemScripting.SWbemLocator")
        $Service = $Locator.ConnectServer($strPCName, "root/cimv2", "", "", "", "", $iSecurityFlags)

$Locator = ObjCreate("WbemScripting.SWbemLocator")
Dim $iSecurityFlags = 128
$Service = $Locator.ConnectServer($strPCName, "root/cimv2", "", "", "", "", $iSecurityFlags)

also works - and I've used it with good results so far.

Share this post


Link to post
Share on other sites
Graywalker

Well, its not working. Its better, but I just verified with trace lines that the script hangs on :

$Service = $Locator.ConnectServer($strPCName, "root/cimv2", "", "", "", "", $iSecurityFlags)

(its either that line or "If IsObj($Service) Then")

and will just sit there forever - even though it is supposed to time out.

Here is a code snippit to test with - granted, the actual script (619 lines with an additional 6 includes) connects to active directory and goes through 500 to 700+ computers, pulling info from AD, WMI and direct file reads, before hanging on one. Its not usually the same system - I just verified the last run it zoomed past the computer name it hung up on this time.

$strPCName = InputBox("PC Name","Input Computer Name")
        $Locator = ObjCreate("WbemScripting.SWbemLocator")
    Dim $iSecurityFlags = 128 ; "wbemConnectFlagUseMaxWait" or 128 or &H080
        $Service = $Locator.ConnectServer($strPCName, "root/cimv2", "", "", "", "", $iSecurityFlags)
    If IsObj($Service) Then
        ; Test to see if the WMI Service is behaving
        $psscmd = " \\" & $strPCName & " query winmgmt"
        $WMITest = Run($psservice & $psscmd, "", @SW_HIDE)
        $pss_check = ProcessWaitClose($WMITest, 128)
        If $pss_check = 1 Then
            Msgbox(0,"WMI Test", $strPCName & " has responsive WMI.")
                Else
                        Msgbox(0,"WMI Test", $strPCName & " has broken WMI.")
                Endif
        Else
                Msgbox(0,"WMI Test", $strPCName & " - can not connect."
        Endif

My trace report got :

Starting (Some Server Name) pc 574
Starting (Some Server Name) at 5/3/2011 3:42:22 PM
Line 299 $pingresult = Run
  Ping Completed - 1123.9115747736 milliseconds
Line 338 $Service = $Locator.ConnectServer

and I saw this on 5/4/2011 at 8:20 AM

With my Trace lines :

$Locator = ObjCreate("WbemScripting.SWbemLocator")
    FileWriteLine($trace, "Line " & (@ScriptLineNumber + 2) & " $Service = $Locator.ConnectServer" & @CRLF)
    FileFlush($trace)
    $Service = $Locator.ConnectServer($strPCName, "root/cimv2", "", "", "", "", $iSecurityFlags)
    If IsObj($Service) Then
        ; Test to see if the WMI Service is behaving
        $psscmd = " \\" & $strPCName & " query winmgmt"
        FileWriteLine($trace, "Line " & (@ScriptLineNumber + 2) & " $WMITest = Run($psservice & $psscmd" & @CRLF)
        FileFlush($trace)
        $WMITest = Run($psservice & $psscmd, "", @SW_HIDE)
        $pss_check = ProcessWaitClose($WMITest, 128)
        If $pss_check = 1 Then

Includes in the big script :

#include "String.au3"

#include "ADFunctions.au3"

#include "ServiceControl.au3"

#include "array.au3"

#include "Date.au3"

#include "Constants.au3"

Share this post


Link to post
Share on other sites
Graywalker

http://msdn.microsoft.com/en-us/library/aa393720(v=vs.85).aspx is not much help

I found : http://www.adras.com/Check-WMI-Connectivity.t3372-52.html

Which suggests that using

Dim $iSecurityFlags = "&H80" ; "wbemConnectFlagUseMaxWait" or 128 or &H80 not &H080

Works. "&H80" instead of "&H080" - so am trying this. No errors so far (26 systems)

Edited by Graywalker

Share this post


Link to post
Share on other sites
UEZ

Thanks for the hint! I thought &H80 is 0x80 which I used but still hangs on some system where the WMI service is not running properly (restart of the service helps most time).

I will try it with "&H80" now.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
Graywalker

Apparently I was wrong and that is not my issue. The script is just randomly hanging at any and all locations.

the report from :

Opt("TrayIconDebug", 1) ;0=no info, 1=debug line info

does not agree with what I am getting from a trace report txt file - the txt file shows the script is way past where the debug tray info shows.

I am at a complete loss... no correlation as to effect and cause... hangs on random computers, after random number of systems - from 22 to 1575 - and random lines in the script.

My only thought now is that I am using up resources or handles???

I replaced the $locator with :

$psscmd = " \\" & $strPCName & " query winmgmt"
        
        $WMITest = Run(@ComSpec & " /c sc" & $psscmd, "", @SW_HIDE, $STDOUT_CHILD)
        $pt = TimerInit()
        While 1
            $line = StdoutRead($WMITest)
            If @error Then ExitLoop
            $ptd = TimerDiff($pt)
            If $ptd > 50000 Then
                ProcessClose($WMITest)
                ExitLoop
            EndIf
        
            If $line <> "" Then
                $wmires = $wmires & $line & @CRLF
            EndIf
        WEnd
        StdioClose($WMITest)
        $pss_check = StringInStr($wmires, "Running")

Just to be sure that it was not causing the hanging... Its just gotten worse.

So - question is, does using the Run and $STDOUT_CHILD three times on each computer, with hundreds of computers, max out some resources and hang the script? If so, how to release those after each computer? Or something else causing the hanging?

Full code :

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=C:\Users\ME\Pictures\computer_network.ico
#AutoIt3Wrapper_outfile=AssetCollect.exe
#AutoIt3Wrapper_UseX64=n
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include "String.au3"
#include "ADFunctions.au3"
#include "ServiceControl.au3"
#include "array.au3"
#include "Date.au3"
#include "Constants.au3"
;OnAutoItExitRegister("ExitReport")
;Opt("TrayAutoPause", 0) ;0=no pause, 1=Pause
;Opt("TrayIconDebug", 1) ;0=no info, 1=debug line info
;Opt("TrayIconHide", 0) ;0=show, 1=hide tray icon
FileInstall("psservice.exe", @ScriptDir & "\psservice.exe")
FileInstall("sc.exe", @ScriptDir & "\sc.exe")
FileInstall("NoBMC.exe", @ScriptDir & "\NoBMC.exe")
FileInstall("NoTrend.exe", @ScriptDir & "\NoTrend.exe")
;----- Customize the following constants for your needs -----
Const $statusfilen = "status" & @MON & @MDAY & @YEAR & ".txt"
Const $ResultsFile = "AssetCollect" & @MON & @MDAY & @YEAR & ".csv"
Const $ScriptVersion = 1.0
Global $g_eventerror = 0 ; to be checked to know if com error occurs. Must be reset after handling.
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Install a custom error handler
Global $psservice = @ComSpec & ' /c "' & @ScriptDir & '\psservice.exe" -accepteula'
Global $pslist = @ComSpec & ' /c "' & @ScriptDir & '\pslist.exe" -accepteula'
Global $recheck = ""
;------ Constant Declarantions ----------
Const $ForReading = 0
Const $ForWriting = 2
Const $ForAppending = 1
Const $LDAP_Suffix = "DC=WORKPLACE,DC=local"
Const $adOpenStatic = 3
Const $adLockOptimistic = 3
Const $adCmdText = "&H0001"
Const $ADS_SCOPE_SUBTREE = 2
If Not FileExists("NoBMC.txt") Then
    Global $nobmcfile = FileOpen("NoBMC.txt", 2)
    Global $notrendfile = FileOpen("NoTrend.txt", 2)
Else
    Global $nobmcfile = FileOpen("NoBMC2.txt", 2)
    Global $notrendfile = FileOpen("NoTrend2.txt", 2)
EndIf
;     Constants for the NameTranslate function.
Const $ADS_NAME_INITTYPE_GC = 3
Const $ADS_NAME_TYPE_NT4 = 3
Const $ADS_NAME_TYPE_1779 = 1
; Dim the Variables used
Global $objAD_Recordset, $strDN, $strPCName, $objPC, $RecordCount, $pcc, $objAD_Command, $a_PCNames
Dim $i = 0
Global $ErrorFile, $timenow, $start, $statusfile
Global $ExistingService, $sTunerService, $TunerPath
Global $servicenames[18]
; Set Service Names
$servicenames[0] = "BMCservice1"
$servicenames[1] = "BMCservice2"
$servicenames[2] = "BMCservice3"
$servicenames[3] = "BMCservice4"
$servicenames[4] = "BMCservice5"
$servicenames[5] = "BMCservice6"
$servicenames[6] = "BMCservice7"
$servicenames[7] = "BMCservice8"
$servicenames[8] = "BMCservice9"
$servicenames[9] = "BMCservice10"
$servicenames[10] = "BMCservice11"
$servicenames[11] = "BMCservice12"
$servicenames[12] = "BMCserviceLaptop"
$servicenames[13] = "BMCserviceSrVR"
$servicenames[14] = "BMCservicePkg"
$servicenames[15] = "BMCservice_end"
$servicenames[16] = "BMCservice_Main"
$servicenames[17] = "Test"
; Establish Log File to write results to
$LogFile = FileOpen($ResultsFile, $ForWriting)
If @error = -1 Then
    MsgBox(0, "Error", "ERROR: Unable to initialize requested log file, " & $ResultsFile & ".")
    FileClose($LogFile)
    Exit
EndIf ;Err.Number <> 0
$ErrorFile = FileOpen("ErrorFile" & @MON & @MDAY & @YEAR & ".csv", $ForWriting)
If @error = -1 Then
    MsgBox(0, "Error", "ERROR: Unable to initialize requested Error log file.")
    FileClose($ErrorFile)
    Exit
EndIf
; Set the Status file
If FileExists($statusfilen) Then
    FileCopy($statusfilen, "OldStatusFile.txt")
    FileDelete($statusfilen)
EndIf
$statusfile = FileOpen($statusfilen, $ForWriting)
; Set start time in status file
$now = _Now()
;$status = FileOpen($statusfile, $ForWriting)
FileWriteLine($statusfile, "Started Collect at : " & $now)
;FileClose($status)
; Prepare Active Directory and Query
Global $objConnection = ObjCreate("ADODB.Connection"); Create COM object to AD
$objConnection.ConnectionString = "Provider=ADsDSOObject"
$objConnection.Open("Active Directory Provider"); Open connection to AD
$objAD_Command = ObjCreate("ADODB.Command")
$objAD_Command.ActiveConnection = $objConnection
$objAD_Command.CommandText = "SELECT * FROM 'LDAP://DC=WORKPLACE,DC=local' WHERE objectClass='computer' AND 'userAccountControl:1.2.840.113556.1.4.803:'<>2"
$objAD_Command.Properties("Page Size") = 1000
$objAD_Command.Properties("Searchscope") = 2
$objAD_Recordset = $objAD_Command.Execute
$RecordCount = $objAD_Recordset.recordcount
; Set Results File Headers
FileWriteLine($LogFile, "Computer Name, Ping?, OS, OS SP, OS Version, Date Changed (GMT), Date Created (GMT), Password Changed, IP Subnet, Tuner File?, Tuner Installed?, Tuner Service?, Trend?, TrendServer, GoverLan?, Free C: MB, StartBMC?")
;$status = FileOpen($statusfile, $ForAppending)
FileWriteLine($statusfile, "Working with " & $RecordCount & " computers.")
;FileClose($status)
Do
    ; Set trace file
    ;$trace = FileOpen(@ScriptDir & "\tracereport.txt", 2)
    $strDN = $objAD_Recordset.Fields(0).value
    $objPC = ObjGet($strDN)
    If IsObj($objPC) Then
        $strPCName = ($objPC.cn)
        $pcc = $pcc + 1
        ;FileWriteLine($trace, 'Starting ' & $strPCName & " pc " & $pcc & @CRLF & @CRLF) ;### Trace Console
        InfoCollect($strPCName, $objPC, $pcc)
    Else
        FileWriteLine($ErrorFile, "Unable to get Object  (line 130) on PC " & $pcc & ", " & $strDN)
        FileWriteLine($LogFile, $strDN & ",Not an Object")
        $recheck = $recheck & "!" & $strDN
        $pcc = $pcc + 1
    EndIf
    $objAD_Recordset.MoveNext()
    ; Set trace file
    ;FileClose($trace)
Until $objAD_Recordset.EOF()
FileClose($nobmcfile)
FileClose($notrendfile)
; Kick off BMC Install and Trend Repair
Run("NoBMC.exe", @ScriptDir)
Run("NoTrend.exe", @ScriptDir)
; re-check unavailable computers
; Set complete time for first run
$now = _Now()
FileWriteLine($statusfile, "Completed & Started Re-Check at : " & $now)
;FileClose($status)
$pcc = 0
; make sure time is during working hours to capture laptops missed by midnight launch
Do
    $timenow = @HOUR & @MIN
    If $timenow > "0830" And $timenow < "1400" Then
        $start = "YES"
        ExitLoop
    Else
        $start = "No"
        Sleep(1500000)
    EndIf
Until $start = "YES"
; put entry into status file
$now = _Now()
FileWriteLine($statusfile, "Re-check begins working at : " & $now)
If Not FileExists("NoBMC.txt") Then
    Global $nobmcfile = FileOpen("NoBMC.txt", 2)
    Global $notrendfile = FileOpen("NoTrend.txt", 2)
Else
    Global $nobmcfile = FileOpen("NoBMC2.txt", 2)
    Global $notrendfile = FileOpen("NoTrend2.txt", 2)
EndIf
$a_PCNames = StringSplit($recheck, "!")
If Not IsArray($a_PCNames) Then
    $a_PCNames[0] = 1
    $a_PCNames[1] = $recheck
EndIf
$RecordCount = $a_PCNames[0]
$now = _Now()
FileWriteLine($statusfile, "Re-checking " & $RecordCount & " computers, starting : " & $now)
;FileClose($status)
Do
    ; Set trace file
    ;$trace = FileOpen(@ScriptDir & "\tracereport.txt", 2)
    ;FileWriteLine($trace, 'Recheck Starting ' & $strPCName & " pc " & $pcc & @CRLF & @CRLF) ;### Trace Console
    $q_eventerror = 0
    $pcc = $pcc + 1
    $strDN = "LDAP://" & $a_PCNames[$pcc]
    $objPC = ObjGet($strDN)
    If IsObj($objPC) Then
        $strPCName = ($objPC.cn)
        InfoCollect($strPCName, $objPC, $pcc)
    Else
        FileWriteLine($ErrorFile, "Unable to get Object  (line 182) on PC " & $pcc & ", " & $strDN)
        FileWriteLine($LogFile, $strDN & ",Not an Object")
    EndIf
    ; close trace file
    ;FileClose($trace)
Until $pcc = $RecordCount
ResultsCleanup()
; Set complete time for full run
$now = _Now()
FileWriteLine($statusfile, "Completed : " & $now)
FileClose($statusfile)
FileClose($LogFile)
FileClose($ErrorFile)
FileClose($nobmcfile)
FileClose($notrendfile)
; Kick off BMC Install and Trend Repair again
If ProcessExists("NoBMC") Then
    While ProcessExists("NoBMC")
        Sleep(3000)
    WEnd
Else
    Run("NoBMC.exe", @ScriptDir)
EndIf
If ProcessExists("NoTrend") Then
    While ProcessExists("NoTrend")
        Sleep(3000)
    WEnd
Else
    Run("NoTrend.exe", @ScriptDir)
EndIf
Exit
; ********* Functions that do the actual working **********
Func InfoCollect($strPCName, $objPC, $pcc)
    Dim $strOS, $strOSsp, $strOSv, $strChange, $strCreate, $sTempFile, $IsAlive
    Dim $stripIP, $arrIP, $strSubnet, $pingresult, $sStart, $sEnd, $sString, $strADGROUP
    Dim $cDay, $cHour, $cMin, $cMonth, $cYear, $crDay, $crYear, $crHour, $crMin, $crMonth, $netwrkAddr, $pt, $ptd
    Dim $strGroup, $Group, $Groups, $s, $sTrendService, $qTrendService, $qTunerService, $lastlogon, $thegroup, $wmires = ""
    Dim $arrGroups, $fqdn, $memberof, $strTunerPathC, $strTunerPathD, $strTunerPathE, $strPwdCh, $strPwdChd, $exitcode
    Dim $IPSubkey, $IPkey, $keyread = "No", $r = 0, $drivespace, $Locator, $Service, $TrendServer, $hundreds, $startbmc
    Dim $ABCDService, $ABCSAService, $ABCRpService, $ABCLaptop, $ApplicationPkgr, $EndNode, $JacksonMain, $Test, $stanum, $ABCDRepeater
    Dim $ABCDay, $ABCMirror, $ABCATM, $sTunerServicei, $WMITest, $psscmd, $pss_check, $sGoverLan, $qGoverLan, $ABC_Server
    Dim $services = "\HKLM\SYSTEM\CurrentControlSet\Services"
    Dim $iSecurityFlags = "&H80" ; "wbemConnectFlagUseMaxWait" or 128 or &H80
    Global $trendkey = "\\" & $strPCName & "\HKLM\SOFTWARE\TrendMicro"
    $qTunerService = 0
    $qGoverLan = 0
    $qTrendService = 0
    $sTunerService = ""
    $sTrendService = "X"
    $sGoverLan = "X"
    $TrendServer = "X"
    $IsAlive = "X"
    $strOS = "X"
    $strOSsp = "X"
    $strOSv = "X"
    $strChanged = "X"
    $strCreated = "X"
    $strPwdChd = "X"
    $strSubnet = "X"
    $TunerPath = "X"
    $sTunerServicei = "X"
    $drivespace = "X"
    $startbmc = "X"
    $keyread = "X"
    $stanum = $pcc / 100
    If IsInt($stanum) Then
        $now = _Now()
        FileWriteLine($statusfile, "   " & $pcc & " at : " & $now)
        FileFlush($LogFile)
        FileFlush($statusfile)
    EndIf
    ;FileWriteLine($trace, "Starting " & $strPCName & " at " & $now)
    ;;FileFlush($trace)
    ;MsgBox(0, "PC Name", $strPCName)
    $fqdn = $objPC.distinguishedName
    $strOS = $objPC.operatingSystem
    $strOSsp = $objPC.operatingSystemServicePack
    $strOSv = $objPC.operatingSystemVersion
    $strChange = $objPC.whenChanged
    $strCreate = $objPC.whenCreated
    $strPwdCh = $objPC.passwordLastChanged
    ; check for stale workstation or BMC only AD Account
    If StringInStr($fqdn, "stale") Then
        $IsAlive = $IsAlive & "STALE "
    ElseIf StringInStr($fqdn, "BMC CM") Then
        $IsAlive = $IsAlive & "BMC AD "
    EndIf
    ;   Convert the whenChanged date to something useable by Excel
    $cYear = StringLeft($strChange, 4)
    $cMonth = StringMid($strChange, 5, 2)
    $cDay = StringMid($strChange, 7, 2)
    $cHour = StringMid($strChange, 9, 2)
    $cMin = StringMid($strChange, 11, 2)
    $strChanged = $cMonth & "/" & $cDay & "/" & $cYear & " " & $cHour & ":" & $cMin
    ;   Convert the whenCreated date to something useable by Excel
    $crYear = StringLeft($strCreate, 4)
    $crMonth = StringMid($strCreate, 5, 2)
    $crDay = StringMid($strCreate, 7, 2)
    $crHour = StringMid($strCreate, 9, 2)
    $crMin = StringMid($strCreate, 11, 2)
    $strCreated = $crMonth & "/" & $crDay & "/" & $crYear & " " & $crHour & ":" & $crMin
    ;   Convert the pwdLastSet to something useable by Excel
    $crYear = StringLeft($strPwdCh, 4)
    $crMonth = StringMid($strPwdCh, 5, 2)
    $crDay = StringMid($strPwdCh, 7, 2)
    $crHour = StringMid($strPwdCh, 9, 2)
    $crMin = StringMid($strPwdCh, 11, 2)
    $strPwdChd = $crMonth & "/" & $crDay & "/" & $crYear & " " & $crHour & ":" & $crMin
    ;   Ping the computer and store results in a temp file
    Local $pingresult, $ping
    $ping = ""
    ;FileWriteLine($trace, "Line " & (@ScriptLineNumber + 2) & " $pingresult = Run" & @CRLF)
    ;;FileFlush($trace)
    $pingresult = Run(@ComSpec & " /c " & 'ping -n 2 ' & $strPCName, "", @SW_HIDE, $STDOUT_CHILD)
    $pt = TimerInit()
    While 1
        $line = StdoutRead($pingresult)
        If @error Then ExitLoop
        $ptd = TimerDiff($pt)
        If $ptd > 50000 Then
            ProcessClose($pingresult)
            ExitLoop
        EndIf
        ;MsgBox(0,"line",$line)
        If $line <> "" Then
            $ping = $ping & $line & @CRLF
        EndIf
    WEnd
    StdioClose($pingresult)
    ;   Check to see if there was a Time to Live on the ping, meaning the pc responded.
    ;MsgBox(0, "Ping", $ping)
    $TTL = StringInStr($ping, "TTL=")
    Select
        Case $TTL > 0
            $IsAlive = "Responding"
        Case Else
            $IsAlive = "Not Responding to Ping"
    EndSelect
    ;FileWriteLine($trace, "  Ping Completed - " & $ptd & " milliseconds, " & $IsAlive)
    ;;FileFlush($trace)
    $sString = $ping
    $sStart = "from "
    $sEnd = ": "
    $stripIPa = _StringBetween($sString, $sStart, $sEnd)
    If IsArray($stripIPa) Then
        $stripIP = $stripIPa[0]
    Else
        $stripIP = @error
    EndIf
    ;$Locator = ObjCreate("WbemScripting.SWbemLocator")
    ;;FileWriteLine($trace, "Line " & (@ScriptLineNumber + 2) & " $Service = $Locator.ConnectServer" & @CRLF)
    ;;;FileFlush($trace)
    ;$Service = $Locator.ConnectServer($strPCName, "root/CIMV2", "", "", "", "", $iSecurityFlags) ; strserver,namespace,user,password,locale,authority,isecurityflags,namedvalueset
    ;If IsObj($Service) Then
    If $IsAlive = "Responding" Then
        ;FileWriteLine($trace, "Line " & (@ScriptLineNumber + 2) & ' $psscmd = " \\" & $strPCName & " query winmgmt"' & @CRLF)
        ;;FileFlush($trace)
        ; Test to see if the WMI Service is behaving
        $psscmd = " \\" & $strPCName & " query winmgmt"
        ;FileWriteLine($trace, "Line " & (@ScriptLineNumber + 2) & " $WMITest = Run(@ComSpec & ' /c sc' & $psscmd, "", @SW_HIDE, $STDOUT_CHILD)" & @CRLF)
        ;;FileFlush($trace)
        $WMITest = Run(@ComSpec & " /c sc" & $psscmd, "", @SW_HIDE, $STDOUT_CHILD)
        ;$pss_check = ProcessWaitClose($WMITest, 32)
        ;$exitcode = @extended
        $pt = TimerInit()
        While 1
            $line = StdoutRead($WMITest)
            If @error Then ExitLoop
            $ptd = TimerDiff($pt)
            If $ptd > 50000 Then
                ProcessClose($WMITest)
                ExitLoop
            EndIf
            ;MsgBox(0,"line",$line)
            If $line <> "" Then
                $wmires = $wmires & $line & @CRLF
            EndIf
        WEnd
        StdioClose($WMITest)
        $pss_check = StringInStr($wmires, "Running")
        ;FileWriteLine($trace, "Line " & (@ScriptLineNumber + - 2) & " $pss_check : " & $pss_check & " exit code : " & @extended & @CRLF)
        ;;FileFlush($trace)
        If $pss_check > 1 Then
            ;   IF ping succeeded, pull the IP address from the ping response file *otherwise* Try to pull from the Registry if Ping Failed
            If $IsAlive = "Responding" Then
                ; all is good
            Else
                $keyread = "working"
                While $keyread = "working"
                    $r = $r + 1
                    ;FileWriteLine($trace, "Line " & (@ScriptLineNumber + 2) & " $IPSubkey = RegEnumKey(" & @CRLF)
                    ;;FileFlush($trace)
                    $IPSubkey = RegEnumKey("\\" & $strPCName & "\HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters\Interfaces\", $r)
                    If @error <> 0 Then
                        Select
                            Case @error = -1
                                $keyread = "outofrange"
                            Case @error = 3
                                $keyread = "Unable to connect to Registry"
                            Case @error = 2
                                $keyread = "No such Key"
                            Case @error = 1
                                $keyread = "No Subkey Found"
                        EndSelect
                    EndIf
                    SetError(0)
                    $IPkey = RegRead("\\" & $strPCName & "\HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters\Interfaces\" & $IPSubkey, "DhcpIPAddress")
                    If $IPkey <> "" Then
                        $keyread = "GotIt"
                    EndIf
                    SetError(0)
                WEnd
                If $IPkey = "" Then
                    $IPkey = "Can Not.Acquire IP." & $keyread & ".Error"
                EndIf
            EndIf
            If $keyread = "GotIt" Or StringInStr($stripIP, ".") Then
                ;   Take either method's IP address and pull out the subnet only
                ;MsgBox(0, "StringBetween", $stripIP[0])
                $isip = StringInStr($stripIP, ".")
                If $isip > 0 Then
                    $arrIP = StringSplit($stripIP, ".")
                    ;MsgBox(0, "Array size for IP", $arrIP[0])
                    $strSubnet = $arrIP[1] & "." & $arrIP[2] & "." & $arrIP[3]
                Else
                    If StringInStr($IPkey, ".") Then
                        $arrIP = StringSplit($IPkey, ".")
                        If $arrIP[0] > 3 Then
                            $strSubnet = $arrIP[1] & "." & $arrIP[2] & "." & $arrIP[3]
                        Else
                            $strSubnet = $IPkey
                        EndIf
                    Else
                        $strSubnet = $IPkey
                    EndIf
                EndIf
                ;   Check for the BMC Tuner File
                Select
                    Case FileExists("\\" & $strPCName & "\C$\Program Files\marimba\tuner\tuner.exe")
                        $TunerPath = "C Drive"
                    Case FileExists("\\" & $strPCName & "\E$\Program Files\marimba\tuner\tuner.exe")
                        $TunerPath = "E Drive"
                    Case FileExists("\\" & $strPCName & "\D$\Program Files\marimba\tuner\tuner.exe")
                        $TunerPath = "D Drive"
                    Case FileExists("\\" & $strPCName & "\C$\Program Files (x86)\marimba\tuner\tuner.exe")
                        $TunerPath = "C Drive 64"
                    Case FileExists("\\" & $strPCName & "\E$\program files\BMC Software\BBCA\Tuner\tuner.exe")
                        $TunerPath = "E BBCA"
                    Case FileExists("\\" & $strPCName & "\D$\program files\BMC Software\BBCA\Tuner\tuner.exe")
                        $TunerPath = "D BBCA"
                    Case FileExists("\\" & $strPCName & "\U$\program files\marimba\tuner\tuner.exe")
                        $TunerPath = "U Drive"
                    Case FileExists("\\" & $strPCName & "\V$\program files\BMC Software\BBCA\Tuner\tuner.exe")
                        $TunerPath = "V BBCA"
                    Case FileExists("\\" & $strPCName & "\V$\program files\marimba\tuner\tuner.exe")
                        $TunerPath = "V Drive"
                    Case Else
                        $TunerPath = "Z_No Tuner File"
                        If Not StringInStr($strOS, "Server") And Not StringInStr($strOS, "NT") Then
                            FileWriteLine($nobmcfile, $strPCName)
                        EndIf
                EndSelect
                ;   Check for the BMC Tuner Service
                $sTunerServicei = ""
                ;FileWriteLine($trace, "Line " & (@ScriptLineNumber + 2) & " For $Tservice In $servicenames" & @CRLF)
                ;;FileFlush($trace)
                For $Tservice In $servicenames
                    $tsregread = RegRead("\\" & $strPCName & $services & "\" & $Tservice, "DisplayName")
                    If $tsregread <> "" Then
                        $sTunerServicei = $sTunerServicei & ";" & $tsregread
                    EndIf
                Next
                ; clean up ;
                $sTunerServicei = StringTrimLeft($sTunerServicei, 1)
                ;FileWriteLine($trace, "Line " & (@ScriptLineNumber + 2) & " $TrendServer = RegRead($trendkey" & @CRLF)
                ;;FileFlush($trace)
                $TrendServer = RegRead($trendkey & "\PC-cillinNTCorp\CurrentVersion", "Server")
                Select
                    Case $TrendServer = 1
                        $TrendServer = "Unable to open Key"
                    Case $TrendServer = 2
                        $TrendServer = "Trend Registry Not Found"
                    Case $TrendServer = 3
                        $TrendServer = "Unable to Connect Registry"
                    Case $TrendServer = -1
                        $TrendServer = "Value Not Found"
                    Case $TrendServer = -2
                        $TrendServer = "Value Type Not Supported"
                EndSelect
                If $TrendServer = "" Then
                    $TrendServer = RegRead("\\" & $strPCName & "\HKLM\SOFTWARE\Wow6432Node\TrendMicro\PC-cillinNTCorp\CurrentVersion", "Server")
                EndIf
                $drivespace = DriveSpaceFree("\\" & $strPCName & "\c$")
                CheckTunerService($strPCName)
                If $sTunerService = "Z_No Tuner Running" And $ExistingService <> "X" And $TunerPath <> "Z_No Tuner File" Then
                    If StringInStr($ExistingService, ";") Then
                        ; should get down to one service! Requires manual investigation
                    Else
                        ;FileWriteLine($trace, "Line " & (@ScriptLineNumber + 2) & ' $startbmc = ShellExecute("sc.exe",' & @CRLF)
                        ;;FileFlush($trace)
                        $startbmc = ShellExecute("sc.exe", "\\" & $strPCName & " start " & $ExistingService, "", "", @SW_HIDE)
                        Sleep(3000)
                        CheckTunerService($strPCName)
                    EndIf
                EndIf
                If $sTunerService = "Z_No Tuner Running" Then
                    ; Check for Tuner.exe process (Windows 7)
                    ;FileWriteLine($trace, "Line " & (@ScriptLineNumber + 2) & ' $pscommand = $pslist & " \\" & $strPCName & " tuner.exe >> pslist.txt"' & @CRLF)
                    ;;FileFlush($trace)
                    $pscommand = $pslist & " \\" & $strPCName & " tuner.exe >> pslist.txt"
                    Local $pwcmdproc
                    $pwcmdproc = Run($pscommand, "", @SW_HIDE)
                    Local $pwc = ProcessWaitClose($pwcmdproc, 60)
                    If $pwc = 0 Then
                        ProcessClose($pwcmdproc)
                    EndIf
                    $pslistfile = FileOpen("pslist.txt", $ForReading)
                    $psfileread = FileRead($pslistfile)
                    $pslistfound = StringInStr($psfileread, "CPU Time")
                    If $pslistfound > 0 Then
                        $sTunerService = "Tuner Process"
                    EndIf
                EndIf
                ; Check Trend Service
                $qTrendService = _ServiceRunning($strPCName, "ntrtscan")
                If $qTrendService = 1 Then
                    $sTrendService = "Trend Running"
                Else
                    $sTrendService = "No Trend Running"
                    If Not StringInStr($strOS, "Server") And Not StringInStr($strOS, "NT") Then
                        FileWriteLine($notrendfile, $strPCName)
                        ShellExecute("sc.exe", "\\" & $strPCName & " start ntrtscan")
                        ShellExecute("sc.exe", "\\" & $strPCName & " start tmlisten")
                    EndIf
                EndIf
                ; Check GoverLan Service
                $qGoverLan = _ServiceRunning($strPCName, "GOVsrv")
                If $qGoverLan = 1 Then
                    $sGoverLan = "GoverLan Running"
                Else
                    $sGoverLan = "No GoverLan Running"
                EndIf
            Else
                ProcessClose($WMITest)
                $sTunerService = "WMI Locked Up"
                $sTrendService = "WMI Locked Up"
            EndIf
        Else
            ProcessClose($WMITest)
            $sTunerService = "WMI Unavailable"
            $IsAlive = $IsAlive & ": WMI Locked (" & $exitcode & ")"
        EndIf
        ;   Write the results to the Results file
        ;FileWriteLine($trace, "Line " & (@ScriptLineNumber + 2) & " FileWriteLine($LogFile" & @CRLF)
        ;;FileFlush($trace)
        FileWriteLine($LogFile, $strPCName & "," & $IsAlive & "," & $strOS & "," & $strOSsp & "," & $strOSv & "," & $strChanged & "," & $strCreated & "," & $strPwdChd & "," & $strSubnet & "," & $TunerPath & "," & $sTunerServicei & "," & $sTunerService & "," & $sTrendService & "," & $TrendServer & "," & $sGoverLan & "," & $drivespace & "," & $startbmc)
    Else
        ;FileWriteLine($trace, "Line " & (@ScriptLineNumber + 2) & " FileWriteLine($LogFile" & @CRLF)
        ;;FileFlush($trace)
        FileWriteLine($LogFile, $strPCName & "," & $IsAlive & "," & $strOS & "," & $strOSsp & "," & $strOSv & "," & $strChanged & "," & $strCreated & "," & $strPwdChd & "," & $strSubnet & "," & $TunerPath & "," & $sTunerServicei & "," & $sTunerService & "," & $sTrendService & "," & $TrendServer & "," & $sGoverLan & "," & $drivespace & "," & $startbmc)
        $recheck = $recheck & "!" & $fqdn
    EndIf
    ;   Cleanup all variables, just to see if there is carryover and prevent contamination of next record
    $WMITest = ""
    $strPCName = "carryover"
    $IsAlive = "carryover"
    $strOS = "carryover"
    $strOSsp = "carryover"
    $strOSv = "carryover"
    $strChanged = "carryover"
    $strCreated = "carryover"
    $strSubnet = "carryover"
    $sTunerService = ""
    $sTrendService = "carryover"
    $strADGROUP = "carryover"
    $Groups = "carryover"
    $strSubnet = "carryover"
    $IPkey = "carryover"
    $TunerPath = "carryover"
    $sTunerServicei = "carryover"
    $sGoverLan = "carryover"
    $startbmc = "carryover"
    $ExistingService = ""
    $Locator = ""
    $Service = ""
EndFunc   ;==>InfoCollect
Func CheckTunerService($strPCName)
    ;FileWriteLine($trace, "Line " & (@ScriptLineNumber - 1) & " Func CheckTunerService($strPCName)" & @CRLF)
    ;;FileFlush($trace)
    Dim $qTunerService, $qTunerServiceE
    $sTunerService = ""
    $Tservice = ""
    ; Check Tuner Services (some excluded)
    For $Tservice In $servicenames
        $qTunerService = _ServiceRunning($strPCName, $Tservice)
        $qTunerServiceE = _ServiceExists($strPCName, $Tservice)
        If $qTunerServiceE = 1 Then
            $ExistingService = $ExistingService & ";" & $Tservice
        EndIf
        If $qTunerService = 1 Then
            $sTunerService = $sTunerService & ";" & $Tservice
        EndIf
    Next
    ; If Z_No Tuner found then say so
    If $sTunerService = "" Then
        $sTunerService = "Z_No Tuner Running"
    EndIf
    ;clean up ExistingService
    $ExistingService = StringTrimLeft($ExistingService, 1)
    $sTunerService = StringTrimLeft($sTunerService, 1)
EndFunc   ;==>CheckTunerService
; This is my custom COM error handler
Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    $timenow = @HOUR & ":" & @MIN
    FileWriteLine($ErrorFile, $strPCName & "," & $timenow & "," & $oMyError.description & "," & $HexNumber & ", Line " & $oMyError.scriptline & "," & $oMyError.windescription)
    $g_eventerror = 1 ; something to check for when this function returns
EndFunc   ;==>MyErrFunc
Func ResultsCleanup()
    Dim $sfile, $search, $filecreated, $now, $datetest, $created
    $search = FileFindFirstFile("AssetCollect*.csv")
    If $search = -1 Then Return
    While 1
        $sfile = FileFindNextFile($search)
        If @error Then ExitLoop
        $created = FileGetTime($sfile)
        $filecreated = $created[0] & "/" & $created[1] & "/" & $created[2] & " " & $created[3] & ":" & $created[4] & ":" & $created[5]
        $now = _NowCalc()
        $datetest = _DateDiff("D", $filecreated, $now)
        If $datetest > 14 Then
            FileDelete($sfile)
        EndIf
    WEnd
    $search = FileFindFirstFile("status*.txt")
    If $search = -1 Then Return
    While 1
        $sfile = FileFindNextFile($search)
        If @error Then ExitLoop
        $created = FileGetTime($sfile)
        $filecreated = $created[0] & "/" & $created[1] & "/" & $created[2] & " " & $created[3] & ":" & $created[4] & ":" & $created[5]
        $now = _NowCalc()
        $datetest = _DateDiff("D", $filecreated, $now)
        If $datetest > 7 Then
            FileDelete($sfile)
        EndIf
    WEnd
    $search = FileFindFirstFile("Error*.csv")
    If $search = -1 Then Return
    While 1
        $sfile = FileFindNextFile($search)
        If @error Then ExitLoop
        $created = FileGetTime($sfile)
        $filecreated = $created[0] & "/" & $created[1] & "/" & $created[2] & " " & $created[3] & ":" & $created[4] & ":" & $created[5]
        $now = _NowCalc()
        $datetest = _DateDiff("D", $filecreated, $now)
        If $datetest > 7 Then
            FileDelete($sfile)
        EndIf
    WEnd
EndFunc   ;==>ResultsCleanup
;Func ExitReport()
;   $xitrpt = FileOpen(@ScriptDir & "\Exitreport.txt", 1)
;   FileWriteLine($xitrpt, @MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN)
;   FileWriteLine($xitrpt, "Exit Code : " & @exitCode & @CRLF & "Exit Method : " & @exitMethod)
;   FileClose($xitrpt)
;   Exit
;EndFunc   ;==>ExitReport

Anyone have any ideas on why this is hanging up??

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.