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( )

Thursday, November 17, 2022

CS6515 - My Experience with Gerogia Tech's - Intro to Graduate Algorithms course

4 fundamental topics of the course. If you understand these 4, you will pass the course.

  • Dynamic Programming
  • Divide and Conquer
  • Graph Theory
  • NP Reductions
Key is to do all home work assignments practice problems and practice problems from DPV book.
This course is highly theoretical in nature. Course can be extremely challenging for a person new to Computer science. Hard work and practice will see you through. 

Three exams weight 75% of your grades. Remaining are Home work assignments, Coding assignments and Polls. Together they form 25% of your grade. So getting 20-25% should be doable. 

Sunday, October 10, 2021

New Minimum Spanning Tree

 \textbf{Step 1:} Take the input graph $G$ such that $v$ number of vertices and $e$ number of edges.

\newline \newline

\textbf{Step 2:} Take the input graph $F$ that is a subgraph of $G$.

\newline \newline

\textbf{Step 3:} Initialize an array $result$ to store the minimum spanning tree $(mst)$.

\newline \newline

\textbf{Step 4:} We then iterate through all the edges of the graph $G$ [$for$ $e \in G$], check if the current edge is in the subgraph $F$ (if $e \in F$)

\newline \newline

\textbf{Step 5:} If the condition is true, then add the edge into the $result$ array and delete the edge from $G$.

\newline \newline

\textbf{Step 6: }Repeat steps 4 and 5 until all the edges of $G$ are visited.

\newline \newline

\textbf{Step 7:} We then run Kruskal's algorithm on $G$.

\newline \newline

\textbf{Step 8:} We then sort remaining edges in $G$ in ascending order based on the weight of the edge.

\newline \newline

\textbf{Step 9: }Pick the smallest edge from $G$ and check whether it forms a cycle with already existing edges in the $result$ array. If the cycle is not formed, then add it to the $result$ array. Otherwise, delete it.

\newline \newline

\textbf{Step 10: }Repeat step 9 until there are $(v-1)$ edges in the minimum spanning tree.

\newline \newline

\textbf{Correctness of the algorithm - }We want to include all the edges of the subgraph $F$ in the resulting minimum spanning tree. So, based on above steps we first, we first add all the edges of the subgraph $F$ and then apply Kruskal's algorithm on the remaining edges of graph $G$ to find the minimum spanning tree.

\newline \newline

\textbf{Time Complexity - } 

Step 4 - Time complexity for iteration of all edges of graph $G$ is $\mathcal{O}(e)$ \newline

Step 7 - Time complexity of Kruskal's algorithm is $\mathcal{O}(|E| \log |V|)$, \newline

Step 8 - Time complexity of Sorting remaining edges is $\mathcal{O}(|E| \log |E|)$ \newline

Step 9 - Time complexity for picking smallest edge and repeating step is $\mathcal{O}(|E| \log |V|)$ \newline

\newline \newline

\textbf{Total Time Complexity} is $\mathcal{O}(e)$ + $\mathcal{O}(|E| \log |V|)$ + 

$\mathcal{O}(|E| \log |E|)$ + $\mathcal{O}(|E| \log |V|)$ = \textbf{$\mathcal{O}(|E| \log |E|)$}