Я пытаюсь использовать C # для подключения к обмену и возврата списка почтовых ящиков для определенного пользователя.Я могу запустить это локально в PowerShell, однако в контексте сценария он не показывает то, что я ожидаю.
Я вижу выходные данные, такие как tmp_1qf14mc3.wiv или tmp_ra1gbenl.ols вместо списка почтовых ящиков.
Дополнительные примечания:
- Я установил ссылку на пространство имен System.Management.Automation и импортировал его;
- I'mВыполнение этого в Visual Studio 2017 с использованием сборки IIS Express
- При этом используется проект .NET Core API
.
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" };