|
param
(
[parameter(Mandatory = $false)][switch]$AzureMoonCake,
[parameter(Mandatory = $false)][switch]$DoNotLogin,
[Parameter(Mandatory = $true)][string]$LocationName,
[Parameter(Mandatory = $true)][string]$ResourceGroupName,
[parameter(Mandatory = $true)][string]$VMName,
[parameter(Mandatory = $true)][string]$VhdUri,
[parameter(Mandatory = $true)][string]$VnetName,
[parameter(Mandatory = $true)][string]$SubnetName,
[parameter(Mandatory = $false)][switch]$HybridBenefit,
[parameter(Mandatory = $false)][string]$VMSizeName = "Standard_D2",
[parameter(Mandatory = $false)][string]$AvailabilitySetName,
[parameter(Mandatory = $false)][ValidateSet("Windows", "Linux")][string]$OS = 'Windows',
[parameter(Mandatory = $false)][string]$VnetPrefix = "10.140.0.0/16",
[parameter(Mandatory = $false)][string]$SubnetPrefix = "10.140.0.0/24",
[parameter(Mandatory = $false)][ValidateSet("Static", "Dynamic")][string]$PublicIPAllocationMethod = "Dynamic"
)
#检查Location是否存在,并返回结果
function Check-AzureRmLocation()
{
param
(
[string]$LocationName = $(throw "Parameter missing: -LocationName LocationName")
)
Write-Host "$(Get-Date) * Checking location $LocationName" -ForegroundColor Green
$Location = Get-AzureRmLocation | Where-Object { $_.Location -eq $LocationName }
If (-not ($Location))
{
Write-Host "$(Get-Date) * The location" $LocationName "does not exist." -ForegroundColor Red
return $false
}
Else
{
Write-Host "$(Get-Date) * Location $LocationName exists" -ForegroundColor Green
return $true
}
}
#检查RG是否存在,不存在则创建新的RG
function Check-AzureRmResourceGroup()
{
param
(
[string]$ResourceGroupName = $(throw "Parameter missing: -ResourceGroupName ResourceGroupName"),
[string]$LocationName = $(throw "Parameter missing: -LocationName LocationName")
)
Write-Host "$(Get-Date) * Checking resource group $ResourceGroupName, if not, created it." -ForegroundColor Green
Try
{
$ResourceGroup = Get-AzureRmResourceGroup -Name $ResourceGroupName -Location $LocationName -ErrorAction SilentlyContinue
If (-not ($ResourceGroup))
{
Write-Host "$(Get-Date) * Creating resource group" $ResourceGroupName "..." -ForegroundColor Green
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $LocationName -ErrorAction Stop
return $true
}
Else
{
Write-Host "$(Get-Date) * Resource group $ResourceGroupName exists" -ForegroundColor Green
return $true
}
}
Catch
{
Write-Host -ForegroundColor Red "$(Get-Date) * Create resource group" $LocationName "failed." $_.Exception.Message
return $false
}
}
#随机生成新的NIC
function AutoGenerate-AzureRmNetworkInterface()
{
param
(
[string]$ResourceGroupName = $(throw "Parameter missing: -ResourceGroupName ResourceGroupName"),
[string]$LocationName = $(throw "Parameter missing: -LocationName LocationName"),
[string]$VMName = $(throw "Parameter missing: -VMName VMName"),
[string]$SubnetName,
[string]$VnetName,
[string]$SubnetPrefix = "10.140.0.0/24",
[string]$VnetPrefix = "10.140.0.0/16",
[switch]$Create,
[string]$PublicIPAllocationMethod = "Dynamic"
)
Try
{
$RandomNum = Get-Random -minimum 100 -maximum 9999
$IpName = $VMName + "-ip" + $RandomNum
$NicName = $VMName + "-ni" + $RandomNum
Write-Host "$(Get-Date) * Auto generate network interface $NicName" -ForegroundColor Green
$Pip = New-AzureRmPublicIpAddress -Name $IpName -ResourceGroupName $ResourceGroupName -Location $LocationName -AllocationMethod $PublicIPAllocationMethod -ErrorAction Stop
if ($Create)
{
#Vnet does not exist
$Subnet = New-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetPrefix -ErrorAction Stop
$Vnet = New-AzureRmVirtualNetwork -Name $VnetName -ResourceGroupName $ResourceGroupName -Location $LocationName -AddressPrefix $VnetPrefix -Subnet $Subnet -ErrorAction Stop
$Nic = New-AzureRmNetworkInterface -Name $NicName -ResourceGroupName $ResourceGroupName -Location $LocationName -SubnetId $Vnet.Subnets[0].Id -PublicIpAddressId $Pip.Id -ErrorAction Stop
}
else
{
#Vnet exist
$Vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroupName -Name $VnetName -ErrorAction stop
$subnet = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $Vnet -Name $SubnetName -ErrorAction SilentlyContinue
if ($subnet -eq $null)
{
#subnet does not exist
Write-Host "$(Get-Date) * Subnet $SubnetName does not exist,create it,subnet prefix $SubnetPrefix" -ForegroundColor Green
$Subnet = New-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetPrefix -ErrorAction Stop
$vnet.subnets += $Subnet
#update Vnet
Set-AzureRmVirtualNetwork -VirtualNetwork $Vnet -ErrorAction Stop
#get vnet again
$Vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroupName -Name $VnetName -ErrorAction stop
$Subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $Vnet -ErrorAction stop
$Nic = New-AzureRmNetworkInterface -Name $NicName -ResourceGroupName $ResourceGroupName -Location $LocationName -SubnetId $Subnet.Id -PublicIpAddressId $Pip.Id -ErrorAction Stop
}
else
{
#subnet exist
Write-Host "$(Get-Date) * Subnet $SubnetName exist" -ForegroundColor Green
$Nic = New-AzureRmNetworkInterface -Name $NicName -ResourceGroupName $ResourceGroupName -Location $LocationName -SubnetId $Subnet.Id -PublicIpAddressId $Pip.Id -ErrorAction Stop
}
}
return $Nic.Id
}
Catch
{
Write-Host -ForegroundColor Red "$(Get-Date) * Auto generate network interface" $_.Exception.Message
return $false
}
}
#Login Azure with ARM mode
Import-Module AzureRM.Profile
$Error.Clear()
if (!$DoNotLogin)
{
if ($AzureMoonCake)
{
Write-Warning "$(Get-Date) * Current environment is Azure China(Mooncake)"
Login-AzureRmAccount -EnvironmentName AzureChinaCloud
}
else
{
Write-Warning "$(Get-Date) * Current environment is Azure Global"
Login-AzureRmAccount
}
if ($? -eq $true)
{
Write-Host "$(Get-Date) * Login succeeded!" -ForegroundColor Green
}
else
{
Write-Host $Error[0].Exception.Message -ForegroundColor Red
break
}
}
else
{
$CurrentSubscription = Get-AzureRmSubscription
if ($CurrentSubscription -eq $null)
{
Write-Warning "$(Get-Date) * Didn't find any subscription for now! Please login"
break
}
}
try
{
#check location
$Error.clear()
if (Check-AzureRmLocation -LocationName $LocationName)
{
#check RM resource group, if not exist, create one
if (Check-AzureRmResourceGroup -ResourceGroupName $ResourceGroupName -LocationName $LocationName)
{
#Check VM Name
If (Get-AzureRmVM -Name $VMName -ResourceGroupName $ResourceGroupName -ErrorAction Ignore)
{
Write-Host -ForegroundColor Red "$(Get-Date) * VM $VMName has already exist."
}
else
{
#Check VM Size
Write-Host "$(Get-Date) * Checking VM Size $VMSizeName" -ForegroundColor Green
If (Get-AzureRmVMSize -Location $LocationName | Where-Object { $_.Name -eq $VMSizeName })
{
Write-Host "$(Get-Date) * VM Size $VMSizeName exist" -ForegroundColor Green
If ($VhdUri)
{
#Create a network interface
$Vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroupName -Name $VnetName -ErrorAction SilentlyContinue
if ($Vnet -eq $null)
{
Write-Host "$(Get-Date) * Virtual network $VnetName does not exist,create it,vnet prefix $VnetPrefix" -ForegroundColor Green
$Nid = AutoGenerate-AzureRmNetworkInterface -Location $LocationName -ResourceGroupName $ResourceGroupName -VMName $VMName -VnetName $VnetName -VnetPrefix $VnetPrefix -SubnetName $SubnetName -SubnetPrefix $SubnetPrefix -PublicIPAllocationMethod $PublicIPAllocationMethod -Create
}
else
{
Write-Host "$(Get-Date) * Virtual network $VnetName exist" -ForegroundColor Green
$Nid = AutoGenerate-AzureRmNetworkInterface -Location $LocationName -ResourceGroupName $ResourceGroupName -VMName $VMName -VnetName $VnetName -VnetPrefix $VnetPrefix -SubnetName $SubnetName -SubnetPrefix $SubnetPrefix -PublicIPAllocationMethod $PublicIPAllocationMethod
}
If ($Nid)
{
Write-Host "$(Get-Date) * Creating VM $VMName ..." -ForegroundColor Green
if ($AvailabilitySetName)
{
Write-Host "$(Get-Date) * Verify availability set" -ForegroundColor Green
$AvailabilitySet = Get-AzureRmAvailabilitySet -ResourceGroupName $ResourceGroupName -Name $AvailabilitySetName -ErrorAction SilentlyContinue
if (!$AvailabilitySet)
{
write-host "$(Get-Date) * AvailabilitySet $AvailabilitySetName does not exist, create a new one" -ForegroundColor Green
$AvailabilitySet = New-AzureRmAvailabilitySet -ResourceGroupName $ResourceGroupName -Name $AvailabilitySetName -Location $LocationName -ErrorAction Stop
$VM = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSizeName -AvailabilitySetId $AvailabilitySet.Id -ErrorAction Stop
}
else
{
$VM = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSizeName -AvailabilitySetId $AvailabilitySet.Id -ErrorAction Stop
}
}
else
{
$VM = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSizeName -ErrorAction Stop
}
}
#Choose source image
#$VM = Set-AzureRmVMSourceImage -VM $VM -PublisherName $PublisherName -Offer $OfferName -Skus $SkusName -Version "latest" -ErrorAction Stop
#Add the network interface to the configuration.
$VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $Nid -ErrorAction Stop
$DiskName = "vmosdisk"
#$vmConfig = Set-AzureRmVMOSDisk -VM $vmConfig -Name $osDiskName -VhdUri $destinationVhd -CreateOption Attach -Linux
#$VM = Set-AzureRmVMOSDisk -VM $VM -Name $DiskName -VhdUri $OSDiskUri -CreateOption fromImage -Caching $OSDiskCaching -ErrorAction Stop
if ($OS -eq "Windows")
{
$VM = Set-AzureRmVMOSDisk -VM $VM -Name $DiskName -VhdUri $VhdUri -CreateOption attach -Caching None -Windows -ErrorAction Stop
}
else
{
$VM = Set-AzureRmVMOSDisk -VM $VM -Name $DiskName -VhdUri $VhdUri -CreateOption attach -Caching None -Linux -ErrorAction Stop
}
#Create a virtual machine
if ($HybridBenefit)
{
Write-Host "$(Get-Date) * Hybrid Benefit enabled for VM $VMName !" -ForegroundColor Green
New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $LocationName -VM $VM -ErrorAction Stop -LicenseType "Windows_Server" | Out-Null
}
else
{
New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $LocationName -VM $VM -ErrorAction Stop | Out-Null
}
Write-Host "$(Get-Date) * Create virtual machine $VMName successfully!" -ForegroundColor Green
#Set private nic to static
}
}
else
{
Write-Host -ForegroundColor Red "$(Get-Date) * VM Size $VMSizeName does not exist."
}
}
}
}
}
catch
{
Write-Host -ForegroundColor Red "$(Get-Date) * Create a virtual machine $VMName failed" $_.Exception.Message
}
|
|