Wie kann ich mit C# eine Azure VM mit spezifischen Umgebungsvariablen erstellen? Das Betriebssystem der VM sollte Windows 11 sein.
To create an Azure VM with specific environment variables using C# and Windows 11, you need to follow these steps:

1. Install the Azure Management Libraries for .NET by running the following command in the Package Manager Console:
``` 
Install-Package Microsoft.Azure.Management.Fluent
``` 
2. Set up authentication by creating an Azure Active Directory (AD) app and granting it the required permissions. You can do this via the Azure portal or with the Azure CLI. Get the Tenant ID, Client ID, and Client Secret after setting up the app.

3. Create a C# console application in Visual Studio and add the following namespaces:
``` 
using Microsoft.Azure.Management.Compute.Fluent;
using Microsoft.Azure.Management.Compute.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using System;
``` 
4. Add the following code to create a Windows 11 VM with the specific environment variables:
``` 
public static async Task Main(string[] args)
{
    // Fill in your Azure AD app credentials
    var clientId = "YOUR_CLIENT_ID";
    var clientSecret = "YOUR_CLIENT_SECRET";
    var tenantId = "YOUR_TENANT_ID";
    var subscriptionId = "YOUR_SUBSCRIPTION_ID";

    // Authenticate with Azure
    var credentials = new AzureCredentials(new ServicePrincipalLoginInformation
    {
        ClientId = clientId,
        ClientSecret = clientSecret
    }, tenantId, AzureEnvironment.AzureGlobalCloud);

    // Initialize the Azure management client
    var azure = Azure.Authenticate(credentials).WithSubscription(subscriptionId);

    // Set up the VM configuration
    var groupName = "yourResourceGroup";
    var vmName = "yourVmName";
    var location = Region.USWest;

    // Create or update the resource group
    var resourceGroup = await azure.ResourceGroups.Define(groupName)
        .WithRegion(location)
        .CreateAsync();

    // Define the VM
    var vm = await azure.VirtualMachines.Define(vmName)
        .WithRegion(location)
        .WithExistingResourceGroup(resourceGroup)
        .WithNewPrimaryNetwork("10.0.0.0/28")
        .WithPrimaryPrivateIPAddressDynamic()
        .WithNewPrimaryPublicIPAddress("yourPublicIP")
        .WithPopularWindowsImage(KnownWindowsVirtualMachineImage.Windows11Pro)
        .WithAdminUsername("yourAdminUsername")
        .WithAdminPassword("yourAdminPassword")
        .WithSize(VirtualMachineSizeTypes.StandardD2sV3)
        .DefineNewExtension("CustomScriptExtension")
            .WithPublisher("Microsoft.Compute")
            .WithType("CustomScriptExtension")
            .WithVersion("1.10")
            .WithMinorVersionAutoUpgrade()
            .WithPublicSetting("fileUris", new List<string> { "https://yourScriptUrl/set-environment-variables.ps1" })
            .WithPublicSetting("commandToExecute", "powershell -ExecutionPolicy Unrestricted -File set-environment-variables.ps1")
            .Attach()
        .CreateAsync();

    Console.WriteLine("VM created!");
}
``` 
5. Replace placeholders with your actual Azure credentials and other details. You will also need to replace https://yourScriptUrl/set-environment-variables.ps1 with the actual URL of your PowerShell script that sets the environment variables.

6. Create a PowerShell script named set-environment-variables.ps1 and upload it to a publicly accessible location. The script should set your desired environment variables, like this:
```
[System.Environment]::SetEnvironmentVariable("VAR_NAME1", "VAR_VALUE1", [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::SetEnvironmentVariable("VAR_NAME2", "VAR_VALUE2", [System.EnvironmentVariableTarget]::Machine)
# Add more environment variables as needed

# Restart the VM to apply the environment variables
Restart-Computer -Force
```
Replace VAR_NAME1, VAR_VALUE1, VAR_NAME2, and VAR_VALUE2 with your desired environment variable names and values. Add more lines as needed to set additional environment variables.

This script sets the environment variables at the machine level, which means they will be available for all users. If you want to set them for a specific user, replace [System.EnvironmentVariableTarget]::Machine with [System.EnvironmentVariableTarget]::User.

After completing these steps, your C# console application will create an Azure VM with the specified environment variables. The VM will have Windows 11 as its operating system. Note that you may need to update the KnownWindowsVirtualMachineImage enumeration in the code if the Windows 11 image is not yet available. In this case, use a Windows Server image and manually upgrade to Windows 11.