首页 > 解决方案 > How to see logs for failure reason when creating VM

问题描述

I am creating an Azure VM using the below PowerShell script

$username = 'xxxxxxx'
$password = ConvertTo-SecureString 'xxxxxxxx&Cx4cA' -AsPlainText -Force
$WindowsCred = New-Object System.Management.Automation.PSCredential ($username, $password)

New-AzVm `
    -ResourceGroupName "1-83eb7c26-playground-sandbox" `
    -Name "aznewvm23" `
    -Location 'eastus' `
    -VirtualNetworkName "mynewazvm23" `
    -SubnetName "default" `
    -SecurityGroupName "mynewvmNSG23" `
    -PublicIpAddressName "mypublicip23" `
    -Credential $WindowsCred `
    -OpenPorts 80,3389 `
    -AsJob

And when I see job status using Get-Job it's getting failed.

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
1      Long Running O… AzureLongRunni… Failed        True            localhost            New-AzVM

I want to see the failure reason, How can I get job logs using PowerShell?

PS az204_prep> New-AzVM `
>>     -ResourceGroupName 'psdemo-rg' `
>>     -Name 'psdemo-win-az4' `
>>     -Image 'Win2019Datacenter'`
>>     -Credential $WindowsCred `
>>     -OpenPorts 3389 `
>>     -AsJob


Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
1      Long Running O… AzureLongRunni… Running       True            localhost            New-AzVM

PS az204_prep> Get-Job


Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
1      Long Running O… AzureLongRunni… Failed        True            localhost            New-AzVM

PS az204_prep> (Get-Job 1).JobStateInfo.State

Failed
PS az204_prep> (Get-Job 1).JobStateInfo.Reason

PS az204_prep> 

标签: azurepowershellvirtual-machine

解决方案


To Get the Job Details, You can use Get-Job | Format-List -Property * , to get the Full details of the Job performed in Powershell.

enter image description here


The Operation is failing as you are creating a VM with Default Configuration and default command , So you don't have to use -As Job there. Please remove -As Job and it will successfully get created.

I tested it on my environment using the same command you are using (removing -As Job):

$username = 'xxxxxxx'
$password = ConvertTo-SecureString 'xxxxxxxx&Cx4cA' -AsPlainText -Force
$WindowsCred = New-Object System.Management.Automation.PSCredential ($username, $password)
New-AzVM -ResourceGroupName 'myresourcegroupname' -Name 'psdemo-win-az4' -Image 'Win2019Datacenter' -Credential $WindowsCred -OpenPorts 443 

Outputs:

enter image description here

enter image description here

Reference:

New-AzVM (Az.Compute) | Microsoft Docs


Creating with -As Job :

$VMLocalAdminUser = "LocalAdminUser"
$VMLocalAdminSecurePassword = ConvertTo-SecureString "Password" -AsPlainText -Force
$LocationName = "westus"
$ResourceGroupName = "myresourcegroup"
$ComputerName = "mycomputername"
$VMName = "MYVMName"
$VMSize = "Standard_DS3"

$NetworkName = "Myvnet"
$NICName = "MyNIC"
$SubnetName = "MySubnet"
$SubnetAddressPrefix = "10.0.0.0/24"
$VnetAddressPrefix = "10.0.0.0/16"

$SingleSubnet = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetAddressPrefix
$Vnet = New-AzVirtualNetwork -Name $NetworkName -ResourceGroupName $ResourceGroupName -Location $LocationName -AddressPrefix $VnetAddressPrefix -Subnet $SingleSubnet
$NIC = New-AzNetworkInterface -Name $NICName -ResourceGroupName $ResourceGroupName -Location $LocationName -SubnetId $Vnet.Subnets[0].Id

$Credential = New-Object System.Management.Automation.PSCredential ($VMLocalAdminUser, $VMLocalAdminSecurePassword);

$VirtualMachine = New-AzVMConfig -VMName $VMName -VMSize $VMSize
$VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $ComputerName -Credential $Credential -ProvisionVMAgent -EnableAutoUpdate
$VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id
$VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName 'MicrosoftWindowsServer' -Offer 'WindowsServer' -Skus '2012-R2-Datacenter' -Version latest

New-AzVM -ResourceGroupName $ResourceGroupName -Location $LocationName -VM $VirtualMachine -Verbose -AsJob

I failed it by not satisfying the password Criteria, So, when I check the error message I can see why it failed.

enter image description here

After I change the Password satisfying the criteria the job succeeds.

enter image description here

In Portal:

enter image description here


推荐阅读