powershellvisual-studioscripting

Is there anyway to launch multiple Visual Studio projects, all in the same solution, for debugging, from powershell?


The reason I believe I can't just set multiple start up projects is that they have to start in a certain order, and each one has to be fully initialized before the next project starts, and so I need to put delays in.

I've been using PowerShell, but I just can't figure out how to launch a specific project.

Right now my script looks like: '''

# Load the Visual Studio DTE COM object
$visualStudio = [Runtime.InteropServices.Marshal]::GetActiveObject("VisualStudio.DTE.17.0")  # Use 17.0 for VS2022

if ($null -eq $visualStudio) {
    Write-Host "No active Visual Studio instance found!"
    exit
}

# Path to the solution
$solution = $visualStudio.Solution

if ($solution.IsOpen) {
Write-Host "Solution is open: $($solution.FullName)"

# Get the projects in the solution
$projects = $solution.Projects

# Define the list of project names you want to start
$desiredProjects = @(
    "Proj1",
    "Proj2",
    "Proj3"
)

foreach ($desiredProjectName in $desiredProjects) {
    $projectFound = $null

    # Loop through each project to find by name
    for ($i = 1; $i -le $projects.Count; $i++) {
        $project = $projects.Item($i)
        Write-Host "Checking Project: $($project.Name)"
        
        if ($project.Name -eq $desiredProjectName) {
            $project.DTE.ExecuteCommand("ClassViewContextMenus.ClassViewProject.Debug.Startnewinstance")
            Write-Host "Found project: $($project.Name)"
            Write-Host "Project Type: $($project.Properties.Item('OutputType').Value)"
            
            # Set the project as the startup project
            # $solution.Properties.Item("StartupProject").Value = $project.UniqueName
            # Write-Host "Set startup project to: $($project.Name)"

            # Start debugging
            Write-Host "Started debugging for project: $($project.Name)"
            
            # Optional: Add delay before starting the next project
            Start-Sleep -Seconds 5  # Delay of 5 seconds (adjust as needed)

            break
        }
    }
}

'''

The problem is that this always just launches whatever I have set as the startup project. I would like it to start those projects in the list, in order, with that 5 second delay.

I've tried changing the start up project, but once you've started debugging, that is no longer allowed.

So is there any way for to launch all three from the same instance of visual studio?


Solution

  • Poked around for a bit longer and found a solution, if anyone else comes across this and wants to do something similar, here is what I found

    It seems that it might be impossible to actually launch multiple projects the same way "Debug > Start New Instance" does from the context menu.

    What I ended up doing is launching the three executables and attaching the debug service to them.

    # Paths to the project executables
    $projects = @(
        "path\to\proj1\Proj1.exe",
        "path\to\Proj2\Proj2.exe",
        "path\to\Proj3\Proj3.exe"
    )
    
    foreach ($project in $projects) {
        $process = Start-Process $project -PassThru
    
        # Attach the debugger
        $processId = $process.Id
        Write-Host "ProcessId: $($processId)"
    
        $visualStudio = [Runtime.InteropServices.Marshal]::GetActiveObject("VisualStudio.DTE.17.0")
        # Get the list of local processes available for debugging
        $processes = $visualStudio.Debugger.LocalProcesses
    
        # Loop through the processes to find the one with the matching Process ID
        foreach ($runningProcess in $processes) {
            if ($runningProcess.ProcessID -eq $processId) {
                    Write-Host "Attaching debugger to process: $($process.Name) (PID: $($process.ProcessID))"
                    $runningProcess.Attach()  # Attach the debugger to the process
                    Write-Host "Debugger attached to process: $($process.Name)"
                }
        }
        Start-Sleep -Seconds 5
    }