2016-08-22 4 views
0

Ich versuche, VM-Erstellung in Azure-Umgebung zu automatisieren. Alles funktioniert gut, sogar die VM wird mit diesem Skript erstellt, aber das Skript wird gehängt und jedes Mal muss ich Strg + Alt + Entf verwenden, um das Skript zu beenden. Während ich den Debugger setze, sehe ich, dass die Ausführung in der letzten Zeile meines Codes aufgehängt wird. Rest der ganzen Linie funktioniert gut, nur wenn man zur letzten Zeile kommt, wird es gehängt.PowerShell Azure Neu-AzureRmVM reagiert nicht

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

Ich legte den folgenden Code, um den Status der VM-Erstellung zu lesen. Ich möchte den Status lesen und den Benutzer entsprechend aktualisieren. Leider kommt die Kontrolle nicht hierher.

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

Folgende sind das gesamte Skript. Irgendwelche Vorschläge sind willkommen, das zu beseitigen.

[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 "[email protected]" –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 
} 

} 

Antwort

0

Versuchen this-

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