Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

AddingTimeout to powershell script

Posted on 2014-09-30
2
Medium Priority
?
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40353917
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 29

Accepted Solution

by:
becraig earned 1500 total points
ID: 40356297
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

Ready for your healthcare security check-up?

In the past few years, healthcare organizations have become a prime target for advanced attacks. Does your organization have what it needs to defend itself? Schedule your healthcare security check-up today and download our free Healthcare Security Resource Kit today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

596 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