Calling TF.exe from PowerShell script in TeamCity - powershell

I am trying to call tf.exe from within a Powershell step of a TeamCity build configuration.
The executable is usually found at C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\TF.exe (or similar). In my script I am setting a variable to the path of tf.exe as follows:
$tfsExe = "%env.ProgramFiles(x86)%\Microsoft Visual Studio 11.0\Common7\IDE\TF.exe"
I have verified the existence of tf.exe on the build server by accessing it through RDP. I have also called it in a powershell console window with the following command
& "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\tf.exe" help
which responded with the following output confirming that it is accessible and working:
Microsoft (R) TF - Team Foundation Version Control Tool, Version 11.0.50727.1
Copyright (c) Microsoft Corporation. All rights reserved.
Type tf help <command name> for command line description.
Type tf msdn <command name> for full documentation in Microsoft Document Explorer.
Commands:
tf add Adds new files and folders from a local file system location to Team Foundation version control.
tf branch Copies an item or set of items, including metadata and version control history, from one location to another on the Team Foundation version control server and in the workspace.

The issue here was that the script was being executed on a build agent not the build server. Once the TFS plugin was installed on the build agent tf.exe was available.

Related

Getting a list of all TFS checked out files

I am aware that in the past, this was possible via TFS Power Tools, but that seems to have been deprecated.
You can use tf command, if you installed visual studio 2017 or 2019.
Open Developer Command Prompt for VS, and run below command.
Tf.exe status /collection:"<project collection URL>" /user:* /recursive
tf.exe is now integerated with VS2017/VS2019. You can find it in Visual Studio installation folder (eg. C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer)

Unable to find the TF executable while adding tfs to VSCode

I am adding TFS to Visual Studio Code IDE(1.42.0) for that I have installed Azure Repos extension.
In VS code at File-->Preference-->Setting-->Tfvc:Location, when I am entering path C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\tf.exe.
Getting error
(team) Unable to find the TF executable. Please ensure TF is installed
and the path specified contains the filename.
How can I solve this issue?
Edit 1: After installing Azure Repos extension, I am entering above path in tfvc:Location(for both tabs User and Workspace), but after entering path nothing happens. Check below screenshot
Edit2: Now I am getting this error at right below in VS code IDE
Since you have not got any help yet. I will take a shot. Any chance there is a issue with finding the executable and casing for the extension? My local TF is capitilized. \TF.exe
Also might want to checkout the Github and readme for TFVC.
Make sure you have installed VS on your computer. With a typical installation of Visual Studio, the Windows version of the TFVC command line client (tf.exe) is available under the Program Files (x86) folder. It will typically be placed in a location similar to C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\TF.exe. On the 2017 version of Visual Studio Community, it can be found in a location similar to C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\TF.exe.
I found a .vscode folder in my project
delete it, close and reopen vscode solved the problem

Start Developer PowerShell for VS 2019 from Azure DevOps pipeline

We have configured a windows virtual machine and deployed an agent there to build our code and run scripts.
In our VM we two different flavors of PowerShell command prompt:
Windows PowerShell
Developer PowerShell for VS 2019.
How can we start "Developer PowerShell for VS 2019" from our pipeline YAML script and execute our checked out .ps1 file there?
There are several flavors of PowerShell tasks that can be initiated from the pipeline though and not sure which one of them will serve the purpose. They are the following:
Azure PowerShell
PowerShell
PowerShell on Target Machine
Service Fabric PowerShell.
Which of the above represent "Developer PowerShell for VS 2019"?
The reason behind this specific flavor of PowerShell is:
Need to have some of the .NET Framework Tools (CorFlags.exe) which are only accessible in the "Developer PowerShell for VS 2019" and not in the other one.
The Developer PowerShell for VS 2019 is a regular PowerShell with a module imported, you can see exactly what in this way:
Go the Start menu and search for Developer PowerShell for VS 2019.
Right-click on it and Open file location - you will get this:
Now right-click again on the Developer PowerShell for VS 2019 shortcut and Properties.
You will see in the location that is run the regular PowerShell with some command:
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noe -c "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell bc97b47b}"
Now, if you open a PowerShell and run the script there:
&{Import-Module "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"; Enter-VsDevShell bc97b47b}
You will get the Developer PowerShell for VS 2019 and CoreFlags.exe will work:
So, you need to use the regular PowerShell task (your option 2) and run the above command at the begging of the script.
Note: you should copy exactly the command you see in the Properties, in each machine it could be different.

How to Import Nuget Module to powershell script [duplicate]

I was trying to use my favorite source control from the Package Manager console in Visual Studio 2010. I had issues described in another topic. For now the best answer is to move all command-line stuff from Visual Studio to bare PowerShell.
But this case the Visual Studio related commandlets are not working. For example, most crucial one — I cannot run Update-Database command from Entity Framework.
Is it possible to register somehow Visual Studio specific commandlets for the current project in PowerShell? Or make PowerShell automatically take current project context from the packages subfolder?
The NuGet PowerShell commands rely on being run from within Visual Studio so will not work outside in the normal PowerShell running from the command line.
You can however use migrate.exe which ships with the EntityFramework NuGet package and use that from the command line to update your database.
As a prototype I put together a way to use NuGet PowerShell commands from the normal PowerShell command line using SharpDevelop. Unfortunately at the moment the EntityFramework NuGet package does not work with SharpDevelop.
Another interesting project is StudioShell which provides a new DTE: drive inside Visual Studio but can also be used outside from the command line. I do not believe it supports NuGet PowerShell commands being run from the normal PowerShell command line.
I can find the NuGet.psd1 file at:
C:\Program Files (x86)\Microsoft Visual Studio
12.0\Common7\IDE\Extensions\5ttpefif.3mk\Modules\NuGet\NuGet.psd1.
However, when you try to load it:
PS> Import-Module $pathToNuGetPsd1 -Force -NoClobber -Scope Global
Import-Module : The name of the current Windows PowerShell host is: 'ConsoleHost'.
The module 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\
Extensions\5ttpefif.3mk\Modules\NuGet\NuGet.psd1' requires the following Windows
PowerShell host: 'Package Manager Host'.
I think we're out of luck. It has to be run from the Package Manager Host and requires things from Visual Studio as stated by Matt.
To solve my problem I used Chocolatey to install NuGet.CommandLine and then used NuGet.bat to do what I needed. It is a little more work and may not work in all cases depending on what you're trying to do.
Chocolatey: https://github.com/chocolatey/chocolatey/wiki/Installation
NuGet.CommandLine:
PS> cinst NuGet.CommandLine
I'm not terribly familiar with the Visual Studio cmdlets, but you can import a module into your PowerShell session by using Import-Module -Name <ModuleName>. You can list the available (aka. "installed") PowerShell modules by using `Get-Module -ListAvailable'.
My guess would be that the Visual Studio cmdlets are contained with its own PowerShell module, but it's quite possible that it's not "installed" to one of the standard locations in $env:PSModulePath. If this is the case, then you might need to locate the module directory and import either the .psd1 or .psm1 file directly, and pass that into: Import-Module -Name <FullPathToModuleFile>.
As an example of the above, take notice of where the Windows Azure PowerShell module is located: http://trevorsullivan.net/2012/06/07/introducing-microsofts-official-windows-azure-powershell-module/
It's under the Program Files directory, and is not immediately available to PowerShell, unless you import the module from its fully qualified path (the .psd1 module manifest file).
Hope this helps.

powershell tf command not recognized as the name of a cmlet

tf : The term 'tf' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and
try again.
I set up the environment variable but does not work. tf command works in Developer Command Prompt for VS2013 but does not work in Powershell. How can I make this to work?
You need to include tf.exe full path. For example, you want to check in files with tf checkin command in PowerShell:
$filePath = "C:\Users\username\Source\Workspaces\teamproject\solutionname\projectname\Class1.cs"
Set-Alias tfs "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\tf.exe"
tfs checkin $filePath /noprompt
This worked for me. I added the path of TF.exe
("C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE" in my case) to system environment PATH variable (through My Computer->Properties->Advanced System Settings->Environment Variables->System Variables->Path->Edit).
You can check if "tf" is working by typing "tf" in the powershell. If you get an output something as :
Microsoft (R) TF - Team Foundation Version Control Tool, Version 14.102.25619.0
Copyright (c) Microsoft Corporation. All rights reserved.
......
tf is working properly.
To answer the question form another perspective, this is how you are supposed to call the tf environment variable you created:
%tf%
I've given it the value C:\Windows\notepad.exe, and the application opens when calling the variable from cmd.
You need to download and install the Microsoft Team Foundation Server Power Tools in order to run the TFPT command.
TFS 2015 version can be found here:
Microsoft Team Foundation Server 2015 Power Tools
If you run the Powershell from VS Tools then you need to close your Powershell session and restart your VS in order for it to pick up the newly installed tools and run the Powershell again.