PowerShell Azure New-AzureRmVM没有响应 [英] PowerShell Azure New-AzureRmVM not responding

查看:59
本文介绍了PowerShell Azure New-AzureRmVM没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Azure环境中自动创建VM.一切运行良好,即使使用此脚本创建了VM,但该脚本也被挂起,每次我必须使用Ctrl + Alt + Delete结束脚本时.在放置调试器时,我看到执行被挂在代码的最后一行.只有在到达最后一行时,所有行的其余部分才能正常工作.

I am trying to automate VM creation in Azure environment. Everything is working fine, even the VM is getting created with this script, but the script is getting hanged and every time I have to use Ctrl+Alt+Delete to end the script. While putting debugger I am seeing the execution is getting hanged on the last line of my code. Rest of all the line is working fine only while coming to the last line its getting hanged.

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

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

I put the following code to read the status of VM creation. I want to read the status and update user accordingly. Unfortunately control is not coming here at all.

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.'
} 

以下是整个脚本.欢迎提出任何建议以摆脱这种情况.

Following are the entire script. Any suggestions are welcome to get rid of this.

[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
}

}

推荐答案

尝试一下-

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.'
}

这篇关于PowerShell Azure New-AzureRmVM没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆