0
votes

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
Does the service account running the build process actually have access to the share? It doesn't look like it.Maximilian Burszley
Yes - I logged in using the service account and I can run the script or execute New_item on the share no problem.Ben_G
Have you tried using the -Force switch? If the directory structure doesn't exist, this will create it, and it also causes Read-Only to be ignored.Maximilian Burszley
Nope - I hadn't thought of that as I thought New-Item would create the directory if it didn't exist (which it never will - I'm always creating a new directory). But it works. Thanks!Ben_G

1 Answers

0
votes

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