PS 新项目:访问被拒绝从命令行运行正常,但在从 TFS 构建执行期间运行不正常

PS New-Item : Access is denied runs fine from command line but not during execution from TFS build

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

这是原样的(删节)脚本:

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

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

但是当我 运行 来自 TFS 的脚本时,我得到这个错误:

New-Item : Access is denied

我可以 运行 命令提示符下的脚本甚至作为服务帐户登录 运行ning 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 进行硬编码的情况下执行此操作?

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