Hi all,
I am using Exchanging server 2013.
Please see code below is not working after published in IIS. In locally it is working fine.
I think the problem is here I am accessing Powershell script file in C# code . powershell files[.ps1] was unable to identify by IIS. so i add new MIME type for .ps1 file extension.
but still no use.
In the below code , please see the path string file = "D:/Scripts/listcmd.ps1";
my Remaining code is working fine in IIS , if i Comment below code . so i think the problem is here
please help me
var runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); object psSessionConnection; // Create a powershell session for remote exchange server using (var powershell = PowerShell.Create()) { var command = new PSCommand(); command.AddCommand("New-PSSession"); command.AddParameter("ConfigurationName", "Microsoft.Exchange"); command.AddParameter("ConnectionUri", new Uri("http://ExchangeServer.admin.com/powershell/Microsoft.Exchange")); command.AddParameter("Authentication", "Kerberos"); powershell.Commands = command; powershell.Runspace = runspace; // TODO: Handle errors var result = powershell.Invoke(); psSessionConnection = result[0]; } // Set ExecutionPolicy on the process to unrestricted using (var powershell = PowerShell.Create()) { var command = new PSCommand(); command.AddCommand("Set-ExecutionPolicy"); command.AddParameter("Scope", "Process"); command.AddParameter("ExecutionPolicy", "Unrestricted"); powershell.Commands = command; powershell.Runspace = runspace; powershell.Invoke(); } // Import remote exchange session into runspace using (var powershell = PowerShell.Create()) { var command = new PSCommand(); command.AddCommand("Import-PSSession"); command.AddParameter("Session", psSessionConnection); powershell.Commands = command; powershell.Runspace = runspace; powershell.Invoke(); } String file = "D:/Scripts/listcmd.ps1"; // here i am accessing .ps1 file Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.AddScript(file); Collection<PSObject> results = pipeline.Invoke(); runspace.Close(); foreach (PSObject obj in results) { var name = obj.Members["Identity"].Value.ToString(); }