Need Help with Package as Service

Is there a way without using Start-Job to execute a while loop function as part of the OnStart() function and still have it return the service to Started State?

The Problem I am having is that because of the while loop the service is in a perpetual state of “Starting” even though it is running. Is there any way to accomodate this without using Jobs? I ask because when you use a job it creates a new powershell.exe process that is more difficult to monitor. I want it so that if the EXE stops for any reason to include user termination that the service will stop as well. and in the job scenario, if the powershell.exe instance is closed the service does not stop.

Here is the script I am trying to make a service. There are other functions it calls that are not included for security reasons, and it seems to have no problem calling them when it gets stuck in “starting” state

Just hangs on the while($true) loop which prevents it from reaching “started/running” state

  # Script Start
  $servicepath = Split-Path (Get-WmiObject win32_service | Where-Object{$_.Name -eq 'Pega-PagerDuty'} | Select-Object @{Name="Path"; Expression={$_.PathName.split('"')[1]}}).Path
  $exePath = Convert-Path "$servicepath"
  Set-Location $exePath
  Write-Host "Started: $(Get-Date)" -ForegroundColor Green
  Out-File -InputObject "$(Get-Date): Service Started" -FilePath "$exePath\Log.txt" -Append
  $enablePagerDuty = $true
  $script:config = Get-ConfigFile -file "$exePath\Config.ini"
  $config | Out-File -FilePath "$exePath\testconfig.txt"
  while($true) {
    #Get Pega OAUTH Token
    if(!($tokenDetails)) {
      #Get Initial Token
      $script:tokenDetails = Get-PegaAuthToken
      $script:tokenExpire = (Get-Date).AddSeconds(($tokenDetails.expires_in - 300))
      $script:refreshToken = $tokenDetails.refresh_token
    if ((Get-Date) -ge $tokenExpire) {
    #Refresh Token
      $script:tokenDetails = Update-PegaAuthToken($refreshToken)
      $script:tokenExpire = (Get-Date).AddSeconds($tokenDetails.expires_in - 300)
      $script:refreshToken = $tokenDetails.refresh_token
    Out-File -InputObject $tokenDetails -FilePath "$exePath\testtokendetails.txt"
    if(((get-date).Second % $script:config.CheckInterval) -eq 0) {
      #Fetch and Post Data
      $pegaData = (Get-PegaNotifications($tokenDetails.access_token) | Sort-Object GeneratedDateTime)
      $pegaData | Out-File -FilePath "$exePath\testpegaData.txt"
      if($pegaData) {
        Write-Host "$(Get-Date): Processing $($pegaData.count) Alerts" -ForegroundColor Yellow
        Write-EventLog -LogName Application -Source "Pega-PagerDuty" -EntryType Information -EventId 1 -Message "Processing $($pegaData.count) Alerts"
        Out-File -InputObject "$(Get-Date): Processing $($pegaData.count) Alerts" -FilePath "$exePath\Log.txt" -Append
        Foreach($item in $pegaData) {
          $formattedItem = Format-DataforPagerDuty($item)
          if($enablePagerDuty) {
            $pagerDutyResponse = Add-DataToPagerDuty($formattedItem)
            $pagerDutyResponse | Tee-Object -FilePath "$exePath\Log.txt" -Append
        Write-Host "$(Get-Date): Alert Processing Completed" -ForegroundColor Green
        Out-File -InputObject "$(Get-Date): Alert Processing Completed" -FilePath "$exePath\Log.txt" -Append
        Write-EventLog -LogName Application -Source "Pega-PagerDuty" -EntryType Information -EventId 1 -Message "Alert Processing Completed"
        start-sleep 1
        #Current Second not 00 or 30
        start-sleep 1
# Script End