如何在Azure ASM中指定DNS名称(域名)前缀?

时间:2016-02-08 15:35:42

标签: powershell azure dns

这是我用来在Azure中创建经典(ASM)VM的脚本

param(
[switch] $Help = $false,                                                      
$azurePassword,                                                               
$azureUsername ,                                                              
$azureSubscriptionName ,
$VMImageName,                                                                 
$azureStorageAccountName,
$AzureResourceGroupName,
$VMSize,                                                                      
$VMName,
$VMAdministratorUsername,
$VMAdministratorPassword,
$VNetName,
$VNetSubnetName
)

#Preparation steps
Clear-Host
$startTime = Get-Date
Write-Host -ForegroundColor Cyan "This script will create a classic VM '$VMName'"
Write-Host "Script started ($startTime)"
DetectPowershellVersion 4
$azurePassword = $azurePassword | ConvertTo-SecureString -asPlainText -Force
$azureCredentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $azureUsername,$azurePassword
Disable-AzureDataCollection -WarningAction SilentlyContinue

#1
Write-Host -ForegroundColor Cyan "Step 1: Logging in to Azure..."
Add-AzureAccount -credential $azureCredentials | Out-Null
Write-Host "Logged in."

#2
Write-Host -ForegroundColor Cyan "Step 2: Selecting subscription..."
Select-AzureSubscription -SubscriptionName $azureSubscriptionName -Current
New-AzureStorageAccount -StorageAccountName $azureStorageAccountName -Label $azureStorageAccountName -Location "North Europe" -ErrorAction SilentlyContinue
Set-AzureSubscription -SubscriptionName $azureSubscriptionName -CurrentStorageAccountName $azureStorageAccountName
Write-Host "Selected Subscription = $((Get-AzureSubscription -Current).SubscriptionName)"
Write-Host "Selected Storage Account Name = $azureStorageAccountName "

#3
Write-Host -ForegroundColor Cyan "Step 3: Locating VMImage subscription..."
$VMImage = (Get-AzureVMImage | Where { $_.Label -like "$($VMImageName)*" } | Sort-Object PublishedDate -Descending)[0]
write-host "Selected image = $($VMImage.Label)"
write-host "Selected image OS = $($VMImage.OS)"   

#4
Write-Host -ForegroundColor Cyan "Step 4: Locating requested size..."
write-host "Requested size = $VMSize"
$VMConfig = New-AzureVMConfig -Name $VMName -InstanceSize $VMSize -ImageName $VMImage.ImageName 

#5 
Write-Host -ForegroundColor Cyan "Step 5: Creating credentials for VM..."
if ($VMImage.OS -eq "Windows")
{
    $VMProvision = Add-AzureProvisioningConfig -Windows -AdminUsername $VMAdministratorUsername -Password $VMAdministratorPassword -VM $VMConfig
}
else #Linux
{
    $VMProvision = Add-AzureProvisioningConfig -Linux -LinuxUser $VMAdministratorUsername -Password $VMAdministratorPassword -VM $VMConfig
}
Write-Host "Credentials created. Username = $VMAdministratorUsername Password = *******"


#6 Delete VM if exists
Write-Host -ForegroundColor Cyan "Step 6: Deleting VM '$VMName' if exists..."
$vmstatus = Get-AzureVM -ServiceName $azureResourceGroupName -Name $VMName 
if ($vmstatus)
{
    Write-Host "VM $VMName detected."
    Write-Host "Stopping $VMName"   
    Stop-AzureVM -ServiceName $azureResourceGroupName -Name $VMName -Force
    Write-Host "Deleting VM $VMName"
    Remove-AzureVM -ServiceName $azureResourceGroupName -Name $VMName -DeleteVHD        
}
else
{
    Write-Host "VM $VMName NOT detected."
}


#7 PROVISION VM!
Write-Host -ForegroundColor Cyan "Step 7: Creating Virtual Machine in Azure..."
Write-Host "This will take a while"
Write-Host "Starting creation of VM $VMName"
Write-Host "Using VNET $VNETName and Subnet $VNetSubnetName"
Set-AzureSubnet -SubnetNames $VNetSubnetName -VM $VMConfig | Out-Null
New-AzureService -ServiceName $AzureResourceGroupName -Location "North Europe" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue

$VMResult = $VMProvision | New-AzureVM -ServiceName $AzureResourceGroupName -WaitForBoot -VNetName $VNetName

# END
$endTime = Get-Date
Write-Host "Creation of VM $VMName finished ($endTime)"
$elapsedTime = new-timespan $startTime $endTime 
Write-Host -ForegroundColor Cyan "SUCCESS: VM Creation time = $elapsedTime"

exit 0

我用参数(以及其他)来称呼它

  • AzureResourceGroupName" testBananas"
  • VMName" testOranges"

它正常工作并创建虚拟机,如屏幕截图所示 enter image description here

正如您所见,VMName是 testOranges ,它是在服务(资源组) testBananas 中创建的。

但DNS名称默认创建为 testBananas .cloudapp.net 我希望它是 testOranges .cloudapp.net或我指定的任何内容。我认为默认情况下会填充DNS名称。

所以我的问题是:

如何在Azure ASM中指定DNS名称前缀?

我发现了很多信息要使用自定义DNS或将其加入域...但我不想这样做,我只是想指定我的自定义DNSname.cloudapp.net,因为我打算在同一个资源组中有几个VM

注意1 :当我执行脚本时,我收到此警告:

  

警告:在服务中未找到任何部署:' testBananas'

注意2 :如果创建第二个VM" test-Apples"虽然私有IP不同,但DNS名称和PublicIP与" test-Oranges"相同。测试橙的外部SSH端口为55525,测试苹果的外部SSH端口为53456.如果我同时使用SSH并执行"主机名"这些名字是匹配的......

我的脚本定义错误: - (

1 个答案:

答案 0 :(得分:1)

在#7 Provision VM中!你有

New-AzureService -ServiceName $AzureResourceGroupName `
                 -Location "North Europe" `
                 -ErrorAction SilentlyContinue `
                 -WarningAction SilentlyContinue

$AzureResourceGroupName更改为TestOranges会为您提供所需的结果

您遇到的问题是由于ASM没有像ARM那样使用资源组。 (它对它们的使用有点不一致 - 主要是用自己的名字来表达)