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 1 year and 5 days 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.
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:
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?
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.
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.
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 1 year and 5 days 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.