【发布时间】: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