0

I have a PowerShell script which needs to access a folder and to make stuff inside it (like deleting or renaming a subfolder). I think it crashes because my on-premises TFS agent is denied to access this folder in this remote machine. The error message is:

2017-10-12T12:49:06.6816226Z ##[error]Remove-Item : Cannot remove item \[path_to_the_folder_I_want_my_script_to_access]\old.1: Access to the path

2017-10-12T12:49:06.6835446Z ##[error]is denied.

I googled the problem and found a probable solution: giving the read/write permissions to the folder accessed by the script to the Build Service Account of my TFS (Svc_tfsbuild account). But it doesn't work.

So if someone has another solution, it would be awesome. :D

Thank you so much in advance.

Have a great day!


1 Answers1

0

It's the permission issue, make sure you have correctly set the permission to let the Build Agent service account to access the folder.

You said that "the code did work with another agent, a TFS test agent", So you can compare the Build Agent service account with the test agent service account to correct the permission settings.

Another workaround is running the Remove-Item command with the specific credential which has the sufficient permission to remove the folder/files. If the account works locally ,then it will also work during the building process. I tested and worked at my side.

You can use below sample script to do that:

Param(
  [string]$computerName = "vtinmo502vm",
  [string]$path ="C:\Software\test.txt"
)
$Username = "domain\user"
$Password = ConvertTo-SecureString "EnterPasswordHere" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($Username,$password) 
Invoke-Command -computername $computerName {Remove-Item -path $args[0] -Recurse} -cred $cred  -ArgumentList $path 
Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55