Creating A Service script

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 11 months and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
B Daring
Posts: 90
Last visit: Mon Feb 05, 2024 3:00 pm
Answers: 2
Has voted: 1 time

Creating A Service script

Post by B Daring »

Hello,

I'm trying to wrap my head around creating a powershell script to run as a service. I got all that down but I have a lot of inconsistencies with how it operates. Can I get some clarification on how the script actually functions? I understand the start and stop and invoke functions, but let say 'I have this:

Code: Select all

function Invoke-MyService
{
	$global:bServiceRunning = $true
	while($global:bRunService) {
		try 
		{
			# Use Write-Host or any other PowerShell output function to write to the System's application log
                        if($global:bServicePaused -eq $false) #Only act if service is not paused
			{
			    if ($Foo -ne 0){
                                 #Place code for your service here
				For ($H = 1; ($H - 1) -lt $HeaderCount; $h++) #Loop for each Header item
				{
                                    #do some stuff here, but if a variable isn't empty I break out of it
                                      If ($Something -ne "")  { Break }
                                  }
                                 #more code below after break.
                                 .
                                 .
                                 .
                                 .
                            }
                            Pause-MyService

			}
		}
		catch
		{
			# Log exception in application log
			Write-Host $_.Exception.Message
		}
		# Adjust sleep timing to determine how often your service becomes active
		if($global:bServicePaused -eq $true)
		{
			Start-Sleep -Seconds 20 # if the service is paused we sleep longer between checks
			Continue-MyService
		}
		else
		{
			Start-Sleep –Seconds 10 # a lower number will make your service active more often and use more CPU cycles
		}
	}
	$global:bServiceRunning	= $false
}
Question is, once the pause-MyService has executed, where does the script continue from? Does it continue from the break in the for-loop? or the very top of the Invoke-MyService func? or even the pause function?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Creating A Service script

Post by jvierra »

When the "paused" frag is true the service runs in the sleep section. When false it takes the other path.

Why would you want it to do this? Pausing just skips the main loop. Where did you get the original code? There is no not need to put continue into the paused section.

The "if" just executes a sleep statement when paused and executes the false section when not paused. This is where your code goes.
  1.             if($global:bServicePaused -eq $false) #Only act if service is not paused
  2.             {
  3.                 #Place code for your service here
  4.                 #e.g. $ProcessList = Get-Process solitaire -ErrorAction SilentlyContinue
  5.                
  6.                 # Use Write-Host or any other PowerShell output function to write to the System's application log
  7.             }
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Creating A Service script

Post by Alexander Riedel »

Pause-MyService and Continue-MyService are event handlers called by the service wrapper when a service is paused or continued from the service control panel (or PowerShell service functions)
You must not call these functions yourself.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
B Daring
Posts: 90
Last visit: Mon Feb 05, 2024 3:00 pm
Answers: 2
Has voted: 1 time

Re: Creating A Service script

Post by B Daring »

Thanks guys,
I was confused by this line from the template

#It is your responsibility in the service loop to pause processing until a 'continue' command is issued.

That's why I added it. But if that pause function is only if you right click and pause the service that makes sense. It is not well perceived from that commented text that you shouldn't do it in code.

I also found out my problem. I was not getting my variables to clear when I cleared them and found out that they MUST be declared in the Start-MyService function. I had some of them declared in there but not all of them.
This topic is 11 months and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked