none
Powershell with C# RRS feed

  • Question

  • 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


    Thursday, September 3, 2015 4:48 AM

Answers

  • Hello,

    For coding and script related questions, please post the question on Exchange Development Forum:

    https://social.technet.microsoft.com/Forums/exchange/en-US/home?forum=exchangesvrdevelopment

    Thanks,

    Simon Wu
    TechNet Community Support

    Thursday, September 3, 2015 6:46 AM
    Moderator