header1   header
header
header : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012

PowerShellCommunity.org is moving!  This community software, and the hardware that it sits on, are no longer serving the purposes of this community.  As a result, we have decided to move this community to a new home at PowerShell.org.  PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site.  We've already started getting some great questions from members of the community over there so please, come on over and join us!

While we are going through this transition, this site will remain up for the short term.  New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.

[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.

If you have any questions, please let us know on the PowerShell.org site.

Thank you,

Kirk "Poshoholic" Munro

 
PSSession and service-restart with a timeout
Last Post 08 Sep 2010 08:04 AM by leeh. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
pannivasUser is Offline
New Member
New Member
Posts:1
Avatar

--
10 Jun 2010 03:09 AM
    Hello community,

    I have a script that basically does the following things.
    - establish a PSSession with a remote host
    - get the service status
    - according to the status of the service then stop/start/kill process

    My only problem is that the specific service sometimes gets stacked on the StopPending status thus my script get's stacked.  This does not happen often nevertheless it does.

    Is there a way to start the stop-service scriptblock with a failed timeout or something? 
    For example

    stop-service wait for 60seconds if it doesn't stop then do something else

    this is the part of the code that is important.


    $PSRemoteSession = New-PSSession -ComputerName $ServerName -Credential user1

    $serviceInfo = Invoke-Command -Session $PSRemoteSession -ScriptBlock {Get-Service -Name SrvNameHere}

    $serviceName = $serviceInfo.Name

    switch ($serviceInfo.status) {

    'Stopped' {
    Invoke-Command -Session $PSRemoteSession -Verbose -ArgumentList $serviceName -ScriptBlock {param ($serviceName) Start-Service -Name $serviceName -Verbose}
     }

    'StopPending' {
    Invoke-Command -Session $PSRemoteSession -Verbose -ScriptBlock {Stop-Process -Force -Name mtsrv -Confirm} Invoke-Command -Session $PSRemoteSession -Verbose -ArgumentList $serviceName -ScriptBlock {param ($serviceName) Start-Service -Name $serviceName -Verbose}
     }

    'Running' {
    #this is the step were i would ideally start the service stop with a timeout of 60seconds.
    #if nothing happens then do something else for example kill the process and start it again.

    Invoke-Command -Session $PSRemoteSession -Verbose -ArgumentList $serviceName -ScriptBlock {param ($serviceName) Stop-Service -Name $serviceName -Force -Confirm}

     #basicaly if the service is not stop the script keeps running for ever.
    }
    }

    Any help will be much appreciated.

    Thanks
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    14 Jun 2010 02:00 AM

    You can use a Timer for this:

    $ElapsedEventHandler = {
        param ([System.Object]$sender, [System.Timers.ElapsedEventArgs]$e)
       
        ($sender -as [System.Timers.Timer]).Stop()
        Unregister-Event -SourceIdentifier Timer.Elapsed
       
        # Do whatever you want
    }

    $PSRemoteSession = New-PSSession -ComputerName $ServerName -Credential user1
    $serviceInfo = Invoke-Command -Session $PSRemoteSession -ScriptBlock { Get-Service -Name SrvNameHere }

    $serviceName = $serviceInfo.Name

    switch ($serviceInfo.Status) {
        'Stopped' {
            Invoke-Command -Session $PSRemoteSession -Verbose -ArgumentList $serviceName -ScriptBlock { param ($serviceName) Start-Service -Name $serviceName -Verbose }
         }

        'StopPending' {
            Invoke-Command -Session $PSRemoteSession -Verbose -ScriptBlock { Stop-Process -Force -Name mtsrv -Confirm } Invoke-Command -Session $PSRemoteSession -Verbose -ArgumentList $serviceName -ScriptBlock { param ($serviceName) Start-Service -Name $serviceName -Verbose }
         }

        'Running' {
            $timer = New-Object System.Timers.Timer
            $timer.Interval = 60000
            Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier Timer.Elapsed -Action $ElapsedEventHandler
            $timer.Start()

            Invoke-Command -Session $PSRemoteSession -Verbose -ArgumentList $serviceName -ScriptBlock { param ($serviceName) Stop-Service -Name $serviceName -Force -Confirm }
        }
    }

    leehUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    07 Sep 2010 06:37 AM
    Hi,

    i am trying to implement something similar (but on a local machine, instead of a remote service). Code below:

    $SERVICENAME = "smartFOCUS.Marketer.Connectors.eChannel.Service.eChannelConnector_SFT002"
    $PROCESSNAME = "smartFOCUS.Marketer.Connectors.eChannel.Service.exe"

    $ElapsedEventHandler = { param ([System.Object]$sender, [System.Timers.ElapsedEventArgs]$e)
       ($sender -as [System.Timers.Timer]).Stop() 
       Unregister-Event -SourceIdentifier Timer.Elapsed 

       # stop process
       Stop-Process -Name $PROCESSNAME -Force
    }

    $serviceInfo = Get-Service -Name $SERVICENAME
    switch ($serviceInfo.Status)

       'Running' 
       { 
             $timer = New-Object System.Timers.Timer 
             $timer.Interval = 10000 
             Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier Timer.Elapsed -Action $ElapsedEventHandler 
             $timer.Start() 

             Stop-Service -Name $SERVICENAME -Force 
       }
    }

    Not sure if i've done something silly here, but it doens't appear that timer is firing after it's elapsed 10 seconds. it still returns "service stopping" messages ad-infinitum

    Any suggestions much appreciated.

    Thansk
    leehUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    08 Sep 2010 08:04 AM
    You can do this by executing the Stop-Process as a background job:

    Start-Job -ScriptBlock {Stop-Service -Name "SERVICE NAME" -Force }

    #give it 5 seconds to stop
    Start-Sleep -Seconds 5

    $SERVICESTATE = (Get-Service | where{$_.Name -eq "SERVICE NAME"}).Status
    if( $SERVICESTATE -eq "Stopping" -or $SERVICESTATE -eq "StopPending")
    {
        # still stopping so force process stop
        Stop-Process -Name "PROCESS NAME" -Force
    }
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Many thanks to our original sponsors: Quest Software • SAPIEN Technologies • Compellent • Microsoft footer
    footer   footer