PowerShell Azure New-AzureRmVM没有响应

时间:2016-08-22 11:20:44

标签: powershell azure virtual-machine

我正在尝试在Azure环境中自动创建VM。一切都运行正常,甚至VM都是用这个脚本创建的,但是脚本被挂起了,每次我都要用Ctrl + Alt + Delete来结束脚本。在调试器的时候,我看到执行程序在代码的最后一行被挂起了。剩下的所有线路只有在到达最后一条线路时才能正常工作。

$result = New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VirtualMachine -Verbose

我输入以下代码来读取VM创建的状态。我想读取状态并相应地更新用户。不幸的是,控制权根本就没有到来。

if($result.Status -eq 'Succeeded') {  
$result
Write-Verbose ('VM named ''{0}'' is now ready, you can connect using username: {1} and password: {2}' -f $VMName, $username, $password)
} else {
    Write-Error 'Virtual machine was not created successfully.'
} 

以下是整个脚本。欢迎任何建议摆脱这一点。

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Azure Automation"
$objForm.Size = New-Object System.Drawing.Size(413, 320) 
$objForm.StartPosition = "CenterScreen"
$objForm.TopMost=$true


#$objForm.AutoScaleDimensions= New-Object System.Drawing.SizeF(6F 13F) 
#$objForm.AutoScaleMode = New-Object System.Windows.Forms.AutoScaleMode.Font

# Project Name Label
$lblProjectName = New-Object System.Windows.Forms.Label
 $lblProjectName.AutoSize = $true;
 $lblProjectName.Location = New-Object System.Drawing.Point(23, 55)
 $lblProjectName.Size = New-Object System.Drawing.Size(83, 13)
 $lblProjectName.TabIndex = 0
 $lblProjectName.Text = "Project Name"
 $objForm.Controls.Add($lblProjectName)

 #Project Name TextBox
 $txtProjectName = New-Object System.Windows.Forms.TextBox
 $txtProjectName.Location = New-Object System.Drawing.Point(151, 55)
 $txtProjectName.MaxLength = 9
 $txtProjectName.Name = "txtProjectName"
 $txtProjectName.Size = New-Object System.Drawing.Size(146, 20);
 $txtProjectName.TabIndex=1
 $objForm.Controls.Add($txtProjectName)

 #Label VM Size
$lblVMSize = New-Object System.Windows.Forms.Label
$lblVMSize.AutoSize = $true
$lblVMSize.Location = New-Object System.Drawing.Point(23, 97)
$lblVMSize.Name = "lblVMSize";
$lblVMSize.Size = New-Object System.Drawing.Size(53, 13)
$lblVMSize.TabIndex = 2
$lblVMSize.Text = "VM Size"
$objForm.Controls.Add($lblVMSize)

 #Combo VM Size
 $cmbVMSize = New-Object 'System.Windows.Forms.ComboBox' 
 $cmbVMSize.Location = New-Object System.Drawing.Point(151, 94)
 $cmbVMSize.Name = "cmbVMSize";
 $cmbVMSize.Size = New-Object System.Drawing.Size(146, 21);
 $cmbVMSize.TabIndex = 3;
 $cmbVMSize.SuspendLayout() 
 [void]$cmbVMSize.Items.Add('Standard_A0') 
 [void]$cmbVMSize.Items.Add('Standard_A1') 
 [void]$cmbVMSize.Items.Add('Standard_A2') 
 [void]$cmbVMSize.Items.Add('Standard_A3') 
 [void]$cmbVMSize.Items.Add('Standard_A4') 
 $cmbVMSize.ResumeLayout($false) 
 [void]$objForm.Controls.Add($cmbVMSize) 

  #Lable OS
 $lblOS = New-Object System.Windows.Forms.Label
 $lblOS.AutoSize = $true;
 $lblOS.Location = New-Object System.Drawing.Point(23, 143)
 $lblOS.Name = "lblOS";
 $lblOS.Size = New-Object System.Drawing.Size(106, 13)
 $lblOS.TabIndex = 4
 $lblOS.Text = "Operating System"
 [void]$objForm.Controls.Add($lblOS)

  #Combo OS
 $cmbOS = New-Object 'System.Windows.Forms.ComboBox' 
 $cmbOS.Location = New-Object System.Drawing.Point(151, 140)
 $cmbOS.Name = "cmbOS";
 $cmbOS.Size = New-Object System.Drawing.Size(146, 21);
 $cmbOS.TabIndex = 5;
 $cmbOS.SuspendLayout() 
 [void]$cmbOS.Items.Add('Windows Server') 
 [void]$cmbOS.Items.Add('Ubuntu Server') 
 [void]$cmbOS.Items.Add('Red Hat Enterprise Linux') 
 [void]$cmbOS.Items.Add('SQL Data Center') 
 [void]$cmbOS.Items.Add('Oracle Data Center') 
 $cmbOS.ResumeLayout($false) 
 [void]$objForm.Controls.Add($cmbOS) 

  #Lable DB
 $lblDB = New-Object System.Windows.Forms.Label
 $lblDB.AutoSize = $true;
 $lblDB.Location = New-Object System.Drawing.Point(23, 185)
 $lblDB.Size = New-Object System.Drawing.Size(61, 13)
 $lblDB.TabIndex = 6
 $lblDB.Text = "Database"
 [void]$objForm.Controls.Add($lblDB)

   #Combo DB
 $cmbDB = New-Object 'System.Windows.Forms.ComboBox' 
 $cmbDB.Location = New-Object System.Drawing.Point(151, 182)
 $cmbDB.Name = "cmbDB";
 $cmbDB.Size = New-Object System.Drawing.Size(146, 21);
 $cmbDB.TabIndex = 7;
 $cmbDB.SuspendLayout() 
 [void]$cmbDB.Items.Add('SQL2014-WS2012R2') 
 [void]$cmbDB.Items.Add('SQL2014SP1-WS2012R2') 
 $cmbDB.ResumeLayout($false) 
 [void]$objForm.Controls.Add($cmbDB) 

 # Submit Button
$submitbutton = New-Object System.Windows.Forms.Button
$submitbutton.Location = New-Object System.Drawing.Size(26, 249)
$submitbutton.Size = New-Object System.Drawing.Size(75,23)
$submitbutton.Text = "Submit"
$submitbutton.UseVisualStyleBackColor = $false;
$submitbutton.TabIndex = 8
$submitbutton.Add_Click($Button_Click)
[void]$objForm.Controls.Add($submitbutton)


$objForm.Add_Shown({$objForm.Activate()})
#$objForm.ShowDialog()
 [System.Windows.Forms.Application]::Run($objForm)

 $Button_Click = 
{
    #Assign unique number 
    $randomNumber = Get-Random -minimum 1 -maximum 1000 
    #Value taken from UI 
    $ProjectName = $txtProjectName.Text.ToLower();
    $VMSize = $cmbVMSize.SelectedItem
    $OfferName = $cmbDB.SelectedItem
    #Internal Value
    $Location = "westeurope"
    $ResourceGroupName = $ProjectName + $randomNumber
    $StorageName = $ProjectName + "storage"
    $StorageSku = "Standard_LRS"
    $InterfaceName = $ResourceGroupName + "ServerInterface"
    $VNetName = $ResourceGroupName + "VNet"
    $SubnetName = "Default"
    $VNetAddressPrefix = "10.0.0.0/16"
    $VNetSubnetAddressPrefix = "10.0.0.0/24"
    $TCPIPAllocationMethod = "Dynamic"
    $DomainName = $ProjectName
    $VMName = $ResourceGroupName + "VM"
    $ComputerName = $ResourceGroupName + "Server"   
    $OSDiskName = $VMName + "OSDisk"
    $PublisherName = "MicrosoftSQLServer"    
    $Sku = "Standard"
    $Version = "latest"


    ## Main Logic to connect VM
    # Step 1: Install Azure PowerShell
    Try
{
    $objForm.Close()
    Login-AzureRmAccount
    #Start-Sleep -s 10
    New-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location
    Get-AzureRmStorageAccountNameAvailability $ResourceGroupName
    $StorageAccount = New-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageName -SkuName $StorageSku -Kind "Storage" -Location $Location

    if(-not $StorageAccount)
    {  
    throw "Unable to find storage account '$StorageName'. Cannot continue."
    }
    # Enable verbose output and stop on error 
    # Create the VNET
    Write-Verbose 'Creating Virtual Network'  
    $SubnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $VNetSubnetAddressPrefix
    $VirtualNetwork = New-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName -Location $Location -AddressPrefix $VNetAddressPrefix -Subnet $SubnetConfig
    # Create the NIC
    Write-Verbose 'Creating Public IP'  
    $PublicIpAddress = New-AzureRmPublicIpAddress -Name $InterfaceName -ResourceGroupName $ResourceGroupName -Location $Location -AllocationMethod $TCPIPAllocationMethod -DomainNameLabel $DomainName
    $InterfaceNic = New-AzureRmNetworkInterface -Name $InterfaceName -ResourceGroupName $ResourceGroupName -Location $Location -SubnetId $VirtualNetwork.Subnets[0].Id -PublicIpAddressId $PublicIpAddress.Id
    # Credentials 
    Write-Verbose 'Entering Credentials'  
    $username = "indmukhe"
    $password = ConvertTo-SecureString –String "P@ssword" –AsPlainText -Force
    $Credential = new-object management.automation.pscredential $username,$password
    #$Credential = Get-Credential 
    # Specify the VM name and size
    Write-Verbose 'Creating VM Config'  
    $VirtualMachine = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize
    $VirtualMachine = Set-AzureRmVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $ComputerName -Credential $Credential -ProvisionVMAgent -EnableAutoUpdate #-TimeZone = $TimeZone
    $VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $InterfaceNic.Id
    $OSDiskUri = $StorageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + $OSDiskName + ".vhd"
    $VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -Name $OSDiskName -VhdUri $OSDiskUri -Caching ReadOnly -CreateOption FromImage 
    $VirtualMachine = Set-AzureRmVMSourceImage -VM $VirtualMachine -PublisherName $PublisherName -Offer $OfferName -Skus $Sku -Version $Version
    Write-Verbose 'Creating VM...' 
    $result = New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VirtualMachine -Verbose
    if($result.Status -eq 'Succeeded') {  
    $result
    Write-Verbose ('VM named ''{0}'' is now ready, you can connect using username: {1} and password: {2}' -f $VMName, $username, $password)
    } else {
        Write-Error 'Virtual machine was not created successfully.'
    }  


}
Catch
{
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName

    write-Error $ErrorMessage
    write-Error $FailedItem  

}
Finally
{      

    $objForm.Dispose()
    exit 1
}

}

1 个答案:

答案 0 :(得分:0)

试试这个 -

if($result.ProvisioningState -eq "Succeeded")
{  
    Write-Verbose ('VM named ''{0}'' is now ready, you can connect using    username: {1} and password: {2}' -f $VMName, $username, $password)
} 
else
{
    Write-Error 'Virtual machine was not created successfully.'
}