Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell script returns nothing on IIS execution

Tags:

c#

powershell

I need help with a problem I've been trying to solve for a week.

I have a PowerShell script that returns a list os users from Office365 environment.

If I run the script from my PowerShell console (my machine), the script returns the expected data. If I run the script from Server (W2008 R2 SP1) PowerShell console, the script returns the expected data. If I debug my C# WebService code (from where script is executed) in my machine, the srcipt returns the expected data.

PROBLEM: If I call to the WebService hosted into the Server, the script returns NOTHING.

Code:

RunspaceConfiguration runspcConfig = RunspaceConfiguration.Create();
Runspace run = RunspaceFactory.CreateRunspace(runspcConfig);
run.ApartmentState = System.Threading.ApartmentState.STA;
run.ThreadOptions = PSThreadOptions.UseCurrentThread;
run.Open();
Pipeline pipeline = run.CreatePipeline();
Command cmd = new Command("PathScript.ps1");
cmd.Parameters.Add(new CommandParameter("DOMAIN", "domain.onmicrosoft.com"));
cmd.Parameters.Add(new CommandParameter("PRODUCT", "productReference"));
pipeline.Commands.Add(cmd);
Collection<PSObject> users=pipeline.Invoke();

Finally found the error: The type initializer for 'Microsoft.Online.Administration.Automation.ConnectMsolService' threw an exception.

The user and password are right, IUSR and "ApplicationPool user" are included in Administrator group.

Any idea?

like image 500
user2806842 Avatar asked Sep 23 '13 10:09

user2806842


1 Answers

By OP,

Problem solved: "Load User Profile" to true in IIS application pool advanced settings

like image 166
rae1 Avatar answered Nov 13 '22 23:11

rae1