gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:12:29 24 4
gpt4 key购买 nike

这是我用来在 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”
  • 虚拟机名称“testOranges”

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

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

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

所以我的问题是:

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

我找到了很多使用自定义 dns 或将其加入域的信息...但我不想这样做,我只想指定我的自定义 DNSname.cloudapp.net 因为我计划在同一资源组中拥有多个虚拟机

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

WARNING: No deployment found in service: 'testBananas'

注 2:如果创建第二个虚拟机“test-Apples”,则 DNS 名称和公共(public) IP 与“test-Oranges”相同,但专用 IP 不同。 test-oranges 的外部 SSH 端口是 55525,test-apples 的外部 SSH 端口是 53456。如果我在两者中都使用 SSH 并执行“hostname”,则名称是匹配的...

我的脚本肯定有问题:-(

最佳答案

在您的#7 配置虚拟机中!你有

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

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

您遇到的问题是由于 ASM 不像 ARM 那样使用资源组。 (它对它们的使用有些不一致 - 它很大程度上用名称做自己的事情)

关于powershell - 如何在Azure ASM中指定DNS名称(域名)前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35273364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com