Avatar of forsters
forsters
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Create Remote Powershell Process from ASP.NET C#

Hi Experts,

We have a powershell script which we run manually once every few months to provide our users with an overview of who has access to who's mailbox & vice-versa:

Get-Mailbox -Server “[OUREXCHANGESERVER]” | Get-MailboxPermission | where { ($_.AccessRights -eq “FullAccess”) -and ($_.IsInherited -eq $false) -and -not ($_.User -like “NT AUTHORITY\SELF”) } | export-csv h:\access1.csv

Open in new window


We then import the results to SQL and display them by user via a web page, so each individual accessing the web page sees their specific mailbox details.

What we would like to try and do now is somehow run the script from the web page so that the whole thing becomes dynamic rather than requiring a manual process every few months.

We use Citrix and would intend the Powershell script to run on the Citrix server using parameters to identify who's who.

Can anyone point us in the direction of where to begin, we have got as far as running a simple powershell cmdlet from within an ASP.NET page (locally in a test environment) following the example given here:
http://devinfra-us.blogspot.co.uk/2011/02/using-powershell-20-from-aspnet-part-1.html
But I think we are now starting to appreciate the complexity of what to do next...some general guidance at this stage would be appreaciated.
ASP.NETPowershellC#

Avatar of undefined
Last Comment
forsters

8/22/2022 - Mon
Meir Rivkin

>>But I think we are now starting to appreciate the complexity of what to do next...some general guidance at this stage would be appreciated.

I run the example from the link you've posted and it seems to work fine.
what problems you encounter applying this in your environment?
forsters

ASKER
Hi sedgwick,

My apologies, there are no problems running the linked example locally on my machine, no that works fine...agreed, but that was merely to illustrate our first step in trying to understand the process of running powershell scripts via ASP.NET. It's getting from that fairly simple example to what we want to do that seems more complex.

As I said, we want the powershell script to run on our Citrix server not locally on an individuals PC, We will need the web page to trigger the running of the script and display the results, we don't neccessarily need to embed the script itself in the C# - we could just reference a ps1 file - it's trying to understand how we would be best to do this, any security / connection issues involved and how we can trigger the script to run remotely on a server but specific to the identity of the user who is logged in and requesting the webpage if that makes sense.
ASKER CERTIFIED SOLUTION
Meir Rivkin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
forsters

ASKER
Many thanks, I will try and tackle one thing at a time:

I suppose the first thing that we would like to understand is the usings - in that example which works for both you and I the instructions say to add:
using System.Management.Automation;
but I note from googling the subject that most suggest the need for two further usings:
using System.Collections.ObjectModel;
using System.Management.Automation.Runspaces;


Some also seem to refer to the use of pipelines - for example:

runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("my script");
Collection<PSObject> output = pipeline.Invoke();

Open in new window


I'm not clear on the implications/ neccesity are you able to advise on those points at all?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
SOLUTION
Meir Rivkin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Coralon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
forsters

ASKER
Thanks to both experts, I have split points and will revisit this later, it was just something we were playing with but other projects are dominating.

Many thanks for your help so far