Quantcast
Channel: Exchange Server Development forum
Viewing all articles
Browse latest Browse all 7132

Powershell with C#

$
0
0

Hi,

I am executing powershell commands through c# code. When i run them on Windows powershell those are working fine but when ever i am trying with c# code getting exceptions like

" An unhandled exception of type 'System.Management.Automation.PSInvalidOperationException' occurred in System.Management.Automation.dll "

 powershell1 = PowerShell.Create();
            PSCommand command = new PSCommand();
            command.AddCommand("Set-ExecutionPolicy RemoteSigned");
            command.AddCommand("New-PSSession");
            command.AddParameter("ConfigurationName", "Microsoft.Exchange");
            command.AddParameter("ConnectionUri", psURL);
            command.AddParameter("Credential", creds1);
            command.AddParameter("Authentication", "Basic");
            command.AddParameter("AllowRedirection");

            PSSessionOption sessionOption = new PSSessionOption();
            sessionOption.SkipCACheck = true;
            sessionOption.SkipCNCheck = true;
            sessionOption.SkipRevocationCheck = true;
            command.AddParameter("SessionOption", sessionOption);
            powershell1.Commands = command;

            runspace.Open();

            powershell1.Runspace = runspace;

            Collection<PSSession> result = powershell1.Invoke<PSSession>();

            powershell = PowerShell.Create();
            command = new PSCommand();
            command.AddCommand("Set-Variable");
            command.AddParameter("Name", "ra");
            command.AddParameter("Value", result[0]);
            powershell.Commands = command;
            powershell.Runspace = runspace;
            powershell.Invoke();

            RunspacePool runspacePool = RunspaceFactory.CreateRunspacePool();
            powershell = PowerShell.Create();
            powershell.RunspacePool = runspacePool;
            command = new PSCommand();
            command.AddCommand("Set-ExecutionPolicy RemoteSigned");
            command.AddScript("Import-PSSession");
            command.AddParameter("Session", result[0]);
            command.AddParameter("CommandName", new[] { "Get-Mailbox", "Get-MailboxStatistics" });
            command.AddParameter("ErrorAction ", "SilentlyContinue ");


            powershell.Invoke();

Can anybody tell me what is wrong in my code. When i run the following commands in Windows powershell those are working fine but not in c#. How can i execute same commands through c# for get-mailbox,get-mailboxpermissions,etc

Set-ExecutionPolicy RemoteSigned
$Cred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic –AllowRedirection
Import-PSSession $Session
Get-Mailbox

Please help me to resolve this



Viewing all articles
Browse latest Browse all 7132

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>