PowerShell – How to call / execute PowerShell script from .NET application

.NET application to execute PowerShell script
.NET application to execute PowerShell script

Hi All,

Greetings for the day!!!

Today I learned something new so sharing 🙂

Background / Details

  • We are implementing SharePoint online governance
  • We are automating our tenant Site collections storage quota
  • So we have written PowerShell script which went through all Site Collections in tenant, verifies whether storage quota warning level is crossed with specific percentage (configured in configuration list) or not
  • If Site Collection level warning level is crossed then increment the storage quota by specific percentage (again configurable value in configuration list)
  • We have detailed articles for SharePoint Online storage quota governance – Please have a look at references
  • Since we need to automate this process – we need to schedule the PowerShell script
  • But for certain reason, our team agreed to schedule application (executable) rather PowerShell
  • We need to write .NET application and call to PowerShell script
  • Schedule the respective application

Prerequisites

  • Visual Studio (I am using Visual Studio 2022 community edition for demo purpose)

Steps to call / executing PowerShell script from .NET application

  • Open Visual Studio – Run as administrator
fig : Visual Studio 2022 - Run as administrator
fig : Visual Studio 2022 – Run as administrator
  • Create a new project
fig : Visual Studio 2022 - creating a new project
fig : Visual Studio 2022 – creating a new project
  • Create a C# Console App
fig : Visual Studio 2022 - Creating C# Console App
fig : Visual Studio 2022 – Creating C# Console App
  • Next give the project name
  • Next select the target framework
  • Here I am selecting .NET 6.0 (Long Term Support) framework
fig : Visual Studio 2022 - Selecting .NET framework
fig : Visual Studio 2022 – Selecting .NET framework
  • Finally, our solution looks like
fig : Visual Studio 2022 - created C# console application to call PowerShell
fig : Visual Studio 2022 – created C# console application to call PowerShell
  • To execute our PowerShell we will use ProcessStartInfo class as
//ProcessStartInfo class - This class is used to specify set of values when we start the process
//OR when we use the Process class - https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo?view=net-7.0

            ProcessStartInfo process_start_info = new ProcessStartInfo();

  • ProcessStartInfo class is available in “System.Diagnostics” namespace
  • We will specify “powershell.exe” as FileName property of ProcessStartInfo instance as
            
            process_start_info.FileName = "powershell.exe";

  • And as argument property is our PowerShell script path
            process_start_info.Arguments = "\"& \"\"" + _Script_Path + "\"\"\""; 

  • We will then use Process class to start the PowerShell process as
            //Process class is used start or stop local and remote processes
            //https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=net-7.0 

            Process process = new Process();
            
            //specifying ProcessStartInfo instance
            process.StartInfo = process_start_info;
            process.Start();
            process.WaitForExit();

fig : Visual Studio 2022 - C# console application to call PowerShell script
fig : Visual Studio 2022 – C# console application to call PowerShell script

REFERENCES

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: