Service will be sluggish when scrpit encounters error

Ask your Windows 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.
User avatar
jason@sbtc.com.tw
Posts: 24
Joined: Thu Apr 13, 2017 8:32 pm

Re: Service will be sluggish when scrpit encounters error

Post by jason@sbtc.com.tw » Thu Sep 27, 2018 8:48 pm

jvierra wrote:
Fri Sep 21, 2018 3:22 am
Jason. You are not trapping errors.

Can you please post the complete service script.
Complete script

# Services lab

cls
$logFile = "c:\log\srvlab1.txt"
$sourcefolder = "C:\TripodTechLab\temp\lab\"
$FilesArray = Get-ChildItem $sourcefolder
$Status = $true
$aa = 0
$aa >> $logfile
foreach ($t0 in $FilesArray)
{
try
{
Remove-Item $t0.FullName
}
catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Host "$ErrorMessage"
Write-Host "$FailedItem"
#Write-EventLog "$ErrorMessage"
}
}


while ($Status)
{


Start-Sleep -Seconds 1
$aa = $aa + 1
$aa >> $logfile
if ($aa -gt 10)
{
$Status = $false
}
}

User avatar
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Service will be sluggish when scrpit encounters error

Post by jvierra » Thu Sep 27, 2018 9:29 pm

That is not a service script. It is missing all required elements.

You need to use the "New Project/Service Project" template to create a service. A service script looks like this in part:

Code: Select all

function Start-MyService
{
	# Place one time startup code here.
	# Initialize global variables and open connections if needed
	$global:bRunService = $true
	$global:bServiceRunning = $false
	$global:bServicePaused = $false;
}

function Invoke-MyService
{
	$global:bServiceRunning = $true
	while($global:bRunService) {
		try 
		{
			if($global:bServicePaused -eq $false) #Only act if service is not paused
			{
				#Place code for your service here
				#e.g. $ProcessList = Get-Process solitaire -ErrorAction SilentlyContinue
				
				# Use Write-Host or any other PowerShell output function to write to the System's application log
			}			
		}
		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.
		}
		else 
		{
			Start-Sleep –Seconds 10 # a lower number will make your service active more often and use more CPU cycles
		}
	}
	$global:bServiceRunning	= $false
}

function Stop-MyService
{
	$global:bRunService = $false # Signal main loop to exit
	$CountDown = 30 # Maximum wait for loop to exit
	while($global:bServiceRunning -and $Countdown -gt 0) 
	{
		Start-Sleep -Seconds 1 # wait for your main loop to exit
		$Countdown = $Countdown - 1
	}
	# Place code to be executed on service stop here 
	# Close files and connections, terminate jobs and 
	# use remove-module to unload blocking modules
}

function Pause-MyService
{
	# Service is being paused
	# Save state 
	$global:bServicePaused = $true
	# Note that the thread your PowerShell script is running on is not suspended on 'pause'.
	# It is your responsibility in the service loop to pause processing until a 'continue' command is issued.
	# It is recommended to sleep for longer periods between loop iterations when the service is paused
	# in order to prevent excessive CPU usage by simply waiting and looping.
}

function Continue-MyService
{
	# Service is being continued from a paused state
	# Restore any saved states if needed
	$global:bServicePaused = $false
}

User avatar
jason@sbtc.com.tw
Posts: 24
Joined: Thu Apr 13, 2017 8:32 pm

Re: Service will be sluggish when scrpit encounters error

Post by jason@sbtc.com.tw » Sat Sep 29, 2018 12:34 am

jvierra wrote:
Thu Sep 27, 2018 9:29 pm
That is not a service script. It is missing all required elements.

You need to use the "New Project/Service Project" template to create a service. A service script looks like this in part:
......
.....
Thank you for your help, i will rewrite my script.

Locked