【问题标题】:Connect-PnPOnline - A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredentialConnect-PnPOnline - 找不到接受参数“System.Management.Automation.PSCredential”的位置参数
【发布时间】:2020-03-01 08:20:42
【问题描述】:

我正在尝试从在 Azure 应用服务中运行的 PowerShell 作业连接到共享点站点,但每当我尝试连接时,都会出现以下错误:

Authentication failed: A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'.

在查找位置参数 -Credentials 的 Connect-PnPOnline 方法的源代码时,它需要一个 CredentialPipeBind,它是一个类 public sealed class CredentialPipeBind

该类看起来像是要在来自 Windows 凭据管理器的凭据或 Powershell 凭据对象之间切换。 Azure 作业可能想要从位置参数中获取文字类型? 我试着给它一个

Function Set-SpsConnection ($url, $creds) {
    $crpb = [SharePointPnP.PowerShell.Commands.Base.PipeBinds.CredentialPipeBind]::new($creds)
    Connect-PnPOnline –Url $sharepointurl –Credentials $crpb
}

但无济于事,现在它认为我使用的是SharePointPnP.PowerShell.Commands.Base.PipeBinds.CredentialPipeBind 而不是CredentialPipeBind,也许我可以以某种方式引用程序集,这样我就不必指定了?

该问题似乎仅在由作业运行或我在 Kudu 控制台中使用 .\scriptname.ps1 启动脚本时出现,当我在 Kudu 控制台复制粘贴中执行代码时,它似乎工作正常。 ..

在我看来,这似乎只是 azure 工作系统中的一个固有缺陷,它只会解释 litteral 类型,而不能像在客户端上那样运行。

【问题讨论】:

  • 您还有其他顾虑吗?如果您没有其他顾虑,您能接受答案吗?它可能会帮助更多有类似问题的人。

标签: powershell azure-webjobs


【解决方案1】:

根据我的研究,如果您想在 PowerShell 中使用凭据连接到共享点站点,参数 Credentials 应该是 PSCredential 对象或字符串(来自 Windows 凭据管理器的证书名称凭据)。更多详情请参考articleissuedocument

例如

$name = "username"
$password = "password"
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($name, $secpasswd)
Connect-PnPOnline -Url https://hanxia.sharepoint.com/ -Credentials $mycreds
Get-PnPList


更新

根据我的测试,我们可以在Azure web job中运行脚本

  1. 将模块更新为 Azure vai kuduo

  2. 创建一个网络作业。我的脚本如下

$ProgressPreference="SilentlyContinue"
Import-Module '<the path of your module>\SharePointPnPPowerShellOnline' -DisableNameChecking
$name = "username"
$password = "password"
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($name, $secpasswd)
Connect-PnPOnline -Url https://<>.sharepoint.com/ -Credentials $mycreds

Get-PnPList

【讨论】:

  • 看不到这应该是预期的行为,如果您要在 Azure webjob 中尝试此操作,它将失败,因为它需要不同的值。也许我必须创建一个 CredentialPipeBind,但感觉好像这可能是一个错误......
  • 当我运行您的隔离代码时,它似乎可以工作,但是(某些东西)正在使它在尝试使用我的代码时不起作用。我要多做一些测试,看看能不能弄明白……
  • @ThomErnst 您还有其他顾虑吗?
  • 它不会解决似乎发生的固有问题,感觉就像我需要为每一个动作制作很多新方法,否则它不起作用。我似乎无法理解这个问题,所以我无法解释为什么它也不起作用......
猜你喜欢
  • 1970-01-01
  • 2016-07-06
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 2015-05-21
  • 2019-12-23
相关资源
最近更新 更多