Thursday, December 15, 2022

Powershell script to monitor and restart stopped Windows Services and notify users via email

Powershell script to monitor and restart stopped Windows Services. This can be configured to run to monitor multiple windows service.

$winServices = @{"WinServiceName1", "WinServiceName2", "WinServiceName3"}

$recipients = "dl-group@company.com,dl-group2@gmail.com,abc@gmail.com"

[string[]]$To = $recipients.Split(',')

foreach ($service in $winServices){

$s = Get-Service -Name $service

 if($s.status -ne 'Running')

{

Write-Host $s.Name 'is not running. Take action'

Send-MailMessage -From "NO-REPLY@company.com" -To $To -Subject  "$($s.Name) service is stopped" -Body "$($s.Name) service is stopped and needs attention" -SMTPServer "smpt.server.com"


Write-Host 'Restarting:' $s.Name

Start-Service -Name $s.Name

Send-MailMessage -From "NO-REPLY@company.com" -To $To -Subject  "$($s.Name) service is restarted" -Body "$($s.Name) service is has been restarted" -SMTPServer "smpt.server.com"

}

}


Save the above scripts as MonitorServices.ps1

Make a batch file and use Windows Scheduled Task to run it:

@ECHO OFF

cd /d %~dp0

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '.\MonitorServices.ps1'

exit \b %ERRORLEVEL%

Singleton Data configuration with JSON file for connection strings information

 Singleton Data configuration with JSON file for connection strings information

public class SingletonDataConfig

{

private static readonly Lazy<SingletonDataConfig> _config = 

new Lazy<SingletonDataConfig>(() => 

new SingletonDataConfig(), System.Threading.LazyThreadSafetyMode.PublicationOnly);

private IConfiguration DataConfig { get; } = new ConfigurationBuilder().

AddJsonFile("yourDatabaseSettings.json").Build();

public static SingletonDataConfig Instance => _config.Value;

private string _connstringinfo

public string ConnStringInfo {

get {

var conn  = DataConfig.GetConnectionString("YourConnStringName");

string pwd = DataConfig ["DbPassword"].ToString();

SqlConnectionStringBuilder connBuilder = new SqlConnectionsStringBuilder(conn);

builder.Add("Password", pwd);

_connstringinfo = builder.ConnectionString;

}

return _connstringinfo 

}

}

How to reference the above Singleton Class in your connection string?

using (SqlConnection conn = new SqlConnection(SingletonDataConfig.Instance.ConnStringInfo))

{

....

}

Source Code reference for Singelton - https://csharpindepth.com/articles/singleton

Tuesday, December 13, 2022

Powershell script to create new Windows Service

PowerShell script to create new windows service.

$params = @{

     Name = "<WindowsServiceName>"

     BinaryPathName = 'C:\<Your Service Name.exe> -ServiceParams'

     DependsOn = "NetLogon"

     DisplayName = "Your Service Name"

     StartupType = "Manual"

     Description = "<Your service description>"

New-Service @params

$service = Get-WmiObject -Class Win32_Service -Filter "Name='<Service Name>'"

You can also delete this service -$service.delete( )