powershelltcm

Output format of TCM with PowerShell


I try to get a list of all my TestSuites of a specific TestPlan with PowerShell and TCM.exe by writing:

$listOfSuites = & "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\TCM.exe" suites /list /planid:1234

I get the right result, but it is formated as a single String like:

Id        Name                                                              --------- ----------------------------------------------------------------  1235      Test Project                                                 1236      Test Project -> Manual Tests -> First TestSuite            1237      Test Project -> Manual Tests -> Second TestSuite

Is it possible to get the result as a list or table so that I can iterated through these Suites?

Regards Johannes


Solution

  • You always get a single string if you execute an external program. In order to receive a string table, you first call

    $listOfSuites.split("`r`n")
    

    to get an array of strings, then you need to parse those strings by offset of those strings in the line-filled one (in your case, 0 to 9 is "id", 11 to end is "Name"). An example (taken from this script):

    $starters = New-Object psobject -Property @{"SessionName" = 0; "Username" = 0; 
         "ID" = 0; "State" = 0; "Type" = 0; "Device" = 0;};
    
    foreach($line in $c) {
         if($line.trim().substring(0, $line.trim().indexof(" ")) -eq "SESSIONNAME") {
                $starters.Username = $line.indexof("USERNAME");
                $starters.ID = $line.indexof("ID");
                $starters.State = $line.indexof("STATE");
                $starters.Type = $line.indexof("TYPE");
                $starters.Device = $line.indexof("DEVICE");
                continue;
            }
    
            New-Object psobject -Property @{
                "SessionName" = $line.trim().substring(0, $line.trim().indexof(" ")).trim(">")
                ;"Username" = $line.Substring($starters.Username, $line.IndexOf(" ", $starters.Username) - $starters.Username)
                ;"ID" = $line.Substring($line.IndexOf(" ", $starters.Username), $starters.ID - $line.IndexOf(" ", $starters.Username) + 2).trim()
                ;"State" = $line.Substring($starters.State, $line.IndexOf(" ", $starters.State)-$starters.State).trim()
                ;"Type" = $line.Substring($starters.Type, $starters.Device - $starters.Type).trim()
                ;"Device" = $line.Substring($starters.Device).trim()
        } 
    

    $c is a result from an external command that has no lines with dashes and a header formatted as such:

     SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
    

    Once you do, you can turn each string into a New-Object PSObject -property @{"Id"=$parsedid;"Name"=$parsedname} and get yourself a list of tailored objects to work with. The technique is demonstrated in the same example.