?
Solved

AddingTimeout to powershell script

Posted on 2014-09-30
2
Medium Priority
?
413 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

800 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