Search code examples
c#.netpowershelltfsversion-control

.Net PowerShell Invoke tf(TFS) History


I want to use PowerShell to Automation Invoke 【cd D:/XXXX】&【tf history *】 to change working folder then get my target TFS change History.

But I have Problem to Invoke tf.

.Net use PowerShell Invoke tf(TFS)

public static bool GetFile(string disk, string projectPath, string dateRegion, string fileFullPath)
    {
        bool result = true;
        try
        {
            using (PowerShell powershell = PowerShell.Create())
            {
                powershell.AddScript($"{disk}");
                powershell.AddScript(@$"cd {projectPath}");

                powershell.AddScript(@$"tf vc history * /noprompt /recursive /format:detailed /v:D{dateRegion} > {fileFullPath}");

                foreach (PSObject psResult in powershell.Invoke())
                {
                    Console.WriteLine(psResult);
                }
            }

            Console.WriteLine("執行結束");
            return result;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            result = false;
            return result;
        }
    }

Solution

  • You may use it without powershell like here:

            string foderPath = "YOUR_WORKSPACE";
            string tfexepath = "C:\\Program Files\\Microsoft Visual Studio\\2022\\Professional\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\tf.exe";
    
            var p = Process.Start(
                new ProcessStartInfo(tfexepath, $@"vc history * /noprompt /recursive /format:detailed")
                {
                    CreateNoWindow = true,
                    UseShellExecute = false,
                    RedirectStandardError = true,
                    RedirectStandardOutput = true,
                    WorkingDirectory = foderPath
                }
                );
    
            p.WaitForExit();
    
            string output = p.StandardOutput.ReadToEnd().TrimEnd();
            string error = p.StandardError.ReadToEnd().TrimEnd();
    
            Console.WriteLine(output);
            Console.WriteLine(error);