C# Office 365:使用Power Shell脚本连接到Office Online 365服务问题

C# Office 365:使用Power Shell脚本连接到Office Online 365服务问题,c#,sharepoint,powershell,cmdlets,C#,Sharepoint,Powershell,Cmdlets,我有Windows Server 2008 R2机器,它有Power Shell v1.0。我想使用带C#的Power Shell连接到MS 365在线服务。我已安装Office 365 cmdlet和Microsoft Online Services登录助手。(参考号:) 我的剧本是: $password = ConvertTo-SecureString "xxxxx" -AsPlainText –Force $credential = New-Object System.Managemen

我有Windows Server 2008 R2机器,它有Power Shell v1.0。我想使用带C#的Power Shell连接到MS 365在线服务。我已安装Office 365 cmdlet和Microsoft Online Services登录助手。(参考号:)

我的剧本是:

$password = ConvertTo-SecureString "xxxxx" -AsPlainText –Force

$credential = New-Object System.Management.Automation.PsCredential("xxxx@xxxx.onmicrosoft.com",$password)

$cred = Get-Credential -cred $credential

Import-Module MSOnline

Connect-Msolservice -cred $cred
我可以在powershell命令窗口中成功运行此脚本。但我在c#应用程序中运行此脚本时遇到问题

这是我的c#代码:

public void RunScript()
{
StringBuilder ss=新的StringBuilder();
ss.AppendLine(“$password=converttosecurestring\”+pwd+“\”-AsPlainText-Force”);
ss.AppendLine($credential=New Object System.Management.Automation.PsCredential(\“”+userName+“\”,$password)”);
ss.AppendLine($cred=Get-Credential-cred$Credential”);
ss.AppendLine(“导入模块MSOnline”);
ss.AppendLine(“连接Msolservice-cred$cred”);
使用(Runspace Runspace=RunspaceFactory.CreateRunspace())
{
收集结果=空;
尝试
{
Open();
Pipeline Pipeline=runspace.CreatePipeline();
pipeline.Commands.AddScript(ss.toString());
结果=pipeline.Invoke();
}
最后
{
runspace.Close();
}                
}
}
我得到以下例外情况:

术语“Connect-Msolservice”不能识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试

少了什么吗


谢谢

请确保您已安装以下设备:

 1. SharePoint Online Management Shell
 2. Microsoft Online Services Sign-In Assistant version 7.0 or greater version
 3. Windows Azure Active Directory Module

谢谢你的评论。我也会尝试你的建议。但是,我找到了它无法识别“Connect Msolservice”命令的原因。原因很简单。我创建了一个控制台应用程序,其平台目标是x86,而我的开发机器是Microsoft Server 2008 64位,我在其中安装了MS Online Service module(64位)。通过将平台目标更改为任何cpu来解决问题。嗨,David,我尝试了上述解决方案,但结果相同(没有成功)。顺便说一句,MS Online service module(64位)的cmdlet似乎必须从64位进程/应用程序执行/运行。这就是我所经历的。如果你对此有任何想法,请分享。谢谢分享,Prakash。我遇到了同样的问题。我遇到了问题,然后,在我的64位Windows 10计算机上,在项目属性中,我将Build->Platform Target设置为x64,脚本成功导入了模块。
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { "MSOnline" });

using (Runspace runspace = RunspaceFactory.CreateRunspace(iss))
{
// blah
}
 1. SharePoint Online Management Shell
 2. Microsoft Online Services Sign-In Assistant version 7.0 or greater version
 3. Windows Azure Active Directory Module