【问题标题】:The term 'New-CsOnlineSession' is not recognized as the name of a cmdlet术语“New-CsOnlineSession”未被识别为 cmdlet 的名称
【发布时间】:2017-05-25 14:24:28
【问题描述】:

我正在尝试从 c# 运行 power shell 脚本。 仅运行 power shell 脚本时,它运行成功。但是,在尝试从 c# 运行相同的脚本时。我收到错误“‘New-CsOnlineSession’一词未被识别为 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 新手。

【问题讨论】:

  • 您是否尝试过手动将 lync 模块导入该会话?

标签: c# powershell


【解决方案1】:

解决方案

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

#PSTip Cmdlet Discovery and Module auto-loading

【讨论】:

  • 不起作用。 {未加载指定的模块“lynconlineconnector”,因为在任何模块目录中都找不到有效的模块文件。}
【解决方案2】:

我遇到了同样的问题。您必须按照Technet 中的说明安装 Lync/Skype For Business Online 连接器

安装程序会复制 Skype for Business Online 连接器 模块(和 New-CsOnlineSession cmdlet)到您的本地计算机。

【讨论】:

    猜你喜欢
    • 2019-11-13
    • 2014-08-25
    • 1970-01-01
    • 2019-10-12
    • 2017-12-11
    • 2021-12-09
    • 2020-02-25
    • 2020-04-17
    • 2018-09-30
    相关资源
    最近更新 更多