Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I need a command to stop the IMAservice on a remote server, which has dependencies on two other citrix services, so I have to give a /y to stop.

If I stopped the ima service from the same computer, I would use "net stop IMAService /y".

Is there a way to do this from a remote computer. The "SC \remotecommputer stop IMAservice" doesn't give this Option.

share|improve this question
1  
Any chance you can use Powershell instead of sc or net? –  jscott Feb 17 '13 at 1:01

1 Answer 1

Here's a Powershell script I wrote to stop/start a service. It does some error handling and waits to make sure the process stop/starts correctly, otherwise it raises an error and sends an email after a predefined timeout (modify script variables at top). Feel free to use.

[CmdletBinding()]
Param()

### Define Variables - MODIFY THESE FOR YOUR SERVICE

$ComputerName = "."
$ServiceName = "ServiceName"
$TimeoutSecs = 60
$MailFrom = "user@address.com"
$MailReplyTo = "user@address.com"
$MailTo = "user@address.com"
$MailServer "mail.address.com"

### Define Functions

Function Stop-MyService {
    If ($MyService.Status -eq "Running") {
        Write-Host "Stopping $ServiceName service"
        Try {$MyService.Stop()}
        Catch {Process-Error $_}
        $Timer = [diagnostics.stopwatch]::StartNew()
        While ($MyService.Status -ne "Stopped") {
            If ($Timer.Elapsed -ge $Timeout) {
                Try {Throw "Timeout exceeded stopping $ServiceName service"}
                Catch {Process-Error $_}
            }
            Start-Sleep -Seconds 1
            Write-Verbose "Timer: $([int]$Timer.Elapsed.TotalSeconds) secs"
            $MyService.Refresh()
        }
        Write-Host "Service stopped"
        Return
    }
}

Function Start-MyService {
    If ($MyService.Status -eq "Stopped") {
        Write-Host "Starting $ServiceName service"
        Try {$MyService.Start()}
        Catch {Process-Error $_}
        $Timer = [diagnostics.stopwatch]::StartNew()
        While ($MyService.Status -ne "Running") {
            If ($Timer.Elapsed -ge $Timeout) {
                Try {Throw "Timeout exceeded starting $ServiceName service"}
                Catch {Process-Error $_}
            }
            Start-Sleep -Seconds 1
            Write-Verbose "Timer: $([int]$Timer.Elapsed.TotalSeconds) secs"
            $MyService.Refresh()
        }
        Write-Host "Service started"
        Return
    }
}

Function Process-Error ([System.Management.Automation.ErrorRecord]$MyError) {

  Write-Error -ErrorRecord $_

  $smtpBody = @"
DATE: $(Get-Date -Format "g")
HOST: $Env:COMPUTERNAME
SCRIPT: $((Get-Variable MyInvocation -Scope Script).Value.MyCommand.Path)
ERROR: $MyError
"@

  $smtpMsg = new-object Net.Mail.MailMessage
  $smtpMsg.From = $MailFrom
  $smtpMsg.ReplyTo = $MailReplyTo
  $smtpMsg.To.Add($MailTo)
  $smtpMsg.Subject = "ERROR: $MyError"
  $smtpMsg.Body = $smtpBody
  $smtpClient = new-object Net.Mail.SmtpClient($MailServer)
  $smtpClient.Send($smtpMsg)

  Exit 1
}

### Main

## Create objects
$Timeout = new-timespan -Seconds $TimeoutSecs
Try {$MyService = Get-Service -ComputerName $ComputerName -Name $ServiceName -ErrorAction Stop}
Catch {Process-Error $_}

## Stop service
Stop-MyService

## Start service
Start-MyService

Exit 0
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.