我想使用C#进行连接以交换并返回特定用户的邮箱列表。我可以在PowerShell中本地运行它,但是在脚本的上下文中,它没有显示我的期望。
我看到的是tmp_1qf14mc3.wiv或tmp_ra1gbenl.ols之类的输出,而不是邮箱列表。
其他说明:
。
using (PowerShell PowerShellInstance = PowerShell.Create())
{
// Attempt to get the mailbox listing for the user
PowerShellInstance.AddScript(
"$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList " + user + ",(ConvertTo-SecureString -String " + pass + " -AsPlainText -Force); " +
"$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $credential -Authentication Basic -AllowRedirection; " +
"Import-PSSession $Session -DisableNameChecking; " +
"Get-Mailbox; " +
"Remove-PSSession $Session");
// Invoke execution to collect the output
Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
// Go through each output returned
foreach (PSObject outputItem in PSOutput)
{
// Dump the output to see what's coming out.
if (outputItem != null)
{
return new string[] { outputItem.ToString() };
}
}
}
return new string[] { "Did not get a powershell outputItem" };