Problem:
The Set-MailUser throws Exception while renaming the MailUser UserPrincipalName in the C# Code. The code as follows.
string oldUpn="abc@test-mail.com" ;
string newUpn="abc1@test-mail.com" ;
Command command= new Command("Set-MailUser");
command.Parameters.Add("Identity", oldUpn);command.Parameters.Add("MicrosoftOnlineServicesID", newUpn);
command.Parameters.Add("ForceUpgrade");
PowerShell ps= Powershell.Create();
ps.RunSpace=runspace;
ps.Commands.AddCommand(command);
ps.invoke();
Error:
System.Management.Automation.RemoteException: Cannot invoke this function because the current host does not implement it.
at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.Invoke()
Cause:
In the C# Code Command Parameter ForceUpgrade flag value is FALSE by default.
Solution:
Modify the C# Command Paramter code as follows.
Command command= new Command("Set-MailUser");
command.Parameters.Add("Identity", oldUpn);command.Parameters.Add("MicrosoftOnlineServicesID", newUpn);
command.Parameters.Add("ForceUpgrade",Boolean.Parse("true"));
PowerShell ps= Powershell.Create();
ps.RunSpace=runspace;
ps.Commands.AddCommand(command);
ps.invoke();
The issue will be resolved after adding the ForceUpgrade Parameter value is true.