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

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
- Create a new project
- Create a C# Console App
- Next give the project name
- Next select the target framework
- Here I am selecting .NET 6.0 (Long Term Support) framework
- Finally, our solution looks like
- 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();
REFERENCES
- Small Tips and Tricks – Microsoft 365 : SharePoint Online – Managing site storage limit – from SharePoint admin center – https://knowledge-junction.in/2023/01/29/small-tips-and-tricks-microsoft-365-sharepoint-online-managing-site-storage-limit-from-sharepoint-admin-center/
- Small Tips and Tricks – Microsoft 365 : SharePoint Online – Know the total storage / space allocated and used storage for my organization – from SharePoint admin center – https://knowledge-junction.in/2023/02/14/small-tips-and-tricks-microsoft-365-sharepoint-online-know-the-total-storage-space-allocated-and-used-storage-for-my-organization-from-sharepoint-admin-center/
- Small Tips and Tricks – Microsoft 365 : SharePoint Online – Know the total storage / space allocated and used storage for my organization – from PowerShell – https://knowledge-junction.in/2023/02/15/small-tips-and-tricks-microsoft-365-sharepoint-online-know-the-total-storage-space-allocated-and-used-storage-for-my-organization-from-powershell/
- Small Tips and Tricks – PowerShell – Microsoft 365 : SharePoint Online – Getting site collection storage details – https://knowledge-junction.in/2023/02/16/small-tips-and-tricks-powershell-microsoft-365-sharepoint-online-getting-site-collection-storage-details/
- Small Tips and Tricks – PowerShell – Microsoft 365 : SharePoint Online – Getting site collection storage details – https://knowledge-junction.in/2023/02/16/small-tips-and-tricks-powershell-microsoft-365-sharepoint-online-getting-site-collection-storage-details/
You must log in to post a comment.