How can I catch the Powershell exceptions AccessDeniedException
and GroupExistsException
within my C# program for the cmdlet New-LocalGroup
try
{
PowerShell ps = PowerShell.Create();
ps.AddCommand("New-LocalGroup");
ps.AddParameter("Name", groupName);
ps.AddParameter("Description", description);
ps.Invoke();
}
catch (Exception ex)
{
if (ex is ?)
throw ex;
}
So far, I have this link from Microsoft documentation here.