术语 'New-CsOnlineSession' 未被识别为 cmdlet 的名称
The term 'New-CsOnlineSession' is not recognized as the name of a cmdlet
我正在尝试 运行 来自 c# 的强大 shell 脚本。
当仅 运行 启用强大的 shell 脚本时,它 运行 成功。但是,在尝试 运行 来自 c# 的相同脚本时。我收到错误 "The term 'New-CsOnlineSession' is not recognized as the name of a cmdlet"
代码如下:
public static void GetLyncUsers(string userName, string password)
{
using (PowerShell powerShellInstance = PowerShell.Create())
{
var script = string.Format("$Username =\"{0}\"\n" +
"$Password =\"{1}\"\n" +
"$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force\n" +
"$cred = new-Object System.Management.Automation.PSCredential ($Username , $secpasswd)\n" +
"$CSSession = New-CsOnlineSession -Credential $cred\n" +
"Import-PSSession $CSSession -AllowClobber\n" +
"Get-CsOnlineUser", userName, password);
// use "AddScript" to add the contents of a script file to the end of the execution pipeline.
// use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
powerShellInstance.AddScript(script);
// use "AddParameter" to add a single parameter to the last command/script on the pipeline.
// invoke execution on the pipeline (collecting output)
Collection<PSObject> psOutput = powerShellInstance.Invoke();
// check the other output streams (for example, the error stream)
if (powerShellInstance.Streams.Error.Count > 0)
{
// I am getting this error
//The term 'New-CsOnlineSession' is not recognized as the name of a cmdlet
}
}
有什么我遗漏的吗? powershell 总体上是新手
解决方案:
using (PowerShell powerShellInstance = PowerShell.Create())
{
// Import-Module lynconlineconnector
powershellInstance.Commands
.AddCommand("Import-Module")
.AddArgument("lynconlineconnector");
// rest of your code ....
为什么?
当 运行 在 powershell v3 及更高版本中进行交互式会话时,主机会捕获 CommandNotFound,并在所有已知位置搜索每个模块。如果找到命令,它会自动加载模块,并正常进行。
当 运行 在 C# 中使用相同的脚本时,不会捕获 CommandNotFound 异常,因此会出现错误。
相关问题:
PowerShell - How to Import-Module in a Runspace
我运行遇到了同样的问题。您必须按照 Technet
中的说明安装 Lync/Skype For Business Online Connector
The setup program copies the Skype for Business Online Connector
module (and the New-CsOnlineSession cmdlet) to your local computer.
我正在尝试 运行 来自 c# 的强大 shell 脚本。 当仅 运行 启用强大的 shell 脚本时,它 运行 成功。但是,在尝试 运行 来自 c# 的相同脚本时。我收到错误 "The term 'New-CsOnlineSession' is not recognized as the name of a cmdlet"
代码如下:
public static void GetLyncUsers(string userName, string password)
{
using (PowerShell powerShellInstance = PowerShell.Create())
{
var script = string.Format("$Username =\"{0}\"\n" +
"$Password =\"{1}\"\n" +
"$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force\n" +
"$cred = new-Object System.Management.Automation.PSCredential ($Username , $secpasswd)\n" +
"$CSSession = New-CsOnlineSession -Credential $cred\n" +
"Import-PSSession $CSSession -AllowClobber\n" +
"Get-CsOnlineUser", userName, password);
// use "AddScript" to add the contents of a script file to the end of the execution pipeline.
// use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
powerShellInstance.AddScript(script);
// use "AddParameter" to add a single parameter to the last command/script on the pipeline.
// invoke execution on the pipeline (collecting output)
Collection<PSObject> psOutput = powerShellInstance.Invoke();
// check the other output streams (for example, the error stream)
if (powerShellInstance.Streams.Error.Count > 0)
{
// I am getting this error
//The term 'New-CsOnlineSession' is not recognized as the name of a cmdlet
}
}
有什么我遗漏的吗? powershell 总体上是新手
解决方案:
using (PowerShell powerShellInstance = PowerShell.Create())
{
// Import-Module lynconlineconnector
powershellInstance.Commands
.AddCommand("Import-Module")
.AddArgument("lynconlineconnector");
// rest of your code ....
为什么?
当 运行 在 powershell v3 及更高版本中进行交互式会话时,主机会捕获 CommandNotFound,并在所有已知位置搜索每个模块。如果找到命令,它会自动加载模块,并正常进行。
当 运行 在 C# 中使用相同的脚本时,不会捕获 CommandNotFound 异常,因此会出现错误。
相关问题:
PowerShell - How to Import-Module in a Runspace
我运行遇到了同样的问题。您必须按照 Technet
中的说明安装 Lync/Skype For Business Online ConnectorThe setup program copies the Skype for Business Online Connector module (and the New-CsOnlineSession cmdlet) to your local computer.