Using invoke-command with a timeout

Welcome Forums General PowerShell Q&A Using invoke-command with a timeout

  • This topic has 7 replies, 5 voices, and was last updated 1 year ago by
    js
    Participant
    .
Viewing 4 reply threads
  • Author
    Posts
    • #74662
      Participant
      Topics: 23
      Replies: 15
      Points: 28
      Rank: Member

      I use invoke-command to execute a scriptblock on a remote host.
      My code looks like:

      I need to stop the script block after 30 second, even if myfile.txt is not yet complete.
      How can I set a timeout?
      Regards
      marius

    • #74666
      Participant
      Topics: 2
      Replies: 234
      Points: 189
      Rank: Participant

      You could kick it as a job.

      • #77980
        js
        Participant
        Topics: 32
        Replies: 842
        Points: 2,687
        Helping Hand
        Rank: Community Hero

        Is there a way to make resolve-dnsname timeout in 1 second? stop-job (or remove-job -force) takes about 4 seconds.

    • #77988
      Participant
      Topics: 6
      Replies: 667
      Points: 97
      Helping Hand
      Rank: Member

      You can try using the -quicktimeout Parameter of the Resolve-DNSName cmdlet. It does not let you specify a timeout value, but it’s quicker than the default.

      • #78016
        js
        Participant
        Topics: 32
        Replies: 842
        Points: 2,687
        Helping Hand
        Rank: Community Hero

        -quicktimeout has no effect in this case. Just try any ip without a hostname.

        I think if the first post here isn’t updated, it doesn’t appear on the main page.

      • #78022
        js
        Participant
        Topics: 32
        Replies: 842
        Points: 2,687
        Helping Hand
        Rank: Community Hero

        Oh well. In this case, -dnsonly fixes it.

    • #104366
      Participant
      Topics: 0
      Replies: 1
      Points: 5
      Rank: Member

      OK so from what I can see above the solution is to run Invoke-Command within a job, downside is that it will time out the whole lot when the job is stopped.

      So in my example I had an actual problem where I wanted to retrieve information about services running on several hundred servers. I didn’t want the Invoke-Command to halt after a specific time but, if there was a server that wasn’t responding, it would timeout for that particular computer so that the batch wouldn’t be waiting for it.

      In my case I had one duff server that was making the query take 30 mins to run, with the code below the whole lot runs in about 2.5 mins, less then a tenth of the time.

      Basically I’ve just put the start-job within the ICM.

      Hopefully someone may find this useful

    • #178482
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,687
      Helping Hand
      Rank: Community Hero

      Just to add to that other thread https://powershell.org/forums/topic/using-invoke-command-with-a-timeout/, which is closed for some reason. Invoke-command has an -asjob parameter already. And with multiple computers, they get run as child jobs that you can check on.

      And you can see which ones aren’t completed…

Viewing 4 reply threads
  • The topic ‘Using invoke-command with a timeout’ is closed to new replies.