【问题标题】:PS New-Item : Access is denied runs fine from command line but not during execution from TFS buildPS New-Item:访问被拒绝从命令行运行良好,但在从 TFS 构建执行期间运行良好
【发布时间】:2018-09-08 19:39:10
【问题描述】:

我正在尝试创建一个由 TFS 构建触发的脚本,该构建在网络共享上创建一个新目录,然后将该共享的位置保存在环境变量中。

这是(删节的)脚本,原为:

$NewPathForFiles = "\\NetworkShareName \" + "ProductName "+ $MajorBuildNumber 

New-Item -Path $NewPathForFiles -ItemType directory # Create the folder

但是当我从 TFS 运行脚本时,我得到了这个错误:

New-Item : Access is denied

我可以从命令提示符运行脚本,甚至以运行 TFS 构建的服务帐户身份登录,但在构建中我得到了错误。

我通过使用 -Credential 将驱动器映射到共享来解决此问题,但这意味着我需要在脚本中硬编码 ID 的密码,我不想这样做:

$pass = ConvertTo-SecureString "MyPassword" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("MyID",$pass)

New-PSDrive -Name P -PSProvider FileSystem -Root "\\MyNetworkShare" -Credential $cred

$NewPathForFiles = "p:\ProdctName " + $MajorBuildNumber 

New-Item -Path $NewPathForFiles -ItemType directory # Create the folder

任何人都知道如何在无需对 PW 进行硬编码的情况下做到这一点?

【问题讨论】:

  • 运行构建过程的服务帐户是否实际上有权访问共享?看起来不像。
  • 是的 - 我使用服务帐户登录,我可以运行脚本或在共享上执行 New_item 没问题。
  • 您是否尝试过使用-Force 开关?如果目录结构不存在,则会创建它,同时也会导致Read-Only 被忽略。
  • 不——我没有想到这一点,因为我认为如果 New-Item 不存在,它会创建目录(它永远不会——我总是创建一个新目录)。但它有效。谢谢!

标签: powershell tfs tfsbuild


【解决方案1】:

感谢@TheIncorrigible1 的回答。将 -Force 添加到 New-Item 解决了该问题。谢谢!

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-18
  • 2016-05-26
  • 2019-05-01
  • 2019-10-16
  • 2013-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多