Hi Guys,
1) Below is my powershell script:
#Accept input parameters
Param(
[Parameter(Position=0, Mandatory=$false, ValueFromPipeline=$true)]
[string] $Office365Username,
[Parameter(Position=1, Mandatory=$false, ValueFromPipeline=$true)]
[string] $Office365Password,
[string]$GroupName
)
#Remove all existing Powershell sessions
Get-PSSession | Remove-PSSession
#Did they provide creds? If not, ask them for it.
if (([string]::IsNullOrEmpty($Office365Username) -eq $false) -and ([string]::IsNullOrEmpty($Office365Password) -eq $false))
{
$SecureOffice365Password = ConvertTo-SecureString -AsPlainText $Office365Password -Force
#Build credentials object
$Office365Credentials = New-Object System.Management.Automation.PSCredential $Office365Username, $SecureOffice365Password
}
else
{
#Build credentials object
$Office365Credentials = Get-Credential
}
#Create remote Powershell session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Office365Credentials -Authentication Basic -AllowRedirection
#Import the session
Import-PSSession $Session
#cmds
$result=Get-DistributionGroup -ResultSize Unlimited
return $result
#Clean up session
Remove-PSSession $Session
2) Below is my C# code
public string GetAllGroups()
{
int count = 1;
string getDListScript = @"C:\inetpub\wwwroot\O365Service\Scripts\GetDList.ps1";
string userName = "j*****";
string password = "****";
try
{
using (var ps = PowerShell.Create())
{
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
ps.Runspace = runSpace;
ps.AddCommand(getDListScript).AddParameter("Office365Username", userName).AddParameter("Office365Password", password);
//IAsyncResult async = ps.BeginInvoke();
//StringBuilder stringBuilder = new StringBuilder();
var results = ps.Invoke();
PSDataCollection<ErrorRecord> errors = ps.Streams.Error;
if (errors != null && errors.Count > 0)
{
StringBuilder sb = new StringBuilder();
foreach (ErrorRecord err in errors)
{
sb.Append(err.ToString());
}
System.IO.File.WriteAllText(@"C:\inetpub\wwwroot\RestService\bin\err.text", sb.ToString());
}
count = results.Count;
}
}
catch (Exception ex)
{
return ex.Message.ToString();
}
return count.ToString();
}
When i am executing the C# code on server from Console application it is working fine but when i am executing it from Webservice it is giving me the below exception
[outlook.office365.com] Connecting to remote server outlook.office365.com failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.Cannot validate argument on parameter 'Session'. The argument is null. Provide a valid value for the argument, and then try running the command again.The term 'Get-DistributionGroup' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I have set execution policy to unrestricted of the server.
Please Help me on this.
Regards,
Samira