C# add Session host in azure host pool and trigger deployment

MAHESH NIKAM 1 Reputation point
2023-06-11T16:56:14.6566667+00:00

Hello,

I need help adding a session host in the Azure host pool using C# and Azure SDK.

I tried multiple ways to configure the VM template in HostPoolData.VmTemplate, but it's not getting triggered using C#. So please share the VmTemplate C# class and process to trigger deployment.

Below is the link for class:

https://github.com/Azure/azure-sdk-for-net/tree/a9e03d83467941a7596aff060a5b3109d94d694a/sdk/desktopvirtualization/Azure.ResourceManager.DesktopVirtualization

Thanks.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,862 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,542 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. msrini-MSFT 9,291 Reputation points Microsoft Employee
    2023-06-12T05:50:21.79+00:00

    Hi,

    Can you try the below C# code:

    using Azure.Identity;
    using Azure.ResourceManager.DesktopVirtualization;
    using Azure.ResourceManager.DesktopVirtualization.Models;
    using System;
    
    class Program
    {
        static void Main(string[] args)
        {
            string subscriptionId = "your-subscription-id";
            string resourceGroupName = "your-resource-group-name";
            string hostPoolName = "your-host-pool-name";
            string sessionHostName = "your-session-host-name";
            string vmTemplateName = "your-vm-template-name";
    
            // Authenticate using Azure.Identity
            var credential = new DefaultAzureCredential();
    
            // Create the DesktopVirtualizationManagementClient
            var client = new DesktopVirtualizationManagementClient(subscriptionId, credential);
    
            // Set the base URI if needed
            // client.BaseUri = new Uri("https://management.azure.com");
    
            // Set the resource group and host pool details
            var hostPoolUri = $"{resourceGroupName}/Microsoft.DesktopVirtualization/hostPools/{hostPoolName}";
    
            // Create the session host parameters
            var sessionHostParams = new SessionHost
            {
                AllowNewSession = true,
                Description = "My session host",
                FriendlyName = sessionHostName,
                HostPoolArmPath = hostPoolUri,
                VmTemplate = new WvdApiReference { Reference = vmTemplateName }
            };
    
            // Add the session host to the host pool
            client.SessionHosts.CreateOrUpdate(resourceGroupName, hostPoolName, sessionHostName, sessionHostParams);
    
            Console.WriteLine("Session host added successfully!");
        }
    }
    
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.