0

New to Exchange Management.

Our .NET Web App is hosted in IIS. IIS Application Pool Identity is having permissions to establish the connection.

Before Migration: Exchange Management Shell 2013 , Windows Server 2012.

After Migration: Exchange Management Shell 2019, Windows Server 2022.

We are connecting to the Exchange Server and adding/removing the members from the Distribution List from our code base as below.

Case 1:

public class ExchangeCmdletHepler 
{
    Runspace m_runspace = null;

    public ExchangeCmdletHepler()
    {
                m_runspace = RunspaceFactory.CreateRunspace();
                m_runspace.Open();
                
                connectToExchange();
    }

    private void connectToExchange()
    {
                Pipeline pipeline = m_runspace.CreatePipeline();
                
                pipeline.Commands.AddScript(". 'C:\Program Files\Microsoft\Exchange Server\V15\Bin\RemoteExchange.ps1'; Connect-ExchangeServer <Exchange Server Here> ");            

                pipeline.Invoke();
    }

    public void AddDistributionGroupMember(Object Identity, Object Member, Object DomainController)
    {
        Command cmd = new Command("Add-DistributionGroupMember");           
        cmd.Parameters.Add("Member", Member);
       
        Pipeline pipline = m_runspace.CreatePipeline();
        pipline.Commands.Add(cmd);

        Collection<PSObject> resList = pipline.Invoke();
        //process result of resList;
    }
    
    public void RemoveDistributionGroupMember(Object Identity, Object Member, Object DomainController)
    {
        Command cmd = new Command("Remove-DistributionGroupMember");
        cmd.Parameters.Add("Member", Member);
        
        Pipeline pipline = m_runspace.CreatePipeline();
        pipline.Commands.Add(cmd);
        
        Collection<PSObject> resList = pipline.Invoke();
        //process result of resList;
    }
    
}

If I invoke the AddDistributionGroupMember(), then I am getting error as The term 'Add-DistributionGroupMember' is not recognized as the name of a cmdlet, function, script file, or operable program.

Like wise, If I invoke the RemoveDistributionGroupMember() then I am getting error as, "The term 'Remove-DistributionGroupMember' is not recognized as the name of a cmdlet, function, script file, or operable program."

Likewise few more commands like New-DistributionGroup,Set-DistributionGroup,Get-PSSession are not working in Case 1.

Case 2:

But if I establis the Exchange Server connection as below, then I got the response without any issue for AddDistributionGroupMember() and RemoveDistributionGroupMember()

public class ExchangeCmdletHepler 
{
    Runspace m_runspace = null;

    public ExchangeCmdletHepler()
    {
        //As in Case 1              
    }

    private void connectToExchange()
    {
        var pipleLine = m_runspace.CreatePipeline();

        pipleLine.Commands.AddScript(
          String.Format("$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<ExchangeServerHere>/PowerShell/", exchangeHost)
          );
        pipleLine.Commands.AddScript("Import-PSSession -Session $Session -AllowClobber");

        pipleLine.Invoke();
    }

    public void AddDistributionGroupMember(Object Identity, Object Member, Object DomainController)
    {
        Command cmd = new Command("Add-DistributionGroupMember");           
        //As in Case 1
    }
    
    public void RemoveDistributionGroupMember(Object Identity, Object Member, Object DomainController)
    {
        Command cmd = new Command("Remove-DistributionGroupMember");
        //As in Case 1
    }
}
    

Case 3:

But, if I execute any of the above commands (Ex: Add-DistributionGroupMember, Remove-DistributionGroupMember, New-DistributionGroup) in the "Exchange Management Shell" then I am getting the response without any issue.

I could see that, Exchange Management Shell -> Properties -> Target, it is pointing to

" C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer {{{ServerHere}}} -ClientApplication:ManagementShell -Prefix Prompt "
"

  1. I am confused because, Case 1 C# Code and Case 3 Exchange Powershell is using same to same command to connect. If the commands are executed in the Exchange Powershell then why it is not executed in my Case 1 C# Scenario?

  2. The Case 1 code base working fine before migration (in Exchange Server 2013). But, it is not working after migration (in Exchange Server 2019)

Could some one share some inputs here.

Thanks.

NANDAKUMAR THANGAVELU
  • 611
  • 3
  • 15
  • 34

0 Answers0