Windows Runner to Build using Visual Studio 2022
Asked by David V_ Corbin:

I have created a runner, and registered it. (I can dequeue a job to it that is a "do nothing job"...  

But after many hours.... I simply can not get Visual Studio to be able to bve configured and launch...  

Any chance of an actual YML file that illustrates building a solution:  

**- devenv mysolution.sln**   

<br />


Answers:
author: Patrik S
body: Hello [@David V_ Corbin](/t5/user/viewprofilepage/user-id/2822389) ,

Thank you for reaching out to Atlassian Community!

Essentially, as Windows runners are executed directly in powershell, you should be able to execute the same commands you would be able to execute outside the runner, directly in a Powershell session of your machine.

According to [Microsoft's official documentation](https://learn.microsoft.com/en-us/visualstudio/ide/reference/devenv-command-line-switches?view=vs-2019), you should be able to build a Visual Studio solution from the command line by using a command like the one below :

```
devenv "repos\MySolution.sln" /build Debug /project "CSharpWinApp\CSharpWinApp.csproj" /projectconfig Debug
```

You can refer to [devenv build](https://learn.microsoft.com/en-us/visualstudio/ide/reference/build-devenv-exe?view=vs-2019) documentation for more details about the available flags and what each of them stands for.

Have you tried using a command similar to that in your pipeline YML script? If yes, does it throw any error during the execution? I would suggest also trying running the same command in a PowerShell session outside of the runner to confirm if it's executed properly in the host machine.

Let me know in case you have any questions.

Thank you, [@David V_ Corbin](/t5/user/viewprofilepage/user-id/2822389) !  
Patrik S

comments: author: David V_ Corbin
body: You are missing key parts, this is indicitive not actually having gotten this to work... lets see... Before one can invoke devenv, it is necessary to initialize the environment, This is done by invoking a .BAT file (not a .cmd nor a .ps1) - AND the path to this file will have spaces in it....   

Now with PowerShell, when you quote something, it becomes an expression, so to invoke, one starts the line with an ampersand \[\&\].   

If I were to write my own .ps1 script and do all the work there, it would not be a problem.... BUT that is not the question... the question is how to do it \*in the YAML\* so that it is direct in the pipeline and does not require an (albeit trivial) supporting file.  

If you can please post a bitbucket-pipelines.yml that functions, it would be greatly appreciated.  

For reference, this one does NOT  

pipelines:  
custom:  
Simple_DevEnv_Pipeline:  
- step:  
name: Step 1  
runs-on:  
- 'windows'  
script:  
- echo "What do we Have?";  
- dir  
- echo "Setup Visual Studio Environment"  
- '"C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\Common7\\Tools\\VsDevCmd.bat"'  
- echo "Simple DevEnv...";  
- devenv.exe LiveCode.sln  
caches:  
- windows-bundler  
definitions:  
caches:  
windows-bundler: vendor/bundle  


author: David V_ Corbin
body: (NOTE: The indentation is proper and no error are generated with the aboce. However the environment variables that need to be set via VsDevCmd.bat (including but not limited to path) are not. As a result, the call to devenv.exe fails.


author: Patrik S
body: Hello [@David V_ Corbin](/t5/user/viewprofilepage/user-id/2822389) ,

Thank you for sharing additional context.

The issue here seems that environment variables are not transferred from the child process to the parent process.

Windows runners are executed in a PowerShell session, so when you call the batch script VsDevCmd.bat, this script will run in a separate process that inherits its parent process (powershell) variables. However, any changes to the environment variables that happen in that batch script itself are only scoped to its own process.

This means that when the batch script is completed, all the changes it applied to environment variables will **not** be persisted in the PowerShell session. Unfortunately, there doesn't seem to be an easy way to transfer environment variables from a child batch process to the parent PowerShell session.

After some research, I found this [Stackoverflow thread](https://stackoverflow.com/questions/4384814/how-to-call-batch-script-from-powershell) where some users recommend using the [Powershell Community Extension](https://github.com/Pscx/Pscx) which includes an **Invoke-BatchFile**command that can be used to run a batch file and persist any environment variable modification executed in the batch file. I was able to test it using the following steps :

**1.** Install the Powershell community extension which will enable the usage of **Invoke-BatchFile**command

```
Install-Module Pscx -Scope CurrentUser
```

Please note that this extension is not developed nor officially supported by either Atlassian or Microsoft.

**2.** Create an example script called **my_script.bat** to set a test environment variable:

```
set MY_VARIABLE=123
```

**3.** Execute the batch file in the YML file using **Invoke-BatchFile**and print the test variable :

```
pipelines:
  custom:
      TestEnvVariable:
      - step:
          name: Step 1
          runs-on:
            - 'windows'
          script:
           - Invoke-BatchFile "C:\Users\Administrator\Documents\my_script.bat"
           - $env:MY_VARIABLE
```

You could use a similar approach to execute the VS batch script that sets the environment, and with the variables persisted in the PowerShell session, PowerShell should be able to find the devenv.exe in PATH.

Thank you, [@David V_ Corbin](/t5/user/viewprofilepage/user-id/2822389) !

Patrik S


author: David V_ Corbin
body: [@Patrik S](/t5/user/viewprofilepage/user-id/4603309) - Thanks. Your analysis is spot on. What had blocked me was that semantics valid in other places \[outside of BitBuket yml pipelines\] such as using an "\&" before a quoted string, or using "cmd /k" and then running in a sub-command until an exit... and the like did not work ... when done inline in the yml file \[all worked is I was willing to put the commands in a .ps1 script, but that incretes maintenance and hides visibility...  

I will definitely checkout that Invoke-BatchFile (tomorrow, too late today - dinner is calling)


Tags: bitbucket-cloud

cloud