为新部署分配随机ID,DNS名称

时间:2015-07-21 15:01:10

标签: c# powershell azure virtual-machine

这只是我创建新部署时的一个问题。将AzureVms部署到现有部署(或现有云服务)可正常工作。但是,我只能使用PowerShell而不是c#来正确创建新的部署。

确实有效的Powershell:

$imageName = (Get-AzureVMImage | Where-Object { $_.Label -eq "Windows 7 Enterprise SP1 (x64)" } | Sort-Object PublishedDate -Descending | Select-Object -First 1).ImageName
$vm = New-AzureVMConfig -Name "MyName01" -InstanceSize "Small" -Image $imageName
$vm | Add-AzureProvisioningConfig -TimeZone "Eastern Standard Time" -Windows -AdminUsername "Username01" -Password "Password01"
$vm | Set-AzureSubnet "MySubnet" | Out-Null
$operation = New-AzureVM -ServiceName "MyCloudService" -Location "Central US" -VMs $vm -VNetName "MyVnet"
if($operation.OperationStatus -ne "Succeeded") {throw "Operation Status = " + $operation.OperationStatus}

执行此操作时,会正确创建VM。我在这里正确地定义了三个标准,除了它已经配置并且我可以在RDP中:   - cmdlet Get-AzureVM返回VM   - Azure门户中的DNS名称>虚拟机节点(不是预览门户)是mycloudservice.cloudapp.net   - 部署名称称为mycloudservice

我已尝试过多次c#迭代,但作为一个例子,如果云服务还不存在则不起作用。我尝试过提供和不提供DNSSetting值。我的定义并不像以下那样工作:   - cmdlet Get-AzureVM不返回VM   - Azure门户(不是预览门户)中的DNS名称是一些随机的字母数字字符串+' .cloudapp.net'   - 部署名称与DNS主机相同的随机字母数字字符串

using (var computeclient = new ComputeManagementClient(credentials))
{
//create cloud service if it doesn't exist
if(computeclient.HostedServices.Get(cloudServiceName) == null)
computeclient.HostedServices.Create(
new HostedServiceCreateParameters
{
    Label = cloudServiceName,
    Location = "Central US",
    ServiceName = cloudServiceName
});

//define parameters specific to the vm (role)
var deploymentAttributes = new Role
{
    RoleName = hostname,
    RoleSize = createParameters.VmSize,
    RoleType = VirtualMachineRoleType.PersistentVMRole.ToString(),
    DataVirtualHardDisks = dataHardDrives,
    ConfigurationSets = configurationSets,
    ProvisionGuestAgent = true,
    Label = cloudServiceName,
    VMImageName = createParameters
};


//create deployment parameters and kick off!
DnsServer dnsServer = new DnsServer();
dnsServer.Address = dnsIp;
dnsServer.Name = dnsHostname;
DnsSettings dnsSettings = new DnsSettings();
dnsSettings.DnsServers = new List<DnsServer>() { dnsServer };
var createDeploymentParameters = new     VirtualMachineCreateDeploymentParameters
{
    Name = cloudServiceName,
    Label = cloudServiceName,
    DeploymentSlot = DeploymentSlot.Staging,
    Roles = new List<Role> { deploymentAttributes },
    VirtualNetworkName = vnetName,
    DnsSettings = dnsSettings,
};
var deploymentResult = computeclient.VirtualMachines.CreateDeployment(
    cloudServiceName,
    createDeploymentParameters);
}

1 个答案:

答案 0 :(得分:0)

刚想出来 - 发布这个问题给了我一个想法。简单地部署到生产而不是暂存修复此问题(DeploymentSlot = DeploymentSlot.Production)。