我正在开发一个将部署在Exchange 2013服务器上的C#Web服务.此服务将负责运行powershell命令以配置Exchange.
我通过这样创建的运行空间连接
const string shellUri = "http://schemas.microsoft.com/powershell/microsoft.exchange";
var uri = new Uri(_exchangeConnectionUri);
var credentials = (PSCredential)null; // Windows authentication
var connectionInfo = new WSManConnectionInfo(uri, shellUri, credentials);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
var runspace = RunspaceFactory.CreateRunspace(connectionInfo);
Run Code Online (Sandbox Code Playgroud)
使用此运行空间,我可以在服务器上运行基本的powershell命令.
get-mailbox -ResultSize unlimited
Run Code Online (Sandbox Code Playgroud)
但运行更复杂的命令会给我带来错误(如果直接通过powershell运行,这确实有效)
get-mailbox -ResultSize unlimited | where {$_.emailaddresses -like "*test.com"}
At line:1 char:43
+ get-mailbox -ResultSize unlimited | where {$_.emailaddresses -like "*test.com ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Script block literals are not allowed in restricted language mode or a Data section.
At line:1 char:44
+ …Run Code Online (Sandbox Code Playgroud) 我试图找出使用异步等待通过实体框架选择数据的最佳方法.在以下代码中,我有两个选项.
第一个使用Task.FromResult.
第二个选项尝试使用async await关键字来优化代码.
1.
public Task<IEnumerable<DesignExample>> ExecuteAsync(GetAllExamplesQuery query)
{
var designExamples = _copyDataDb.DesignExamples.OrderBy(dE => dE.Order).Select(DesignExample.MapFromEntity);
return Task.FromResult(designExamples);
}
Run Code Online (Sandbox Code Playgroud)
2.
public async Task<IEnumerable<DesignExample>> ExecuteAsync(GetAllExamplesQuery query)
{
var designExamples = await _copyDataDb.DesignExamples.OrderBy(dE => dE.Order).ToListAsync();
return designExamples.Select(DesignExample.MapFromEntity);
}
Run Code Online (Sandbox Code Playgroud)