Kna*_*aks 5 c# powershell office365
我正在尝试使用C#中集成的Power Shell获取有关用户邮箱的信息,但出现以下错误:
This syntax is not supported by this run space. This might be because it is no-language mode.
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的代码:
using System;
using System.Collections.ObjectModel;
using System.Collections;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Threading;
using System.Globalization;
namespace Office365
{
class Program
{
static void Main(string[] args)
{
CultureInfo oldCI = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
System.Security.SecureString secureString = new System.Security.SecureString();
string myPassword = "mySecretPassword";
foreach (char c in myPassword)
secureString.AppendChar(c);
PSCredential credential = new PSCredential("my@maildomain.com", secureString);
Console.WriteLine("Forbinder....");
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://ps.outlook.com/PowerShell-LiveID?PSVersion=2.0"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
connectionInfo.SkipCACheck = true;
connectionInfo.SkipCNCheck = true;
string cmd2 = @"Get-Mailbox | Select-object Identity, displayname, ProhibitSendReceiveQuota, @{n='size';e={$MBXSTAT=Get-MailboxStatistics $_.Identity; $MBXSTAT.TotalItemSize}}";
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
using (Pipeline pipeline = runspace.CreatePipeline(cmd2))
{
pipeline.Commands.AddScript(cmd2);
Collection<PSObject> results = pipeline.Invoke();
foreach (PSObject obj in results)
{
// Do something with each result object
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?我如何克服这个问题?
通常我喜欢让死者一个人呆着,但在这种情况下,我觉得有必要重新发布这篇旧帖子,因为我遇到了确切的问题,需要一个地方来保存此代码,并希望这可以防止其他人浪费太多时间。
我发现连接到 O365 有WSManConnectionInfo
问题,在浪费了太多时间试图让它工作之后,我不会使用它,除非我别无选择。
也就是说,以下代码对我有用,其行为与我打开 PowerShell 提示符并输入我要运行的命令一样:)它使测试变得更加简单。
using (var _power_shell = PowerShell.Create()) {
_power_shell.AddScript("Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process");
_power_shell.Invoke();
var credential = new PSCredential(username, password.ToSecureString());
_power_shell.Runspace.SessionStateProxy.SetVariable("Credential", credential);
_power_shell.AddScript("$Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential
$Credential -Authentication Basic -AllowRedirection");
_power_shell.AddScript("Import-PSSession $Session");
_power_shell.Invoke();
_power_shell.AddScript("Get-Mailbox -RecipientTypeDetails RoomMailbox | Select-Object DisplayName, PrimarySmtpAddress");
var results = _power_shell.Invoke();
foreach (var obj in results)
{
/* Do something with the results */
}
_power_shell.AddScript("Remove-PSSession $Session");
_power_shell.Invoke();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码假定username
&password
作为已经在范围内的字符串变量。还有一个扩展方法ToSecureString
存在并且在范围内。
据我所知,PowerShell.Creste()
提供了一个设置为 Restricted 的执行环境,所以你首先需要改变它。在这种情况下,因为我们将范围限定为Set-ExecutionPolicy
不需要管理员权限的进程。
从我在该主题上发现的一点点来看,您似乎需要在完成后删除会话,否则您可能会受到限制并将自己锁定在 O365 之外一段时间。