Solved

AddingTimeout to powershell script

Posted on 2014-09-30
2
389 Views
Last Modified: 2014-10-26
Hello Experts,
I have a script which is getting hung and not able to proceed to next commandline
Below is the script
$ST = new-object -com schedule.service
$Server = "Server1"
$ST.Connect($server)
$M = $ST.GetFolder("\").GetTask("GWSTSAutoPatch")
It gets hung on $ST.GetFolder("\").GetTask("GWSTSAutoPatch") and wont proceed into next command.
Now , question is  , Can I add a timeout on this? so , that script will wait for few sec and it will proceed if it does not get any response.
Thanks,
-Prashant Girennavar.
0
Comment
Question by:Prashant Girennavar
  • 2
2 Comments
 
LVL 28

Expert Comment

by:becraig
Comment Utility
You can use a do - until loop

do {$ST.GetFolder("\").GetTask("GWSTSAutoPatch")
Start-sleep 10}
until ($ST.GetFolder("\").GetTask("GWSTSAutoPatch" = some value; $M = $ST.GetFolder("\").GetTask("GWSTSAutoPatch"))

Open in new window



Something like the above, I've not actually tested what you're trying to do though
0
 
LVL 28

Accepted Solution

by:
becraig earned 500 total points
Comment Utility
In the edit below, I simply add a try catch and if do not get a value for M we make it null and proceed.
$ST = new-object -com schedule.service
$Server = "Server1"
$ST.Connect($server)
start-sleep 15
try {
$M = $ST.GetFolder("\").GetTask("GWSTSAutoPatch")
}
catch [Exception] {
$M = $null
 return $_.Exception.Message
}

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now