Stopping a Service

Hi all.
I’m (very) new to the world of Powershell Scripting so please be patient with me :wink:

When our developers finish a new version of their app, its is getting deployed to a testing-machine. But in order to install the new version, the old needs to be stopped first, which shall be done with a simple PS script.

Unfortunately our byte-forgers made the “current version string” part of the app’s name. So I can’t just hardwire the “Service Name” to do a simple stop …

So here is my task: Find the current running Service, stop it … if there’re some processes left, kill 'em. Install/deploy the new version and start the Service afterwards.

Her is a “part” of the Script:

function get-proc {
  param ( $service_name )

  $Service=get-wmiobjectwin32_service|where { $_.name -match $service_name }
    if ($Service -ne $null ){
      $global:procID=$Service.ProcessId
      $global:serviceID=$Service.Name
    }

  Write-Output $Service
}

functionstop-service {
  param ( $service_name )

  foreach ( $srvsID in $global:serviceID ) {
    Write-Output "stopping: $srvsID"
    Stop-Service -Name $srvsID -Force
  } 

Sometimes the script finishes … but nothing (ever) happens. No error thrown, Service still running.
But sometimes it crashes with:
"Script failed du to call depth overflow. "

Can you help me out ?

Hi, welcome to the forum :wave:

This error is occuring because you’re calling your Stop-Service function recursively. Try renaming your Stop-Service function to StopService or Stop-MyService so that it doesn’t clash with the PowerShell Stop-Service cmdlet. That way, the call to Stop-Service in your loop will call the cmdlet and not your function.

Also, while it’s hard to know for certain without seeing the rest of the script, you probably don’t need (and should avoid using) global variables.

You’ve not provided details of them, but I wonder if you could just use a wildcard * in your search for the service and process names?