Bentley Communities
Bentley Communities
  • Site
  • User
  • Site
  • Search
  • User
ProjectWise
  • Product Communities
ProjectWise
ProjectWise PowerShell Extensions Forum ThreadOption Cannot be found
    • Sign In

    • State Not Answered
    • Replies 0 replies
    • Subscribers 64 subscribers
    • Views 115 views
    • Users 0 members are here
    • powershell
    • ProjectWise
    • pwps_dab
    • ProjectWise PowerShell Modules

    ThreadOption Cannot be found

    Ananthakumar Mani
    Offline Ananthakumar Mani 9 months ago

    Intension :  We are running a script via Powershell ISE, this script used to call another script and will running continuesly 24/7. The problem is it get struck between some point. and we need to re-start it manually. For that we put the below script to stop the thread in some point and re-start it again automatically. But the issue is we are getting some error message inbetween the stage of running this script.

    Child Script :  If you are testing, You can link your any one of the script as a child for $script inside RunForEver.ps1

    RunForEver.ps1 :

    $script = 'L:\ProjectWise\PowerShellScripts\Wizdom\UserManage_Membership_SIJ_v1_MVP2_1_Groups_ConnectedUsers_Alt.ps1'
    do
    {
    
    & $script
    Write-Host "Sleeping 90 seconds"
    Start-Sleep -s 90  
    }
    while ($True)
     

    Main Script : 

    $script = "L:\ProjectWise\PowerShellScripts\Wizdom\RunForEver.ps1"
    function sendTheWarning
    {
        $smtpServer = "smtp.XXXXX.com"
        $msg = new-object Net.Mail.MailMessage
        $smtp = new-object Net.Mail.SmtpClient($smtpServer)
        
        $dateString = get-date
        
        $msg.From = "noreply@XXXXXX.com"
        $msg.To.Add("XX@XXXXX.com,XX@XXXX.com")
        $msg.subject = "UserManagement Scripts are Stuck (SERVERNAME)"
        $msg.body = ([string]$dateString) + " - Powershell ISE stuck while running usermanagement scripts at (SERVERNAME) may needed a restart"
        $smtp.send($msg) 
    }
    
    
    
    do
    {
        $i = 0
        $ThreadDone = $False
        $ThreadScript = Start-ThreadJob -FilePath $script -Name "Wizdom Usermanagement Script" -Verbose
        $FailedCounter = 0
        $ThreadRunning = $True
        do
        {
            sleep -Seconds 10
            $ThreadScript.State
            if($ThreadScript.State -eq 'Running' -and (($ThreadScript.Information).Count -gt 0))
            {
                Receive-Job -Job $ThreadScript
                write-host $ThreadScript.Information
                $FailedCounter = 0
                $ThreadRunning = $True
                
            }
            elseif($ThreadScript.State -ne 'Running')
            {
                $ThreadRunning = $False
                break
            }
            elseif($FailedCounter -gt 20)
            {
                $ThreadRunning = $False
                #DO WE DO SOMETHING HERE?
                break
            }
            else
            {
                
                $FailedCounter = $FailedCounter + 1  
                Write-Host ("No update number: " + $FailedCounter)
            }
    
            #$i = $i + 1
        } while ($True)
        write-host $ThreadScript.Information 
        if(!$ThreadRunning)
        {
            sendTheWarning
            "Before stopping: " + $ThreadScript.State
            $ThreadScript.StopJob($True,"Was running for to long.")
            "After stopping: " + $ThreadScript.State
           
        }
        $ThreadScript.Dispose()
        foreach($job in (Get-Job))
        {
            Remove-Job -id $job.Id
        }
        $ThreadScript = $null
        #Get-Job
    
    
        #& $script
        Write-Host "Sleeping 90 seconds"
        Start-Sleep -s 90  
    }
    while ($True)

    • Sign in to reply
    • Cancel

    Communities
    • Home
    • Getting Started
    • Community Central
    • Products
    • Support
    • Secure File Upload
    • Feedback
    Support and Services
    • Home
    • Product Support
    • Downloads
    • Subscription Services Portal
    Training and Learning
    • Home
    • About Bentley Institute
    • My Learning History
    • Reference Books
    Social Media
    •    LinkedIn
    •    Facebook
    •    Twitter
    •    YouTube
    •    RSS Feed
    •    Email

    © 2023 Bentley Systems, Incorporated  |  Contact Us  |  Privacy |  Terms of Use  |  Cookies