Find all locked files in TFS - tfs

I would like to see all files that are locked. so far, I've only found to use tf.exe status and look for anything with '!' because they are not reported as "lock, edit" as they are in the UI. Any ideas? thanks.

If you have the power tools installed, it's a one-liner:
tfstatus . -r -user * | % { $_.pendingchanges } | ? { $_.islock } | select -unique serveritem
If you prefer GUIs to scripts, try TFS Sidekicks.

If you are trying to use TFS Sidekicks, and can't figure out how, it is under Tools, Team Foundation Sidekicks, Status Sidekick. You will need to expand that window, but you will then be able to search for locks for a username.

I don't think this is possible using tf.exe or even tfpt.exe (The Power Tool command line). You'll need to look through the pending changesets for changes that are locks. You could do this in powershell using the Power Tool commandlets or you could do it using the following bit of .NET code that exercises the TFS API:
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
namespace TfsApiExample
{
class Program
{
static void Main(string[] args)
{
GetLockedFiles("http://tfsserver:8080","$/TeamProject");
}
private static void GetLockedFiles(string serverUrl, string serverPath)
{
TeamFoundationServer tfs = new TeamFoundationServer(serverUrl);
VersionControlServer vcServer = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
// Search for pending sets for all users in all
// workspaces under the passed path.
PendingSet[] pendingSets = vcServer.QueryPendingSets(
new string[] { serverPath },
RecursionType.Full,
null,
null);
Console.WriteLine(
"Found {0} pending sets under {1}. Searching for Locks...",
pendingSets.Length,
serverPath);
foreach (PendingSet changeset in pendingSets)
{
foreach(PendingChange change in changeset.PendingChanges)
{
if (change.IsLock)
{
// We have a lock, display details about it.
Console.WriteLine(
"{0} : Locked for {1} by {2}",
change.ServerItem,
change.LockLevelName,
changeset.OwnerName);
}
}
}
}
}
}

from your command prompt
>powershell
Then from powershell do:
PS > tf info * -recursive | &{
begin{
$out=#{}
$prefix = "loc"
}
process{
if ($_ -match "Local information"){
if ($out.Count -gt 0) {
[pscustomobject]$out
$out=#{}
$prefix = "loc"
}
} ElseIf ($_ -match "Server information"){
$prefix = "svr"
} else {
$parts = $_.Split(':')
if ($parts.Length -eq 2){
$out.Add($prefix + $parts[0].Trim(), $parts[1].Trim())
}
}
}
end{
if ($out.Count -gt 0) {
[pscustomobject]$out
}
}
} | where {!($_.svrLock -eq 'none')}

I've found a GUI option.
Start Visual Studio
Open file
Go to source control
Then workspaces
Enter your credentials
Check show remote workspaces
Remove all unwanted workspaces
That simple :)

Related

TFS API Pending Items, Exclude list, Include List

Background,
I am making a TFS merge tool to service out development and Branch requirements.
Withing this tool I have a business object layer that uses the Microsoft.Teamfoundation.ExtendedClient Package.
What I have
I currently have a function that does the checkin for my pending items
The 'changes' object has all my changes in it. Included and Excluded
Workspace workspace = _vcs.GetWorkspace(_workspaceName, _workspaceOwner);
WorkItemCheckinInfo checkInInfo = new WorkItemCheckinInfo(pbi, WorkItemCheckinAction.Associate);
PendingChange[] changes = workspace.GetPendingChanges();
ChangesetID = workspace.CheckIn(changes, checkInComment, null, new WorkItemCheckinInfo[] {
checkInInfo }, null);
What I need
I need to only get his list of 'Included' Pending changes.
Some have suggested using, bu this fails as ITS only has zero rows.
//get all candidate changes and promote them to included changes
PendingChange[] candidateChanges = null;
string serverPath = workspace.GetServerItemForLocalItem(_workspaceName);
List<ItemSpec> its = new List<ItemSpec>();
its.Add(new ItemSpec(serverPath, RecursionType.Full));
workspace.GetPendingChangesWithCandidates(its.ToArray(), true, out candidateChanges);
foreach (var change in candidateChanges)
{
if (change.IsAdd)
{
//ws.PendAdd(change.LocalItem);
}
else if (change.IsDelete)
{
//ws.PendDelete(change.LocalItem);
}
}
I have also tried this but SavedCheckin = null and i get an exception.
SavedCheckin savedCheckin = workspace.LastSavedCheckin;
// Create a list of pending changes.
var pendingAdds = new List<PendingChange>(workspace.GetPendingChanges());
List<PendingChange> excludedChanges = new List<PendingChange>();
for (int i = 0; i <= changes.Length - 1; i++)
{
if (savedCheckin.IsExcluded(changes[i].ServerItem))
{
excludedChanges.Add(changes[i]);
}
Console.WriteLine(changes[i].LocalItem.ToString() + " Change " + changes[i].ChangeType)
}
So I either need to iterate through the 'changes' list and remove 'Excluded' items
or there is bound to be something im missing here.
thanks in advance
Alan
There is no TFS API to get only Included changes, Included/Excluded changes sections exist in Visual Studio/Team Explorer. Visual Studio detects changes you make outside the system.
What you need is Visual Studio Extension API -- IPendingChangesExt Interface, you could refer to the article below or open a case on Visual Studio Extension side.
https://www.mztools.com/articles/2015/MZ2015007.aspx

php scandir produces extra elements (2 dots)

Hi,
I have the following files in a directory called content: index.php, page1.php, page2.php and page3.php ONLY.
Then I have this code:
$column = scandir("content");
foreach ($column as $value) {
$stvalue = str_replace(".php", "", $value);
echo "<div>$stvalue</div>";
}
Nevertheless, this is what I get:
<div>.</div>
<div>..</div>
<div>index</div>
<div>page1</div>
<div>page2</div>
<div>page3</div>
Whats with the first 2 elements? I dont have files named like that so I dont get it.
Thank you.
. - is a special directory referencing the current directory.
.. - is also a special directory and its referencing the parent directory.
To remove the special directories I can think of some options:
1.
foreach(glob("*.php") as $filename) {
echo "<div>$filename</div>";
}
2.
$files = array_diff(scandir("content"), array('..', '.'));
foreach($files as $file) { ... }
3.
foreach ($files as $file) {
if($file != '.' and $file != '..') { ... }
}
All of the above are alternatives. You don't need to use scandir() if you use glob() and vice versa. glob() - expects a pattern. It is possible to also provide it with the path like this:
glob("[path]/*.php") - this will list any php file located in path. glob() documentation can be found here PHP - glob()
$column = scandir("content");
foreach ($column as $value) {
$stvalue = str_replace(".php", "", $value);
if($stvalue != '.' || $stvalue != '..') {
echo "<div>$stvalue</div>";
}
}
. refers to the current directory. .. refers to the parent directory.
This is why typing cd .. on a command prompt will change to the parent directory. It's been this way in DOS, every version of Windows, Mac OS X, it's not just Linux and UNIX variants.
If you don't want to display them on your page, just skip the first two entries using array_slice.

How can I add a custom Build Summary section from a script?

I have a TFS 2013 XAML build process template that runs a PowerShell script (which pushes packages to NuGet).
The build activity WriteCustomSummaryInformation was added in TFS2012 for XAML builds. I'd like to use this same activity or implement the same functionality somehow from my script (so that I can show which packages were published). How can I do this?
I figured it out by running the activity and looking at what it added to the build information.
Function New-CustomSummaryInformation($Build, $Message, $SectionHeader, $SectionName, $SectionPriority = 0)
{
$CustomSummaryInformationType = 'CustomSummaryInformation'
$root = $Build.Information.Nodes | ? { $_.Type -eq $CustomSummaryInformationType } | select -First 1
if (!$root)
{
$root = $Build.Information.CreateNode()
$root.Type = 'CustomSummaryInformation'
}
$node = $root.Children.CreateNode()
$node.Type = 'CustomSummaryInformation'
$node.Fields['Message'] = $Message
$node.Fields['SectionHeader'] = $SectionHeader
$node.Fields['SectionName'] = $SectionKeyName
$node.Fields['SectionPriority'] = $SectionPriority
}
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.Client')
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.VersionControl.Client')
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.Build.Client')
$workspaceInfo = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current.GetLocalWorkspaceInfo($env:TF_BUILD_SOURCESDIRECTORY )
$tpc = new-object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection $workspaceInfo.ServerUri
$vcs = $tpc.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
$buildServer = $tpc.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])
$buildDef = $buildServer.GetBuildDefinition("MyProject", "MyBuildDefn")
$build = $buildServer.GetBuild($def.LastBuildUri)
New-CustomSummaryInformation $build -Message "This is a test message" -SectionHeader "This is the header displayed" -SectionName "ThisIsAnInternalKey"
$build.Information.Save()

can't run the automated project in testcomplete when it calls from jenkins

can't run the automated project in testcomplete when calls from jenkins.
In our continuous integration part ,the project is automated using testcomplete and it is calling through jenkins with the help of bat file.The scripts inside the bat file is
"C:\Program Files\Automated QA\TestComplete 7\Bin\TestComplete.exe " "D:\Test Complete7 Projects\ProjectInput_AllSamples\ProjecInputs.pjs" /r /p:Samples /rt:Main "iexplore" /e
It will open testcomplete and iexplorer ,but it is not filling the data(automation).
It is working perfectly when we directly call the bat file with out jenkins.Is there any solution
From your description it sounds like something in Windows stopping you from allowing your test application to work normally. It might be the fact that the second user could be a problem but I can't confirm that as I was not able find any definite explanations of how it works in Windows XP. I am pretty sure that this won't work on a Windows Vista, 7, 8 or server machine though because of the changes in architecture.
It sounds like the best solution is to make sure that your automated UI tests are started by an interactive user. When I was trying to add automated testing to our builds we used TestComplete 7 on a Windows XP SP2 virtual machine. In order to start our tests as an interactive user we:
Made an user log on when windows started, this way there was always an interactive user which means there was an actual desktop session which has access to the keyboard / mouse. I seem to remember (but can't find any links at the moment) that without an interactive user there is no active desktop that can access the keyboard / mouse.
We wrote a little app that would start when the interactive user logged on. This app would look at a specific file and when that file changed / was created it would read the file and start the application. The code for this app looked somewhat like this:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ApplicationStarter
{
class Program
{
// The string used to indicate that the application should quit.
private const string ExitString = "exit";
// The path which is being watched for changes.
private static string s_LoadFilePath;
static void Main(string[] args)
{
try
{
{
Debug.Assert(
args != null,
"The arguments array should not be null.");
Debug.Assert(
args.Length == 1,
"There should only be one argument.");
}
s_LoadFilePath = args[0];
{
Console.WriteLine(
string.Format(
CultureInfo.InvariantCulture,
"Watching: {0}",
s_LoadFilePath));
}
if (File.Exists(s_LoadFilePath))
{
RunApplication(s_LoadFilePath);
}
using (var watcher = new FileSystemWatcher())
{
watcher.IncludeSubdirectories = false;
watcher.NotifyFilter =
NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;
watcher.Path = Path.GetDirectoryName(s_LoadFilePath);
watcher.Filter = Path.GetFileName(s_LoadFilePath);
try
{
watcher.Created += OnConfigFileCreate;
watcher.EnableRaisingEvents = true;
// Now just sit here and wait until hell freezes over
// or until the user tells us that it has
string line = string.Empty;
while (!string.Equals(line, ExitString, StringComparison.OrdinalIgnoreCase))
{
line = Console.ReadLine();
}
}
finally
{
watcher.Created -= OnConfigFileCreate;
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private static void RunApplication(string configFilePath)
{
var appPath = string.Empty;
var arguments = string.Empty;
using (var reader = new StreamReader(configFilePath, Encoding.UTF8))
{
appPath = reader.ReadLine();
arguments = reader.ReadLine();
}
// Run the application
StartProcess(appPath, arguments);
}
private static void StartProcess(string path, string arguments)
{
var startInfo = new ProcessStartInfo();
{
startInfo.FileName = path;
startInfo.Arguments = arguments;
startInfo.ErrorDialog = false;
startInfo.UseShellExecute = true;
startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardError = false;
}
Console.WriteLine(
string.Format(
CultureInfo.InvariantCulture,
"{0} Starting process {1}",
DateTime.Now,
path));
using (var exec = new Process())
{
exec.StartInfo = startInfo;
exec.Start();
}
}
private static void OnConfigFileCreate(
object sender,
FileSystemEventArgs e)
{
Console.WriteLine(
string.Format(
CultureInfo.InvariantCulture,
"{0} File change event ({1}) for: {2}",
DateTime.Now,
e.ChangeType,
e.FullPath));
// See that the file is there. If so then start the app
if (File.Exists(e.FullPath) &&
string.Equals(s_LoadFilePath, e.FullPath, StringComparison.OrdinalIgnoreCase))
{
// Wait for a bit so that the file is no
// longer locked by other processes
Thread.Sleep(500);
// Now run the application
RunApplication(e.FullPath);
}
}
}
}
This app expects the file to have 2 lines, the first with the app you want to start and the second with the arguments, so in your case something like this:
C:\Program Files\Automated QA\TestComplete 7\Bin\TestComplete.exe
"D:\Test Complete7 Projects\ProjectInput_AllSamples\ProjecInputs.pjs" /r /p:Samples /rt:Main "iexplore" /e
You should be able to generate this file from Jenkins in a build step.
Finally you may need to watch the TestComplete process for exit so that you can grab the results at the end but I'll leave that as an exercise to reader.
If you are running Jenkins (either master or slave) as a windows service, ensure it is running as a user and not as Local System.
We also do the same as Gentlesea's recommends, we run TestExecute on our Jenkins Slaves and keepo the TestComplete licenses for the people designing the TestComplete scripts.

Even after uninstalling windows service, port number remains still occupied

I am installing windows service and it works fine.
Now I am uninstalling the same service. (to be specific I am using installutil command for installing as well as uninstalling) The service gets uninstalled but when I go to command prompt and check the status of the port it shows the port is still occupied. (Using netstat command)
Due to this when I try to delete the folder containing the service some of the dlls are not getting deleted and on trying to delete them forcefully I get the message already in user.
Can someone guide on this.
Use netstat -b to determine which executable is occupying your port, then kill it using task manager with the "Show processes from all users" option enabled.
Finally after doing testing different combination with netstat, I came up with this code
private static int GetProcessId(string portno)
{
string command = "netstat -o -n -a | findstr 0.0:" + portno;
ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
procStartInfo.CreateNoWindow = true;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardOutput = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
StreamReader sr1 = proc.StandardOutput;
string PID = sr1.ReadLine();
int index = PID.LastIndexOf(" ") + 1;
PID = PID.Substring(index, (PID.Length - (index--)));
return Convert.ToInt32(PID);
}
and then to kill that process
private static void KillProcess(int PID)
{
try
{
Process p = Process.GetProcessById(PID);
p.Kill();
}
catch (Exception e)
{
MessageBox.Show(e.Message + "\n" + e.StackTrace);
}
}

Resources