【问题标题】:TFS Release Powershell task to deploy to Sharepoint: "Cannot bind argument to parameter 'String' because it is null."TFS 发布 Powershell 任务以部署到 Sharepoint:“无法将参数绑定到参数‘字符串’,因为它为空。”
【发布时间】:2017-10-18 02:56:19
【问题描述】:

编辑:

我设法让它接受 1 个参数。显然它讨厌接受 2。现在我有另一个例外:远程服务器返回 401 未经授权。我想解决方案是不使用超过 1 个参数,但这并不是真正的解决方案。对我来说听起来像是一个错误。


原问题:

我对此一无所知。

我有一个我想在发布期间执行的 powershell 脚本,它将构建的存档副本上传到 SharePoint 中的文档库。托管 TFS 2017 的构建服务器正在运行 Powershell 4。当我在同一台服务器上的 ISE 中运行 RDP 并运行以下脚本时(恰好是 TFS 似乎正在执行任务中的内容),只要我添加环境,它就可以工作在下面的脚本中对其进行注释的变量。

当我通过 TFS 运行相同的脚本(我从放置文件夹运行它)时,它会产生错误:

2017-10-17T13:48:52.5561597Z D:\BuildAgentWorkFolder\f00869677\Git\drop\publish_to_sharepoint.ps1 D:\Integration_20171017.2.zip Integration_20171017.2
2017-10-17T13:48:53.0241597Z Connecting to http://sharepoint_server/sites/TFS_DefaultCollection/NET as srv_promote...
2017-10-17T13:48:53.2737597Z ##[error]D:\BuildAgentWorkFolder\f00869677\Git\drop\publish_to_sharepoin
2017-10-17T13:48:53.2737597Z ##[error]t.ps1 : Cannot bind argument to parameter 'String' because it is null.

这很疯狂,因为您可以清楚地看到执行脚本的行中的非空参数。

我尝试在param 部分指定类型,例如[string]$filePathToUpload。我已经尝试在命令行参数列表中指定参数,例如.\publish_to_sharepoint.ps1 -filePathToUpload "..." -fileName "...",包括带引号和不带引号。

需要注意的是,当我尝试使用 in-line powershell 脚本运行任务时——由于字符限制,我只能粘贴到第一个“Write-Host” line-- 无误通过参数step,release通过。

这是 TFS Powershell 任务的错误吗?我不知道这个错误是从哪里来的。

提前致谢。以下是相关细节。


Powershell 脚本:

param(
    $filePathToUpload, 
    $fileName
)

#$Env:SharePointDomain = "..."
#$Env:SharePointBaseUrl = "http://sharepoint_server"
#$Env:SharePointSite = "/sites/TFS_DefaultCollection/NET/"
#$Env:SharePointUsername = "username"
#$Env:SharePointPassword = "password"
#$Env:SharePointLibrary = "library_name"

Add-Type -AssemblyName "Microsoft.SharePoint.Client, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL"
Add-Type -AssemblyName "Microsoft.SharePoint.Client.Runtime, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL"

Write-Host 'Connecting to'$Env:SharePointBaseUrl$Env:SharePointSite' as '$Env:SharePointUsername'...'

try{
    $sharepointFullUrl = $Env:SharePointBaseUrl + $Env:SharePointSite

    $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext $sharepointFullUrl
    $clientContext.Credentials = New-Object System.Net.NetworkCredential $Env:SharePointUsername, (ConvertTo-SecureString $Env:SharePointPassword -AsPlainText -Force)
    $web = $clientContext.Web
    $clientContext.Load($web) 
    $clientContext.ExecuteQuery();

    $relDestFilePath = $Env:SharePointSite + $Env:SharePointLibrary + "/" + $fileName

    $fStream = New-Object IO.FileStream $filePathToUpload ,'Open','Read','Read'
    [Microsoft.SharePoint.Client.File]::SaveBinaryDirect($clientContext, $relDestFilePath, $fStream, $true)

    Write-Host 'Successfully uploaded'$fileName' to SharePoint document library '$Env:SharePointLibrary'.'
}
catch {
    Write-Error $_
}

发布任务详情:

释放环境变量:

Powershell 任务日志

2017-10-17T13:48:52.5405597Z . 'D:\BuildAgentWorkFolder\f00869677\Git\drop\publish_to_sharepoint.ps1' D:\Integration_20171017.2.zip Integration_20171017.2
2017-10-17T13:48:52.5561597Z C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command "$env:PSModulePath= $env:PSModulePath + ';' + $([system.io.path]::combine($env:AGENT_HOMEDIRECTORY, 'agent\worker\modules')); try { [System.Security.Cryptography.ProtectedData] | Out-Null } catch { Write-Verbose 'Adding assemly: System.Security' ; Add-Type -AssemblyName 'System.Security' ; [System.Security.Cryptography.ProtectedData] | Out-Null } ; Invoke-Expression -Command ([System.Text.Encoding]::UTF8.GetString([System.Security.Cryptography.ProtectedData]::Unprotect([System.Convert]::FromBase64String('AQAAANCMnd8BFdERjHoAwE/Cl+sAmMDwyguygfTgNLUK3BteusFUlUwAAAAACAAAAAAADZgAAwAAAABAAAACMb/7FzkttmPEf8rMCA10mAAAAAASAAACgAAAAEAAAAN7Pqk+I01BNW4lONiyiuhArrpBqYAAiGrvCfLy/fPm3YiAEuhiuhKwcUEIZFza2IcrqjhKCe4Qn8qRcPMfDEiw82ZUKdNYCrzUjayfThC97Vm3/lNgP15sgTr0NpJwsfvs7fz6zG3zwqwNeu4ivYzcYI/bYd+y608v+jh3d+8vzeQgyIGhto+9WcJlIaCnYv5qymVD7nTME8pnYz+DfNkP02s50jkCySimKgVHGIUAAAAkXCoQxu5+9njKHMCkhB2FSpdivg='), [System.Convert]::FromBase64String('McDlBBSayxJHIwJ35ERNNQ=='), [System.Security.Cryptography.DataProtectionScope]::CurrentUser))) ; if (!(Test-Path -LiteralPath variable:\LastExitCode)) { Write-Verbose 'Last exit code is not set.' } else { Write-Verbose ('$LastExitCode: {0}' -f $LastExitCode) ; exit $LastExitCode }"
2017-10-17T13:48:52.5561597Z Executing the following powershell script. (workingFolder = D:\BuildAgentWorkFolder\f00869677)
2017-10-17T13:48:52.5561597Z D:\BuildAgentWorkFolder\f00869677\Git\drop\publish_to_sharepoint.ps1 D:\Integration_20171017.2.zip Integration_20171017.2
2017-10-17T13:48:53.0241597Z Connecting to http://sharepoint_server/sites/TFS_DefaultCollection/NET as srv_promote...
2017-10-17T13:48:53.2737597Z ##[error]D:\BuildAgentWorkFolder\f00869677\Git\drop\publish_to_sharepoin
2017-10-17T13:48:53.2737597Z ##[error]t.ps1 : Cannot bind argument to parameter 'String' because it is null.
2017-10-17T13:48:53.2737597Z ##[error]At line:1 char:1
2017-10-17T13:48:53.2737597Z ##[error]+ . 
2017-10-17T13:48:53.2737597Z ##[error]'D:\BuildAgentWorkFolder\f00869677\Git\drop\publish_to_sharepo 
2017-10-17T13:48:53.2737597Z ##[error]...
2017-10-17T13:48:53.2737597Z ##[error]+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2017-10-17T13:48:53.2737597Z ##[error]~~~
2017-10-17T13:48:53.2737597Z ##[error]    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorExcep 
2017-10-17T13:48:53.2737597Z ##[error]   tion
2017-10-17T13:48:53.2737597Z ##[error]    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorExceptio 
2017-10-17T13:48:53.2737597Z ##[error]   n,publish_to_sharepoint.ps1
2017-10-17T13:48:53.2737597Z ##[error] 
2017-10-17T13:48:53.2737597Z ##[error]Process completed with exit code 0 and had 1 error(s) written to the error stream.

【问题讨论】:

  • 关于更新,更新的脚本是什么,或者你只是在 PowerShell 任务中传递一个参数? SharePointPassword 变量是否仍然是安全变量?另一方面,添加具有真值的 System.Debug 变量,然后开始发布并在此处发布详细日志。
  • 你解决了这个问题吗?可以分享一下 OneDrive 上的详细日志吗?

标签: sharepoint azure-devops


【解决方案1】:

这是问题所在:$Env:SharePointPassword。秘密不存储为环境变量。修改您的脚本并将密码作为参数传入。

【讨论】:

  • 谢谢,但不是这样;我应该编辑我原来的问题。虽然不理想,但该变量并未标记为机密。有一次是作为变量传入的,但我在传入多个参数时遇到了另一个无法解决的问题。所以这不是我的解决方案。不过,你是对的。
猜你喜欢
  • 2017-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多