通过 C# 创建 Azure VM 在创建资源组时抛出错误
Creating Azure VM via C# Throws Error While Creating Resource Group
我正在尝试以编程方式创建 VM...实际上,按照书中的示例。在 运行 该程序之前,我继续通过门户 https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal 创建了一个 Azure AD 应用程序和服务主体。 (附带一提,也许有人可以向我解释为什么需要这样做,但可以通过门户直接创建 VM 而无需创建服务 principal/AD 应用程序。)
在 运行 应用程序的同时,我能够成功创建管理客户端。下一步是创建资源组,这就是它失败并出现“System.Net.Http.HttpRequestException: 'No such host is known.'”错误的地方。请告知这里可能是什么问题。谢谢。
//Create the management client. This will be used for all the operations we will perform in Azure.
var credentials = SdkContext.AzureCredentialsFactory.FromFile("../../../azureauth.properties");
var azure = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(credentials).WithDefaultSubscription();
//Create a resource group
var groupName = "az204-ResourceGroup";
var vmName = "az204VMTesting";
var location = Region.USEast;
var vNetName = "az204VNET";
var vNetAddress = "172.16.0.0/16";
var subnetName = "az204Subnet";
var subnetAddress = "172.16.0.0/24";
var nicName = "az204NIC";
var adminUser = "azureadminuser";
var adminPassword = "Pa$$w0rd!2019";
Console.WriteLine($"Creating resource group {groupName} ... ");
//Below fails with 'No such host is known'
var resourceGroup = azure.ResourceGroups.Define(groupName).WithRegion(location).Create();
我在我的系统中试过这个代码。试试这个代码
using Microsoft.Azure.Management.Compute.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
namespace AzureVirtualMachine
{
class Program
{
static void Main(string[] args)
{
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal("clientId", "clientSecret", "tenantId", AzureEnvironment.AzureGlobalCloud);
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithSubscription("SubscriptionID”)
var groupName = "sampleResourceGroup";
var vmName = "VMWithCSharp";
var location = Region.EuropeWest;
var resourceGroup = azure.ResourceGroups.Define(groupName)
.WithRegion(location)
.Create();
var network = azure.Networks.Define("sampleVirtualNetwork")
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithAddressSpace("10.0.0.0/16")
.WithSubnet("sampleSubNet", "10.0.0.0/24")
.Create();
var publicIPAddress = azure.PublicIPAddresses.Define("samplePublicIP")
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithDynamicIP()
.Create();
var networkInterface = azure.NetworkInterfaces.Define("sampleNetWorkInterface")
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithExistingPrimaryNetwork(network)
.WithSubnet("sampleSubNet")
.WithPrimaryPrivateIPAddressDynamic()
.WithExistingPrimaryPublicIPAddress(publicIPAddress)
.Create();
var availabilitySet = azure.AvailabilitySets.Define("sampleAvailabilitySet")
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithSku(AvailabilitySetSkuTypes.Aligned)
.Create();
azure.VirtualMachines.Define(vmName)
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithExistingPrimaryNetworkInterface(networkInterface)
.WithLatestWindowsImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter")
.WithAdminUsername("sampleUser")
.WithAdminPassword("Sample123467")
.WithComputerName(vmName)
.WithExistingAvailabilitySet(availabilitySet)
.WithSize(VirtualMachineSizeTypes.StandardB1s)
.Create();
}
}
}
输出:
我通过将 AzureCredentialsFactory.FromFile 替换为 AzureCredentialsFactory.FromServicePrincipal 解决了这个问题。感谢 ShrutiJoshi-MT 的输入。我只是用必要的凭据创建了一个 json 文件。
我还有一些与授权相关的问题。事实证明我没有给 App Service 适当的授权级别。 post 帮助解决了这个问题:The client with object id does not have authorization to perform action 'Microsoft.DataFactory/datafactories/datapipelines/read' over scope.
最终代码:
string jsonString = File.ReadAllText("../../../azureauth.json");
AuthItem authItem = JsonSerializer.Deserialize<AuthItem>(jsonString);
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(authItem.ClientId, authItem.SecretValue, authItem.TenantId, AzureEnvironment.AzureGlobalCloud);
//Create the management client. This will be used for all the operations we will perform in Azure.
var azure = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(credentials).WithSubscription(authItem.Subscription);
//Create a resource group
var groupName = "az204-ResourceGroup";
var vmName = "az204VMTesting";
var location = Region.USEast;
var vNetName = "az204VNET";
var vNetAddress = "172.16.0.0/16";
var subnetName = "az204Subnet";
var subnetAddress = "172.16.0.0/24";
var nicName = "az204NIC";
var adminUser = "azureadminuser";
var adminPassword = "Pa$$w0rd!2019";
Console.WriteLine($"Creating resource group {groupName} ... ");
var resourceGroup = azure.ResourceGroups.Define(groupName).WithRegion(location).Create();
//Every VM needs to be connected to a virtual network
Console.WriteLine($"Creating virtual network {vNetName} ...");
var network = azure.Networks.Define(vNetName)
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithAddressSpace(vNetAddress)
.WithSubnet(subnetName, subnetAddress)
.Create();
//Any VM needs a network interface for connecting to the virtual network
Console.WriteLine($"Creating network interface {nicName} ... ");
var nic = azure.NetworkInterfaces.Define(nicName)
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithExistingPrimaryNetwork(network)
.WithSubnet(subnetName)
.WithPrimaryPrivateIPAddressDynamic()
.Create();
//Create the VM
Console.WriteLine($"Creating VM {vmName} ... ");
azure.VirtualMachines.Define(vmName)
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithExistingPrimaryNetworkInterface(nic)
.WithLatestWindowsImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter")
.WithAdminUsername(adminUser)
.WithAdminPassword(adminPassword)
.WithComputerName(vmName)
.WithSize(VirtualMachineSizeTypes.StandardDS2V2)
.Create();
我正在尝试以编程方式创建 VM...实际上,按照书中的示例。在 运行 该程序之前,我继续通过门户 https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal 创建了一个 Azure AD 应用程序和服务主体。 (附带一提,也许有人可以向我解释为什么需要这样做,但可以通过门户直接创建 VM 而无需创建服务 principal/AD 应用程序。)
在 运行 应用程序的同时,我能够成功创建管理客户端。下一步是创建资源组,这就是它失败并出现“System.Net.Http.HttpRequestException: 'No such host is known.'”错误的地方。请告知这里可能是什么问题。谢谢。
//Create the management client. This will be used for all the operations we will perform in Azure.
var credentials = SdkContext.AzureCredentialsFactory.FromFile("../../../azureauth.properties");
var azure = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(credentials).WithDefaultSubscription();
//Create a resource group
var groupName = "az204-ResourceGroup";
var vmName = "az204VMTesting";
var location = Region.USEast;
var vNetName = "az204VNET";
var vNetAddress = "172.16.0.0/16";
var subnetName = "az204Subnet";
var subnetAddress = "172.16.0.0/24";
var nicName = "az204NIC";
var adminUser = "azureadminuser";
var adminPassword = "Pa$$w0rd!2019";
Console.WriteLine($"Creating resource group {groupName} ... ");
//Below fails with 'No such host is known'
var resourceGroup = azure.ResourceGroups.Define(groupName).WithRegion(location).Create();
我在我的系统中试过这个代码。试试这个代码
using Microsoft.Azure.Management.Compute.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
namespace AzureVirtualMachine
{
class Program
{
static void Main(string[] args)
{
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal("clientId", "clientSecret", "tenantId", AzureEnvironment.AzureGlobalCloud);
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithSubscription("SubscriptionID”)
var groupName = "sampleResourceGroup";
var vmName = "VMWithCSharp";
var location = Region.EuropeWest;
var resourceGroup = azure.ResourceGroups.Define(groupName)
.WithRegion(location)
.Create();
var network = azure.Networks.Define("sampleVirtualNetwork")
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithAddressSpace("10.0.0.0/16")
.WithSubnet("sampleSubNet", "10.0.0.0/24")
.Create();
var publicIPAddress = azure.PublicIPAddresses.Define("samplePublicIP")
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithDynamicIP()
.Create();
var networkInterface = azure.NetworkInterfaces.Define("sampleNetWorkInterface")
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithExistingPrimaryNetwork(network)
.WithSubnet("sampleSubNet")
.WithPrimaryPrivateIPAddressDynamic()
.WithExistingPrimaryPublicIPAddress(publicIPAddress)
.Create();
var availabilitySet = azure.AvailabilitySets.Define("sampleAvailabilitySet")
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithSku(AvailabilitySetSkuTypes.Aligned)
.Create();
azure.VirtualMachines.Define(vmName)
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithExistingPrimaryNetworkInterface(networkInterface)
.WithLatestWindowsImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter")
.WithAdminUsername("sampleUser")
.WithAdminPassword("Sample123467")
.WithComputerName(vmName)
.WithExistingAvailabilitySet(availabilitySet)
.WithSize(VirtualMachineSizeTypes.StandardB1s)
.Create();
}
}
}
输出:
我通过将 AzureCredentialsFactory.FromFile 替换为 AzureCredentialsFactory.FromServicePrincipal 解决了这个问题。感谢 ShrutiJoshi-MT 的输入。我只是用必要的凭据创建了一个 json 文件。
我还有一些与授权相关的问题。事实证明我没有给 App Service 适当的授权级别。 post 帮助解决了这个问题:The client with object id does not have authorization to perform action 'Microsoft.DataFactory/datafactories/datapipelines/read' over scope.
最终代码:
string jsonString = File.ReadAllText("../../../azureauth.json");
AuthItem authItem = JsonSerializer.Deserialize<AuthItem>(jsonString);
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(authItem.ClientId, authItem.SecretValue, authItem.TenantId, AzureEnvironment.AzureGlobalCloud);
//Create the management client. This will be used for all the operations we will perform in Azure.
var azure = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(credentials).WithSubscription(authItem.Subscription);
//Create a resource group
var groupName = "az204-ResourceGroup";
var vmName = "az204VMTesting";
var location = Region.USEast;
var vNetName = "az204VNET";
var vNetAddress = "172.16.0.0/16";
var subnetName = "az204Subnet";
var subnetAddress = "172.16.0.0/24";
var nicName = "az204NIC";
var adminUser = "azureadminuser";
var adminPassword = "Pa$$w0rd!2019";
Console.WriteLine($"Creating resource group {groupName} ... ");
var resourceGroup = azure.ResourceGroups.Define(groupName).WithRegion(location).Create();
//Every VM needs to be connected to a virtual network
Console.WriteLine($"Creating virtual network {vNetName} ...");
var network = azure.Networks.Define(vNetName)
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithAddressSpace(vNetAddress)
.WithSubnet(subnetName, subnetAddress)
.Create();
//Any VM needs a network interface for connecting to the virtual network
Console.WriteLine($"Creating network interface {nicName} ... ");
var nic = azure.NetworkInterfaces.Define(nicName)
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithExistingPrimaryNetwork(network)
.WithSubnet(subnetName)
.WithPrimaryPrivateIPAddressDynamic()
.Create();
//Create the VM
Console.WriteLine($"Creating VM {vmName} ... ");
azure.VirtualMachines.Define(vmName)
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithExistingPrimaryNetworkInterface(nic)
.WithLatestWindowsImage("MicrosoftWindowsServer", "WindowsServer", "2012-R2-Datacenter")
.WithAdminUsername(adminUser)
.WithAdminPassword(adminPassword)
.WithComputerName(vmName)
.WithSize(VirtualMachineSizeTypes.StandardDS2V2)
.Create();