How to add user(s) to distribution list group in Office 365 using C#?

%3CLINGO-SUB%20id%3D%22lingo-sub-961244%22%20slang%3D%22en-US%22%3EHow%20to%20add%20user(s)%20to%20distribution%20list%20group%20in%20Office%20365%20using%20C%23%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-961244%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20creating%20one%20distribution%20list%20for%20Unsubscriber%20and%20dynamically%20wants%20to%20add%20external%20user(s)%20to%20that%20group%20so%20in%20the%20future%20no%20email%20goes%20to%20those%20users.%20How%20to%20do%20with%20programming%20c%23%3F%20Please%20help%20me%20out%20with%20sample%20code.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-961244%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAdmin%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExchange%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%20Groups%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-962288%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20add%20user(s)%20to%20distribution%20list%20group%20in%20Office%20365%20using%20C%23%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-962288%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F287187%22%20target%3D%22_blank%22%3E%40itsmesri%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can't%20use%20Microsoft%20Graph%20API%20or%20Azure%20AD%20Graph%20to%20add%20members%20in%20a%20Distribution%20Group.%20So%20the%20easiest%20way%20is%2C%20add%20member%20using%20the%20Exchange%26nbsp%3BOnline%20Powershell%20cmdlet%26nbsp%3B%3CA%20title%3D%22Add-DistributionGroupMember%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fexchange%2Fusers-and-groups%2Fadd-distributiongroupmember%3Fview%3Dexchange-ps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAdd-DistributionGroupMember%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20refer%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fwushuai%2F2016%2F09%2F18%2Faccess-exchange-online-by-powershell-in-c%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethis%3C%2FA%3E%20post%20to%20connect%20Exchange%20Online%20Powershell%20module%20and%20run%20commands%20in%20C%23.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-963235%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20add%20user(s)%20to%20distribution%20list%20group%20in%20Office%20365%20using%20C%23%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-963235%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F38365%22%20target%3D%22_blank%22%3E%40Kevin%20Morgan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20using%20sample%20code%20that%20you%20gave%20me.%20But%20command%20fails%20to%20me.%20Any%20suggestions%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Estring%20connectionUri%20%3D%20%22%3CA%20href%3D%22%26lt%3Ba%20href%3D%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%3C%2FA%3E%22%20target%3D%22_blank%22%26gt%3B%3CA%20href%3D%22https%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%26lt%3B%2Fa%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%3C%2FA%3E%26gt%3B%22%3B%0Astring%20loginPassword%20%3D%20ConfigurationManager.AppSettings%5B%22PWD%22%5D.ToString()%3B%0Astring%20loginUser%20%3D%20ConfigurationManager.AppSettings%5B%22UID%22%5D.ToString()%3B%0ASecureString%20secpassword%20%3D%20new%20SecureString()%3B%0Aforeach%20(char%20c%20in%20loginPassword)%0A%7B%0Asecpassword.AppendChar(c)%3B%0A%7D%0APSCredential%20credential%20%3D%20new%20PSCredential(loginUser%2C%20secpassword)%3B%0ARunspace%20runspace%20%3D%20System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()%3B%0APowerShell%20powershell%20%3D%20PowerShell.Create()%3B%0APSCommand%20command%20%3D%20new%20PSCommand()%3B%0Acommand.AddCommand(%22New-PSSession%22)%3B%0Acommand.AddParameter(%22ConfigurationName%22%2C%20%22Microsoft.Exchange%22)%3B%0Acommand.AddParameter(%22ConnectionUri%22%2C%20new%20Uri(connectionUri))%3B%0Acommand.AddParameter(%22Credential%22%2C%20credential)%3B%0Acommand.AddParameter(%22Authentication%22%2C%20%22Basic%22)%3B%0Apowershell.Commands%20%3D%20command%3B%0A%0Arunspace.Open()%3B%0Apowershell.Runspace%20%3D%20runspace%3B%0ACollection%3CSYSTEM.MANAGEMENT.AUTOMATION.PSOBJECT%3E%20result%20%3D%20powershell.Invoke()%3B%0Aif%20(powershell.Streams.Error.Count%20%26gt%3B%200%20%7C%7C%20result.Count%20!%3D%201)%0A%7B%0Athrow%20new%20Exception(%22Fail%20to%20establish%20the%20connection%22)%3B%0A%7D%0A%0Apowershell%20%3D%20PowerShell.Create()%3B%0Acommand%20%3D%20new%20PSCommand()%3B%0Acommand.AddCommand(%22Invoke-Command%22)%3B%0Astring%20DLGroupName%20%3D%20%22UnsubscribersList%22%3B%0Acommand.AddParameter(%22ScriptBlock%22%2C%20System.Management.Automation.ScriptBlock.Create(%22Add-DistributionGroupMember%20-Identity%20%22%20%2B%20DLGroupName%20%2B%20%22%20-Member%20%22%20%2B%20txtEmail.Text))%3B%0Acommand.AddParameter(%22Session%22%2C%20result%5B0%5D)%3B%0Apowershell.Commands%20%3D%20command%3B%0Apowershell.Runspace%20%3D%20runspace%3B%0Aresult%20%3D%20powershell.Invoke()%3B%0Aif%20(powershell.Streams.Error.Count%20%26gt%3B%200%20%7C%7C%20result.Count%20!%3D%201)%0A%7B%0Athrow%20new%20Exception(%22Command%20failed%22)%3B%0A%7D%3C%2FSYSTEM.MANAGEMENT.AUTOMATION.PSOBJECT%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20getting%20this%20error%20while%20running%26nbsp%3B%3CSTRONG%3EAdd-DistributionGroupMember%26nbsp%3B%3C%2FSTRONG%3Ecommand%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%7BCouldn't%20find%20object%20%22test11%40gmail.com%22.%20Please%20make%20sure%20that%20it%20was%20spelled%20correctly%20or%20specify%20a%20different%20object.%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20face%3D%22arial%20black%2Cavant%20garde%22%3E%3CU%3E%3CSTRONG%3EUPDATE%3A%3C%2FSTRONG%3E%3C%2FU%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20add%20email%20address%20to%20contacts%20then%20adding%20to%20distribution%20List%20still%20get%20an%20error.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20using%20(powershell%20%3D%20PowerShell.Create())%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20command%20%3D%20new%20PSCommand()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20command.AddCommand(%22Invoke-Command%22)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20command.AddParameter(%22ScriptBlock%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.Management.Automation.ScriptBlock.Create(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22New-MailContact%20-Name%20'%22%20%2B%20txtEmail.Text%20%2B%20%22'%20-ExternalEmailAddress%20'%22%20%2B%20txtEmail.Text%20%2B%20%22'%22))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20command.AddParameter(%22Session%22%2C%20result%5B0%5D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20powershell.Commands%20%3D%20command%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20powershell.Runspace%20%3D%20runspace%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20result%20%3D%20powershell.Invoke()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(powershell.Streams.Error.Count%20%26gt%3B%200%20%7C%7C%20result.Count%20!%3D%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20throw%20new%20Exception(%22Fail%20to%20establish%20the%20connection%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20using%20(powershell%20%3D%20PowerShell.Create())%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20command%20%3D%20new%20PSCommand()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20command.AddCommand(%22Invoke-Command%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20string%20DLGroupName%20%3D%20%22UnsubscribersList%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20command.AddParameter(%22ScriptBlock%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.Management.Automation.ScriptBlock.Create(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Add-DistributionGroupMember%20-Identity%20'%22%20%2B%20DLGroupName%20%2B%20%22'%20-Member%20%22%20%2B%20txtEmail.Text%20%2B%20%22%22))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20command.AddParameter(%22Session%22%2C%20result%5B0%5D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20powershell.Commands%20%3D%20command%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20powershell.Runspace%20%3D%20runspace%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20result%20%3D%20powershell.Invoke()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(powershell.Streams.Error.Count%20%26gt%3B%200%20%7C%7C%20result.Count%20!%3D%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20throw%20new%20Exception(%22Fail%20to%20establish%20the%20connection%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EError%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ECannot%20convert%20the%20%22test11%40gmail.com%22%20value%20of%20type%20%22Deserialized.Microsoft.Exchange.Data.Directory.Management.MailContact%22%20to%20type%20%22System.Management.Automation.Runspaces.PSSession%22.%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-964992%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20add%20user(s)%20to%20distribution%20list%20group%20in%20Office%20365%20using%20C%23%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-964992%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F287187%22%20target%3D%22_blank%22%3E%40itsmesri%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20are%20facing%20problem%20with%20below%20line%20%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Ecommand.AddParameter(%22Session%22%2C%20result%5B0%5D)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EYou%20have%20to%20store%20%3CSTRONG%3ENew-PSSession%3C%2FSTRONG%3E%20result%20(result%5B0%5D)%20in%20another%20variable%20and%20use%20it%20for%20subsequent%20calls.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Evar%20session%20%3D%20result%5B0%5D%3B%2F%2F%20New-PSSession%20result%0A%2F%2F%20In%20subsequent%20calls%2C%20use%20it%20as%20below%20line.%0Acommand.AddParameter(%22Session%22%2C%20session)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EMy%20complete%20working%20script%20%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Estring%20connectionUri%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%3C%2FA%3E%22%3B%0Astring%20loginUser%20%3D%20ConfigurationManager.AppSettings%5B%22UID%22%5D.ToString()%3B%0Astring%20loginPassword%20%3D%20ConfigurationManager.AppSettings%5B%22PWD%22%5D.ToString()%3B%0ASecureString%20secpassword%20%3D%20new%20SecureString()%3B%0Aforeach%20(char%20c%20in%20loginPassword)%0A%7B%0A%20%20%20%20secpassword.AppendChar(c)%3B%0A%7D%0APSCredential%20credential%20%3D%20new%20PSCredential(loginUser%2C%20secpassword)%3B%0ARunspace%20runspace%20%3D%20System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()%3B%0APowerShell%20powershell%20%3D%20PowerShell.Create()%3B%0APSCommand%20command%20%3D%20new%20PSCommand()%3B%0Acommand.AddCommand(%22New-PSSession%22)%3B%0Acommand.AddParameter(%22ConfigurationName%22%2C%20%22Microsoft.Exchange%22)%3B%0Acommand.AddParameter(%22ConnectionUri%22%2C%20new%20Uri(connectionUri))%3B%0Acommand.AddParameter(%22Credential%22%2C%20credential)%3B%0Acommand.AddParameter(%22Authentication%22%2C%20%22Basic%22)%3B%0Apowershell.Commands%20%3D%20command%3B%0A%0Arunspace.Open()%3B%0Apowershell.Runspace%20%3D%20runspace%3B%0ACollection%3CSYSTEM.MANAGEMENT.AUTOMATION.PSOBJECT%3E%20result%20%3D%20powershell.Invoke()%3B%0Aif%20(powershell.Streams.Error.Count%20%26gt%3B%200%20%7C%7C%20result.Count%20!%3D%201)%0A%7B%0A%20%20%20%20throw%20new%20Exception(%22Fail%20to%20establish%20the%20connection%22)%3B%0A%7D%0A%0Avar%20session%20%3D%20result%5B0%5D%3B%0A%0A%2F%2Fstring%20ExtEmail%20%3D%20%22Test1%40gmail.com%22%3B%0Astring%20ExtEmail%20%3D%20txtEmail.Text%3B%0Ausing%20(powershell%20%3D%20PowerShell.Create())%0A%7B%0A%20%20%20%20command%20%3D%20new%20PSCommand()%3B%0A%20%20%20%20command.AddCommand(%22Invoke-Command%22)%3B%0A%0A%20%20%20%20command.AddParameter(%22ScriptBlock%22%2C%0A%20%20%20%20%20%20%20%20System.Management.Automation.ScriptBlock.Create(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22New-MailContact%20-Name%20'%22%20%2B%20ExtEmail%20%2B%20%22'%20-ExternalEmailAddress%20'%22%20%2B%20ExtEmail%20%2B%20%22'%22))%3B%0A%20%20%20%20command.AddParameter(%22Session%22%2C%20session)%3B%0A%20%20%20%20powershell.Commands%20%3D%20command%3B%0A%20%20%20%20powershell.Runspace%20%3D%20runspace%3B%0A%20%20%20%20result%20%3D%20powershell.Invoke()%3B%0A%20%20%20%20if%20(powershell.Streams.Error.Count%20%26gt%3B%200%20%7C%7C%20result.Count%20!%3D%201)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2Fthrow%20new%20Exception(%22Fail%20command%22)%3B%0A%20%20%20%20%7D%0A%7D%0A%0Ausing%20(powershell%20%3D%20PowerShell.Create())%0A%7B%0A%20%20%20%20command%20%3D%20new%20PSCommand()%3B%0A%20%20%20%20command.AddCommand(%22Invoke-Command%22)%3B%0A%20%20%20%20string%20DLGroupName%20%3D%20%22UnsubscribersList%22%3B%0A%20%20%20%20command.AddParameter(%22ScriptBlock%22%2C%0A%20%20%20%20%20%20%20%20System.Management.Automation.ScriptBlock.Create(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Add-DistributionGroupMember%20-Identity%20'%22%20%2B%20DLGroupName%20%2B%20%22'%20-Member%20%22%20%2B%20ExtEmail%20%2B%20%22%22))%3B%0A%20%20%20%20command.AddParameter(%22Session%22%2C%20session)%3B%0A%20%20%20%20powershell.Commands%20%3D%20command%3B%0A%20%20%20%20powershell.Runspace%20%3D%20runspace%3B%0A%20%20%20%20result%20%3D%20powershell.Invoke()%3B%0A%20%20%20%20if%20(powershell.Streams.Error.Count%20%26gt%3B%200%20)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20throw%20new%20Exception(%22Fail%20command%22)%3B%0A%20%20%20%20%7D%0A%7D%3C%2FSYSTEM.MANAGEMENT.AUTOMATION.PSOBJECT%3E%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-965753%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20add%20user(s)%20to%20distribution%20list%20group%20in%20Office%20365%20using%20C%23%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-965753%22%20slang%3D%22en-US%22%3E%3CP%3E%40Thank%20you%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F38365%22%20target%3D%22_blank%22%3E%40Kevin%20Morgan%3C%2FA%3E%26nbsp%3B%20it%20works.%20I%20am%20able%20to%20add%20to%20group.%20But%20still%20I%20get%20exception%20after%20adding%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EEnumeration%20yielded%20no%20results%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3Banother%20question.%20Is%20it%20possible%20to%20add%20email%20address%20to%20group%20without%20adding%20to%20contacts%3F%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

I am creating one distribution list for Unsubscriber and dynamically wants to add external user(s) to that group so in the future no email goes to those users. How to do with programming c#? Please help me out with sample code. 

3 Replies
Highlighted

@itsmesri 

 

You can't use Microsoft Graph API or Azure AD Graph to add members in a Distribution Group. So the easiest way is, add member using the Exchange Online Powershell cmdlet Add-DistributionGroupMember

 

You can refer this post to connect Exchange Online Powershell module and run commands in C#.

Highlighted

@Kevin Morgan 

 

I tried using sample code that you gave me. But command fails to me. Any suggestions?

 

string connectionUri = "<a href="<a href="https://outlook.office365.com/powershell-liveid/" target="_blank">https://outlook.office365.com/powershell-liveid/</a>" target="_blank"><a href="https://outlook.office365.com/powershell-liveid/</a" target="_blank">https://outlook.office365.com/powershell-liveid/</a</a>>";
string loginPassword = ConfigurationManager.AppSettings["PWD"].ToString();
string loginUser = ConfigurationManager.AppSettings["UID"].ToString();
SecureString secpassword = new SecureString();
foreach (char c in loginPassword)
{
secpassword.AppendChar(c);
}
PSCredential credential = new PSCredential(loginUser, secpassword);
Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("New-PSSession");
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("ConnectionUri", new Uri(connectionUri));
command.AddParameter("Credential", credential);
command.AddParameter("Authentication", "Basic");
powershell.Commands = command;

runspace.Open();
powershell.Runspace = runspace;
Collection<System.Management.Automation.PSObject> result = powershell.Invoke();
if (powershell.Streams.Error.Count > 0 || result.Count != 1)
{
throw new Exception("Fail to establish the connection");
}

powershell = PowerShell.Create();
command = new PSCommand();
command.AddCommand("Invoke-Command");
string DLGroupName = "UnsubscribersList";
command.AddParameter("ScriptBlock", System.Management.Automation.ScriptBlock.Create("Add-DistributionGroupMember -Identity " + DLGroupName + " -Member " + txtEmail.Text));
command.AddParameter("Session", result[0]);
powershell.Commands = command;
powershell.Runspace = runspace;
result = powershell.Invoke();
if (powershell.Streams.Error.Count > 0 || result.Count != 1)
{
throw new Exception("Command failed");
}

 

I am getting this error while running Add-DistributionGroupMember command

 

{Couldn't find object "test11@gmail.com". Please make sure that it was spelled correctly or specify a different object.}

 

 

UPDATE:

 

I tried add email address to contacts then adding to distribution List still get an error.

 using (powershell = PowerShell.Create())
            {
                command = new PSCommand();
                command.AddCommand("Invoke-Command");

                command.AddParameter("ScriptBlock",
                    System.Management.Automation.ScriptBlock.Create(
                        "New-MailContact -Name '" + txtEmail.Text + "' -ExternalEmailAddress '" + txtEmail.Text + "'"));
                command.AddParameter("Session", result[0]);
                powershell.Commands = command;
                powershell.Runspace = runspace;
                result = powershell.Invoke();
                if (powershell.Streams.Error.Count > 0 || result.Count != 1)
                {
                    throw new Exception("Fail to establish the connection");
                }
            }

            using (powershell = PowerShell.Create())
            {
                command = new PSCommand();
                command.AddCommand("Invoke-Command");
                string DLGroupName = "UnsubscribersList";
                command.AddParameter("ScriptBlock",
                    System.Management.Automation.ScriptBlock.Create(
                        "Add-DistributionGroupMember -Identity '" + DLGroupName + "' -Member " + txtEmail.Text + ""));
                command.AddParameter("Session", result[0]);
                powershell.Commands = command;
                powershell.Runspace = runspace;
                result = powershell.Invoke();
                if (powershell.Streams.Error.Count > 0 || result.Count != 1)
                {
                    throw new Exception("Fail to establish the connection");
                }
            }

Error

Cannot convert the "test11@gmail.com" value of type "Deserialized.Microsoft.Exchange.Data.Directory.Management.MailContact" to type "System.Management.Automation.Runspaces.PSSession".

 

Highlighted

@itsmesri 

 

You are facing problem with below line :

command.AddParameter("Session", result[0]);

You have to store New-PSSession result (result[0]) in another variable and use it for subsequent calls.

var session = result[0];// New-PSSession result
// In subsequent calls, use it as below line.
command.AddParameter("Session", session);

My complete working script :

string connectionUri = "<a href="https://outlook.office365.com/powershell-liveid/" target="_blank">https://outlook.office365.com/powershell-liveid/</a>";
string loginUser = ConfigurationManager.AppSettings["UID"].ToString();
string loginPassword = ConfigurationManager.AppSettings["PWD"].ToString();
SecureString secpassword = new SecureString();
foreach (char c in loginPassword)
{
    secpassword.AppendChar(c);
}
PSCredential credential = new PSCredential(loginUser, secpassword);
Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("New-PSSession");
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("ConnectionUri", new Uri(connectionUri));
command.AddParameter("Credential", credential);
command.AddParameter("Authentication", "Basic");
powershell.Commands = command;

runspace.Open();
powershell.Runspace = runspace;
Collection<System.Management.Automation.PSObject> result = powershell.Invoke();
if (powershell.Streams.Error.Count > 0 || result.Count != 1)
{
    throw new Exception("Fail to establish the connection");
}

var session = result[0];

//string ExtEmail = "Test1@gmail.com";
string ExtEmail = txtEmail.Text;
using (powershell = PowerShell.Create())
{
    command = new PSCommand();
    command.AddCommand("Invoke-Command");

    command.AddParameter("ScriptBlock",
        System.Management.Automation.ScriptBlock.Create(
            "New-MailContact -Name '" + ExtEmail + "' -ExternalEmailAddress '" + ExtEmail + "'"));
    command.AddParameter("Session", session);
    powershell.Commands = command;
    powershell.Runspace = runspace;
    result = powershell.Invoke();
    if (powershell.Streams.Error.Count > 0 || result.Count != 1)
    {
        //throw new Exception("Fail command");
    }
}

using (powershell = PowerShell.Create())
{
    command = new PSCommand();
    command.AddCommand("Invoke-Command");
    string DLGroupName = "UnsubscribersList";
    command.AddParameter("ScriptBlock",
        System.Management.Automation.ScriptBlock.Create(
            "Add-DistributionGroupMember -Identity '" + DLGroupName + "' -Member " + ExtEmail + ""));
    command.AddParameter("Session", session);
    powershell.Commands = command;
    powershell.Runspace = runspace;
    result = powershell.Invoke();
    if (powershell.Streams.Error.Count > 0 )
    {
        throw new Exception("Fail command");
    }
}