Share via


Powershell Function Time Out

Question

Thursday, March 9, 2017 12:42 PM

Hi All,

I have developed a PowerShell script to monitor the availability of a Exchange Public Folder using Outlook.

I monitor the availability by posting a message to the public folder. If the script is able to post an item to the Public Folder, then it is considered as a success else it is a failure. This is done every minute.

There are scenarios that I encountered where the post takes more than a minute. I want to include a time out to the function so that maximum time the post can happen is should be within a minute else the control needs to be returned for my further processing.

I tried using a stop watch and it did not work. Below is the code snippet from my script.

Function SendPost()
{
$post_item = $PFServerList[0].PFfolder.items.add(6) # This is Outlook COM Object
$uniqueID = get-random  
        $post_subject = "Test Client MAPI from " + $hostname + " " + $uniqueID
        $post_item.Subject = $post_subject
$post_item.Post()  # **More time is taken here and control is not returned **
}

SendPost

I want the SendPost function to execute for a maximum of 1minute. If the execution is taking more than 1minute, immediately I need to do further actions. Can anyone help me in achieving this?

Thanks,
Ashrith

All replies (4)

Thursday, March 9, 2017 1:17 PM

Hi Ashrith,

I think you can do this with PowerShell Jobs where a time out can be set. Here is the example

# Start Job
$job = Start-Job -ScriptBlock {
    Start-Sleep -Seconds 5000
}
# Wait for job to complete with timeout (in sec)
$job | Wait-Job -Timeout 2

# Check to see if any jobs are still running and stop them
$job | Where-Object {$_.State -ne "Completed"} | Stop-Job

Thanks, Samer


Friday, March 10, 2017 4:58 AM

Hi Samer,

Thanks for your suggestion.

The above snippet which I had shared is already inside a scriptblock. The scriptblock creates an instance of Outlook and uses this object to post an item to the Public Folder. If I create another scripblock, I cannot pass this Outlook object as active objects cannot be passed to jobs and leads to thread violation exception. On the other hand I cannot create jobs each minute as it takes 2 or 3 minutes for the Outlook to initialize. Please let me know if there are any other alternatives.

Thanks,

Ashrith


Friday, March 10, 2017 6:08 AM

Hi Ashrith,

I am not aware of any other method for setting up a timeout. For better performance Instead of Jobs we can use Runspace which creates a different thread instead of a new process like Jobs. I think you can get the output of command which is run on a different runspace but we wont be able to pass an object so yeah it does not solve your problem either. May be you will have to create the required objects again in the new runspace if there is no other option.

https://blogs.technet.microsoft.com/heyscriptingguy/2015/11/26/beginning-use-of-powershell-runspaces-part-1/

 

Thanks, Samer Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!


Friday, March 10, 2017 7:37 AM

objects can be passed (or shared) between runspaces.

\(ツ)_/