【问题标题】:Use Connect-SPOService with Powershell 6 (core version)将 Connect-SPOService 与 Powershell 6(核心版本)一起使用
【发布时间】:2019-12-17 14:55:58
【问题描述】:

我正在尝试连接到共享点环境,并且我想使用 Powershell 版本 6 进行连接。为什么?最终,我想将 PS 命令放入 .net core 3 应用程序中。据我所知,我不能在 .net core 中使用 PS5.1。

关于这个 powershell 脚本:

Import-Module -Force -name Microsoft.Online.SharePoint.PowerShell;
Import-Module -Force -name Microsoft.Online.SharePoint.PowerShell -DisableNameChecking;
$username = 'admin@shootme.com';
$password = 'right now';
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $Password -asplaintext -force);
Connect-SPOService -Url https://shootme.sharepoint.com -Credential $cred;

当我在默认的 PS 5.1 中尝试此操作时,它运行良好。当我在 PS 6.2.3 上尝试此操作时,出现错误:

Connect-SPOService : The remote server returned an error: (400) Bad Request.
At line:1 char:1
+ Connect-SPOService -Url https://shootme.sharepoint.com -Credent ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Connect-SPOService], WebException
+ FullyQualifiedErrorId : System.Net.WebException,Microsoft.Online.SharePoint.PowerShell.ConnectSPOService

较新的 Powershell 是否有不同的语法或者我做错了什么? 另外,在 .net core 中运行脚本时,也许有一种方法可以在 ps 5.1 中运行脚本?

【问题讨论】:

  • 您可能需要查看该模块所需的库与 PowerShell Core 的兼容性。我的猜测是在内部异常中,该 cmdlet 需要一些东西才能像你在 PowerShell 的桌面版本中所期望的那样运行。
  • @Ash 也许是这样,但文档真的很糟糕。不知道微软想要什么 ps 6。
  • 如果您使用 C#,您可以针对 .Net Standard 并使用 Sharepoint libraries 创建您自己的 cmdlet。
  • @Ash 我不确定如何针对 .Net Standard(现在针对 .net core 3.1),但实际上我已经尝试过该解决方案:我得到了完全相同的错误。 (400) 错误请求。我认为这与此有关:sharepoint.uservoice.com/forums/329220-sharepoint-dev-platform/…(目前没有对 CSOM 库的 .net 核心支持)
  • 如果您创建一个 .NET Standard 应用程序,然后将其更改为针对 .NET Framework 的兼容版本,例如 4.7.2,您可以使用较旧的库 - docs.microsoft.com/en-us/dotnet/standard/…

标签: powershell sharepoint .net-core powershell-6.0


【解决方案1】:

您是否尝试过通过删除凭据部分并让它提示您登录并测试是否成功解决来手动连接?

编辑:我知道你也可以像这样从 .bat 调用 powershell:

powershell -version 2 .\xyz.ps1

但由于不知道您的具体目标,因此很难建议这是否是一个可行的选择。

【讨论】:

  • 是的,我尝试删除凭据部分。然后我得到 System.Windows.Forms 丢失的错误。它可能想为凭据输入启动一个 .net 框架模式,因此这在 ps 6.2.3 中永远无法工作。
  • 并从 .bat 文件运行 powershell:基础应用程序将是使用 c# 的 .net core 3 网站,所以我宁愿从 c# 运行 Powershell。我现在正在考虑一个单独的.net framework api,它可以运行 Powershell 5.1。
猜你喜欢
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多