Create a workspace from Powershell run within VS Team Services build - powershell

I am writing a Powershell script as part of a Visual Studio Team Services build, this build uses a hosted build controller. The script is intended to check out a file, make changes then check it back in.
In the script I am trying to invoke the tf command to create a new workspace but I'm having authentication issues. The command
& $tf vc workspace /new $tempWorkspaceName /collection:https://mycollection.visualstudio.com/
produces the error:
TF30063: You are not authorized to access https://mycollection.visualstudio.com/.
How can I access TFS from my Powershell script without embedding logins or passwords in the script? Can I access TFS within the context of the build agent, which itself logs in to TFS? Or is there another way I should be doing this?
Note that I haven't used the TFS Power Tool cmdlets as it is a hosted build server. Using the TFS RestAPI also doesn't appear to be an option as there is no facility to check a file in or out.

Check Allow Scripts to Access OAuth Token option of your build defition
Append /noprompt /loginType:OAuth /login:.,$(System.AccessToken) to your TF command (arguments)
About TF Command, you can check this article.

Related

Post Build event that works on both Azure DevOps and Local PC

I have a VS2017 solution that Builds both locally and also on Azure DevOps.
I now need to run a Post Build script to run an EXE. I have this working on my local machine, but I guess there will be an issue with the Path to the EXE which has been added to the DevOps Library.
Note. The EXE is all installed on DevOps and runs fine from a Command Line Task - I just need it to run as a post build on one of the projects so that this project is ready to be packaged in the Installer SetUp project. (During a full Solution build).
This represents the Local Post Build script - How do I handle this on Azure, where the path will be different?
"C:\Program Files (x86)\{dir}\{app}.exe" -file "$(ProjectDir){file.txt}"
Any help appreciated. Thanks!
This represents the Local Post Build script - How do I handle this on
Azure, where the path will be different?
$(ProjectDir) is msbuild property, so it works on both Azure DevOps and Local PC. You only need to pay attention to the {dir} of the xx.exe.
My suggestion is to put the exe in solution folder (where the xx.sln file exists), then you can use script like "$(SolutionDir)\{app}.exe" -file "$(ProjectDir){file.txt}". The $(SolutionDir) and $(ProjectDir) can be recognized by msbuild. (It works for both local pc and online devops.)
Or you can put the xx.exe under root directory of your git repo, then use $(System.DefaultWorkingDirectory) as the path of your xx.exe, but it only works for online devops, it can't work on local PC. (Not recommended)

Error in creating new projects in online TFS using my account

This is a different issue as I am accessing and using my visualstudio.com server. Please see the image.
I am currently working on an automation in powershell which is to create a new project in VSTS using my account. Unfortunately, I am having an error which is below.
System.Management.Automation.RemoteException: The project collection does not have a default location for creating project portal sites configured.
Either configure this location using the Team Foundation Administration Console or specify the /w
ebapplication and /relativepath arguments.
tfpt only works with TFS2015 or more older version. It does not work with VSTS and TFS2017.
You can use Rest API to create the team project via script just as you found. Or you can also consider to use the VSTS CLI: VSTS Create Project.

tf.exe authenticate for vsts

I am trying to list all the workspaces and trying to authenticate tf.exe with vsts using the below commandline.
tf workspaces /Collection: https://[account].visualstudio.com/defaultcollection /login:USername,Password
The vsts account is backed by Azure Active Directory synced with an on-prem AD.
It works fine when I have connected to the vsts from visual studio and then running the above tf.exe command without the login switch.
But, when I use the login switch for tf.exe and use the same credentials that I use in visual studio it throws
TF30063: You are not authorized to access https://[account].visualstudio.com/defaultcollection
TF30063: You are not authorized to access https://[account].visualstudio.com/defaultcollection
What I am trying to achieve is that I am going to provision a server and then not going to login to visual studio and want my tf to work as it works when logged in to the visual studio online account in the team explorer of my visual studio.
I am an admin of the vsts account and have full right on everything. If there are other ways of doing this possibly authenticate TF.exe using PAT token that would work as well.
I would use tf.exe again for a checkin command from automated builds.
Any help in solving this would be appreciated.
There is the Check in changes task in TFVC Build Tasks extension that you can use it to check in changes. Also, you can call TF command to check in changes during the build process, it uses build service account.
On the other hand, you can create a console application to check in changes through TFS API (Workspace.CheckIn method), then call this app through command line.
I was having the same issue (TF30063) when running tf.exe. For me the solution was:
Exit all running instances of Visual Studio
Rename %LocalAppData%\Microsoft\Team Foundation\7.0\Cache to %LocalAppData%\Microsoft\Team Foundation\7.0\Cache_old (or you could delete it; but I wanted to preserve the original so I had the option restore it when going back into VS). NB: the 7.0 varies according to which version of TF you're using.
Navigate to my project's folder pushd c:\projects\vsts_tfvc_repos\Project123
Run tf /collection:myinstance.visualstudio.com /workspace:%computername%
/login:myMicrosoftAccount#example.com,myPassword to initialise the session under the correct account
Thereafter, everything worked as expected...

How to put non-project build files on build server during build?

Tools: Team Foundation Server 2012
NuGet: 3.0
Powershell: 3.0
I would like to know how to put build related files onto the build server during the build process.
We have a CI Build that runs on check in, so unless we want the build firing every time we update our nuget.config or a powershell script we need to store them someplace else.
I can put them in a separate team project, and add them to our Working folders, but then again TFS will launch a build whenever we make a change to our build files. We don't want that.
Is there no way to host some build files into TFS and download them into the build server during build that does NOT cause a new build to fire if you change those files?
If I change a PS1 file, we don't want the build running because of that.
When checking them in you can add: ***NO_CI*** to the comment, and CI builds will not trigger.
Other than that, you can fire off a Powershell script by customizing the XAML and adding an Invoke process task to call powershell.exe and passing the powershell script you do keep in source control.
From there you can either use OneGet or Download-File to fetch and install anything you need.

How to get an agent user capability in TFS Build task?

I've declared a user capability named "MyCapability" in a TFS 2015 build agent with a path as value. How can I get this value in the powershell script of my build task ?
$env:MyCapability and $MyCapability don't work.
You cannot access the capabilities from PowerShell through a variable unfortunately.
You could use the Rest Client to connect back to TFS and read the Build Agent as well as the Build Definition to read out the configured values. This will require access to the OAuth token, which can be configured through a checkbox on the Build Definition's configuration page.