none
Using powershell to run preptool.exe during TFS build process RRS feed

  • General discussion

  • In TFS 2010, there was a custom build template that contained the following parameters to sign files: "/c preptool.exe QBPrepTFS.xml"

    I am trying to get that process to run from the powershell script in TFS 2013. I haven't gotten the below portion to work, I have yet to include 'QBPrepTFS.xml'

    Running this on the build machine and still having the same problem, but works if I put in the full file path

    $s="{0}\{1}" -f $Env:TF_BUILD_SOURCESDIRECTORY, "QB\AS\Sign\"
    
    Start-Process $Env:TF_BUILD_SOURCESDIRECTORY\Main\OST\AS\Sign\preptool.exe 
    Start-Process : This command cannot be run due to the error: The system cannot find the file specified.
    At C:\Classic\Plutonium\QB\SigningKey_QB.ps1:3 char:1
    + Start-Process $Env:TF_BUILD_SOURCESDIRECTORY\Main\OST\AS\Sign\preptool.exe
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
        + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

    Thursday, February 11, 2016 6:03 PM

All replies

  • You probably don't need to use Start-Process. The invocation operator (&) is usually sufficient. Read through the following article for assistance:

    Windows IT Pro: Running Executables in PowerShell


    -- Bill Stewart [Bill_Stewart]

    Thursday, February 11, 2016 6:36 PM
    Moderator
  • $command=Join-Path $Env:TF_BUILD_SOURCESDIRECTORY  'QB\AS\Sign\preptool.exe'

    & $command

    --- OR --

    $command=Join-Path $Env:TF_BUILD_SOURCESDIRECTORY  'QB\AS\Sign\preptool.exe'

    Start-Process $command -NoNewWindow


    \_(ツ)_/

    Thursday, February 11, 2016 9:39 PM
  • I couldn't get it to run with the Join or & 

    But, this works, but I still need it to hit the .xml file

    $s="{0}\{1}" -f $Env:TF_BUILD_SOURCESDIRECTORY, "QB\AS\Sign\"

    "$Env:TF_BUILD_SOURCESDIRECTORY\QB\AS\Sign\preptool.exe" 

    how do I make that run against Text.XML?

    Monday, February 15, 2016 3:23 PM
  • I couldn't get it to run with the Join or &

    Remember: We can't see your screen, so we don't know what "I couldn't get it to run" means. We don't know what you tried because you don't say. I use the & operator all the time with no problems, as documented in the article I posted.


    -- Bill Stewart [Bill_Stewart]

    Monday, February 15, 2016 3:28 PM
    Moderator
  • $command=Join-Path $Env:TF_BUILD_SOURCESDIRECTORY  'QB\AS\Sign\preptool.exe'  

    Start-Process $command QBPrepTFS.xml -NoNewWindow

    Join-Path : Cannot bind argument to parameter 'Path' because it is null.
    At line:1 char:20
    + $command=Join-Path $Env:TF_BUILD_SOURCESDIRECTORY  'QB\AS\Sign\preptool.exe'
    +                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Join-Path], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand

    Monday, February 15, 2016 4:12 PM
  • Help start-process -full

    read the instructions carefully.


    \_(ツ)_/

    Monday, February 15, 2016 4:17 PM
  • That environment variable is actually set in your session?

    Monday, February 15, 2016 4:19 PM
  • $command=Join-Path $Env:TF_BUILD_SOURCESDIRECTORY  'QB\AS\Sign\preptool.exe'  

    Start-Process $command QBPrepTFS.xml -NoNewWindow

    Join-Path : Cannot bind argument to parameter 'Path' because it is null.
    At line:1 char:20
    + $command=Join-Path $Env:TF_BUILD_SOURCESDIRECTORY  'QB\AS\Sign\preptool.exe'
    +                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Join-Path], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand

    This means, of course, that the TF_BUILD_SOURCESDIRECTORY environment variable is not set in the session in which you are running that PowerShell command.

    For this to work, the variable would need to be set for the Join-Path command to work. Otherwise it's the same as typing this:


    Join-Path $null 'QB\AS\Sign\preptool.exe'
    

    If you type that command, of course you will get an error.


    -- Bill Stewart [Bill_Stewart]

    Monday, February 15, 2016 5:03 PM
    Moderator
  • So, now that we are done chastising the guy new to powershell, how do I set that directory correctly?

    The previous powershell scripts I have done for other build functionality lately was able to work with the directory during the build without that issue.

    Monday, February 15, 2016 6:46 PM
  • So, now that we are done chastising the guy new to powershell, how do I set that directory correctly?

    The previous powershell scripts I have done for other build functionality lately was able to work with the directory during the build without that issue.

    Open PowerShell.  type the following:

     $Env:TF_BUILD_SOURCESDIRECTORY 

    IF you are in the TFS build environment it will  be set. If not then lad the build environment This has nothing to do with PowerShell it is just a requirement for using the TFS build environment.

    This is not being chastised.  It is that you have no training in TFS and none in PowerShell so it it hard to show you what you are missing.  Most people here are not TFS users.  They assume you know TFS and are trying to use it from PowerShell.

    For assistance with TFS post issues in TFS forum and ask how t use the TFS build environment from batch or from PowerShell.

    If TFS is loaded then you do not need a path. Jus type:

    preptool project_config.xml

    The tool is in the TFS environment path when the build environment is loaded.


    \_(ツ)_/

    Monday, February 15, 2016 6:59 PM
  • If you are trying to remote this I believe you will do best by creating a custom restricted endpoint.


    \_(ツ)_/

    Monday, February 15, 2016 7:00 PM
  • I understand the build process, but I do not understand powershell. Tried to mimic things I was able to get to work in a few other powershell scripts I did lately. The scripts are being ran during the build process on the TFS build machine.

    When pieces are left out of your suggestions, I am not sure what is missing.

    My current code is

    $s="{0}\{1}" -f $Env:TF_BUILD_SOURCESDIRECTORY, "QB\AS\Sign\"
    
    $command=Join-Path $Env:TF_BUILD_SOURCESDIRECTORY  'QB\AS\Sign\preptool.exe'  
    
    Start-Process $command QBPrepTFS.xml -NoNewWindow

    With the following error in the build log

    Start-Process : This command cannot be run due to the error: The system cannot
     find the file specified.
     At C:\Builds\168\Classic\QuickBid--Plutonium\src\Main\QB\SigningKey_QB.ps1:5
     char:1
     + Start-Process $command QBPrepTFS.xml -NoNewWindow
     + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOp
     erationException
     + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.C
     ommands.StartProcessCommand

    Tuesday, February 16, 2016 3:13 PM
  • I don't know whether the build process involves using a batch file (.bat or .cmd) to set environment variables. If that's the case, read the following article: Windows IT Pro - Take Charge of Environment Variables in PowerShell.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, February 16, 2016 3:18 PM
    Moderator
  • The binaries are in the path  just use "preptool"  it will work.  You do not need Start-Process.

    The reason we suggest posting in TFS forum is because you need to understand how to use TFS. The issues are not PowerShell.

    The binaries folder is not where you are pointing. It is here: $env:TF_BUILD_BINARIESDIRECTORY

    Please post in TFS forum for guidance in how to use TFS Build.


    \_(ツ)_/

    Tuesday, February 16, 2016 3:33 PM
  • At Bill, for a script to zip, I use 

    function create-7zip([String] $aDirectory, [String] $aZipfile){
        [string]$pathToZipExe = "C:\Program Files\7-zip\7z.exe";
        [Array]$arguments = "a", "-tzip", "$aZipfile", "$aDirectory";
        & $pathToZipExe $arguments;
    }
    
    create-7zip "$Env:TF_BUILD_BINARIESDIRECTORY\x86\Release\*" "$Env:TF_BUILD_DROPLOCATION\x86\Installer.zip"
    
    create-7zip "$Env:TF_BUILD_BINARIESDIRECTORY\x64\Release\*" "$Env:TF_BUILD_DROPLOCATION\x64\Installer.zip"

    And to do some copy steps 

    $s="{0}\{1}" -f $Env:TF_BUILD_SOURCESDIRECTORY,"bin\x64\Release\*\"
    Copy-Item -Path $s -Destination $Env:TF_BUILD_BINARIESDIRECTORY\x64\Release –Recurse
    Copy-Item -Path $s -Destination $Env:TF_BUILD_BINARIESDIRECTORY\x64\Debug –Recurse

    Both work as is.

    JRV- I actually posted in TFS forum first. They do not know about powershell. I am just trying to get a grasp of the command needed to run this. Once I know that, the environment variable portion is easy. I can always test it from a directory on the c:

    Tuesday, February 16, 2016 3:39 PM
  • Also, without Start-Process, I have 'Unexpected Token in expression or statement' 

    C:\Builds\168\Classic\QuickBid--Plutonium\src is the source directory

    $Env:TF_BUILD_SOURCESDIRECTORY is the variable for the source directory. Not sure where you got Binary directory from


    • Edited by BossHogg20 Tuesday, February 16, 2016 3:55 PM
    Tuesday, February 16, 2016 3:52 PM
  • Sorry I posted the wrong variable>

    The binaries for PREPTOOL should be in the path.

    Look up where the preptool.exe is stored and you will see what I mean.

    Dump path and look.

    $env:path -split ';'

    PowerShell is fully supported in latest version of TFS build.  The TFS forum can also show you how to see the build environment.

    Here is a good resources: http://www.dotnetcurry.com/visualstudio/1035/environment-variables-visual-studio-2013-tfs

    Another thing you can do to try and understand what is happening is this:

    if(Test-Path "$Env:TF_BUILD_SOURCESDIRECTORY\QB\AS\Sign\preptool.exe"){
          "Path is good"
    }else{
           "Path not found"
    }


    \_(ツ)_/

    Tuesday, February 16, 2016 3:59 PM
  • Note that this page tells yo what the variables are used for:

    https://msdn.microsoft.com/en-us/library/vs/alm/build/scripts/variables?f=255&MSPPError=-2147217396

    BUILD_SOURCESDIRECTORY is not where preptool is located.


    \_(ツ)_/

    Tuesday, February 16, 2016 4:02 PM
  • You need to note how "variable" are used in Windows.  The variables are variable. They are set per project.  The tools and executables for TFS are set in the path so you can find them easily on any version of TFS.   "preptool.exe"is in the path.

    \_(ツ)_/

    Tuesday, February 16, 2016 4:05 PM
  • Also, without Start-Process, I have 'Unexpected Token in expression or statement' 

    C:\Builds\168\Classic\QuickBid--Plutonium\src is the source directory

    $Env:TF_BUILD_SOURCESDIRECTORY is the variable for the source directory. Not sure where you got Binary directory from


    You do not understand tha source is not where preptool is stored.  preptool is a TFS binary.  The build binaries are the folders where the release and debug binaries are stored.

    You say you have an error but fail to post the line that caused the error..


    \_(ツ)_/

    Tuesday, February 16, 2016 4:25 PM
  • In the last build, preptool.exe is in the following folder location

    C:\Builds\168\Classic\QuickBid--Plutonium\src\Main\QB\AS\Sign

    Which is the source directory along with the .XML file I referred to. 

    In TFS 2010, somebody created a custom build template, that is not longer valid, that called it by using "/c preptool.exe QBPrepTFS.xml"

    I couldn't post the error because it's known before you run the script. Intellisense tells me that the leaving it as is incorrect. 

    Tuesday, February 16, 2016 4:45 PM
  • Then do it this way:

     Set-Location "$env:TF_BUILDBINARIESDIRECTORY\QB\AS\Sign"
    .\preptool QBPrepTFS.xml

    Are you sure preptool is in that same folder? Can you see it there?  Your error seems to indicate that it isn't there.


    \_(ツ)_/

    Tuesday, February 16, 2016 5:30 PM
  • Please don't post pictures.

    The following command won't work, as you have noticed:


    $command QBPrepTFS.xml -NoNewWindow

    This is because the $command variable contains a string. (I explain this in my article.)

    If you want to execute the string as a command, that is the purpose of the invocation operator (&).

    Thus, you need to write this instead:


    & $command QBPrepTFS.xml -NoNewWindow


    -- Bill Stewart [Bill_Stewart]

    Tuesday, February 16, 2016 5:59 PM
    Moderator
  • Still doesn't address the issue of here the config file is or where preptool is.

    Too many methods and too much missing information.


    \_(ツ)_/

    Tuesday, February 16, 2016 6:03 PM
  • Agreed. Many build tools (such as visual studio) require environment variable settings to be configured correctly, and these often use shell scripts (.cmd or .bat batch files) to set environment variables in the current session. This won't work directly in PowerShell, as already noted. A workaround for this problem is given in the second article I posted.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, February 16, 2016 6:06 PM
    Moderator
  • Agreed. Many build tools (such as visual studio) require environment variable settings to be configured correctly, and these often use shell scripts (.cmd or .bat batch files) to set environment variables in the current session. This won't work directly in PowerShell, as already noted. A workaround for this problem is given in the second article I posted.

    -- Bill Stewart [Bill_Stewart]

    When a script or task is called in pre-build or post-build it is called with all variables and the path set.  In TFS 2015 and later we can call powershell and it will have direct access to the TFS objects so we would not necessarily use the environment.

    I doubt that preptool is located where the OP says it is.  I have never looked explicitly so I cannot validate this suspicion.  My only TFS server was just decommissioned and moved to the customers site so I cannot test this and the Azure version is not setup yet.

    The first thingfor the OP - BossHog - to do is to validate that the files exist at that location using Test-Path. After that it is all easy depending on which path to take.  Unfortunately it seems that the OP has no experience debugging programs so this is much harder.


    \_(ツ)_/

    Tuesday, February 16, 2016 6:15 PM
  • Appears to be another issue going on. I ran another build on the same machine that has been running for months with powershell scripts. The same error of not finding files is happening there too. I'll have to look into why that is happening. The files haven't changed locations.

    Anyway, I'll look into that, then revisit these suggestions once I have narrowed down my issue with the TFS build machine.

    Wednesday, February 17, 2016 4:43 PM