Hi,
I am running PS 2017 x64 on Windows 10 x64.
I have a question regarding converting powershell scripts into Service.
I have a script which continuously probe VPN tunnel status using ICMP requests. The script using do while loop and runs infinitely.
Now, If i convert this script into service, will it run only once while the service is running or shall i modify the script to run once and once it's transformed into service, the service will run it infinitely ?
and would the script keep running irrespective of whether the user is logged on or not?
Regards,
Navdeep
Question | Powershell script as service
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: Question | Powershell script as service
Please refer to the following articles on how to write a service script:
https://www.sapien.com/blog/2017/07/12/ ... owershell/
If you use the functions mentioned in the following article, you can respond to calls to close the service:
https://www.sapien.com/blog/2018/02/27/ ... ript-2018/
We hope to add the template to PowerShell Studio soon.
https://www.sapien.com/blog/2017/07/12/ ... owershell/
If you use the functions mentioned in the following article, you can respond to calls to close the service:
https://www.sapien.com/blog/2018/02/27/ ... ript-2018/
We hope to add the template to PowerShell Studio soon.
David
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Question | Powershell script as service
Hi David,
I followed the first article and converted the following script into service. The service is running but i don't see any data coming in.
If i manually start the script, i can see the data coming in.
I followed the first article and converted the following script into service. The service is running but i don't see any data coming in.
If i manually start the script, i can see the data coming in.
Code: Select all
### Create a MetricDatum .NET object
$Metric = New-Object -TypeName Amazon.CloudWatch.Model.MetricDatum
start-sleep -Seconds 1
$Metric.MetricName = 'TunnelState'
do
{
Start-Sleep -Seconds 1
if (Test-Connection -Count 1 172.18.2.14 -ErrorAction SilentlyContinue)
{
$Metric.Value = 1
$Metric.Timestamp = [DateTime]::UtcNow
$Metric
Write-CWMetricData -ProfileName AWSCLI -Region ap-southeast-1 -Namespace VPNTunnelStatus -MetricData $Metric
}
else
{
$Metric.Value = 2
$Metric.Timestamp = [DateTime]::UtcNow
$Metric
Write-CWMetricData -ProfileName AWSCLI -Region ap-southeast-1 -Namespace VPNTunnelStatus -MetricData $Metric
}
}
while ($true)
Re: Question | Powershell script as service
Since services are UI-less, any output from a service will go into the event logs.
Alternatively, you have the option to tell PowerShell to redirect output to a log file.
https://docs.microsoft.com/en-us/powers ... rshell-5.1
Alternatively, you have the option to tell PowerShell to redirect output to a log file.
https://docs.microsoft.com/en-us/powers ... rshell-5.1
David
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.