Solved

AddingTimeout to powershell script

Posted on 2014-09-30
2
409 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 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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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 …
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 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