Posts

Running Powershell Command Remotely Using C#

Image
C# has some really awesome nuget package if you want to run the PowerShell script or run the command. I am talking about the nuget package "Microsoft.PowerShell.SDK". Now running the batch file or any command or any executable from the local machine is quite easy and simple. You just need to create the PowerShell object, add the script you want to execute and invoke it. Above code is simple and self explanatory. But lately in my project we had a requirement in which we need to execute the command on the remote machine and fetch the output. It's as good as you are remoting to that machine and executing the command on that machine and fetching the output. Now you would be surprised to know that, you can actually achieve that using the PowerShell SDK and with only few lines of code you can create the magic. PowerShell SDK provides something called as "RunSpace". When you create the RunSpace object you need to provide the machine details and corresponding credential

Deploying Asp.net Core Background Service on IIS

Image
The main purpose of having Background Service within Asp.Net core web api is to make sure that once we start the application pool than Background Service should start immediately. But in general the design of IIS is such that unless you receive the first request worker process is never started and hence background service won't start automatically. So basically to achieve that we need to do some config changes on IIS which will help to start the background service automatically. Below are the steps: Firstly we need to make sure that "Application Initialization" is installed. If not you can add it via "Add Roles and Features Wizard". Once installed make sure you restart you server. Deploy your Asp.Net Core Web Api which has Background Service implemented. Go to "Application Pools" in IIS and select the corresponding application pool. Right Click on it and select "Advanced Settings" and you will see the below screen. Make sure the "Start M

Map IIS Worker Process to PID

Image
Once you deploy your application in IIS, its bit difficult to find the corresponding PID of the application pool if you want to see which worker process is taking more memory. Generally in the Task Manager it will just show the worker process (w3wp.exe) and through task manager its difficult to say which Application Pool is taking more memory. But there is way to identify that through IIS Manager. You can view the Worker Process assignments by going into the Worker Processes section of the server. Once you click the "Worker Processes", it will show all the PIDs mapped with corresponding application pool. Sleeping or Suspended application pool will not be visible. As you can see above, IIS listed all the PID and now I can check in the Task Manager for the corresponding memory details.