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

喜夏-厌秋 提交于 2019-12-24 18:38:31

问题


I'm trying to create a script that's triggered by a TFS build that creates a new directory on a network share and then saves the location of that share in an environment variable.

Here's the (abridged) script as it originally was:

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

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

But when I run the the script from TFS I get this error:

New-Item : Access is denied

I can run the script from the command prompt even logged in as the service account running TFS build, but from within the build I get the error.

I fixed this by mapping a drive to the share using -Credential, but that means I need to hard-code the password for the ID in the script which I'd prefer not to do:

$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

Anyone have any ideas how I can do this without having to hard-code the PW?


回答1:


Thanks to @TheIncorrigible1 for the answer. Adding the -Force to the New-Item fixed the issue. Thanks!



来源:https://stackoverflow.com/questions/49562969/ps-new-item-access-is-denied-runs-fine-from-command-line-but-not-during-execut

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!