创建VM时如何查看失败原因的日志

How to see logs for failure reason when creating VM

我正在使用以下 PowerShell 脚本创建 Azure VM

$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

当我使用 Get-Job 查看作业状态时,它正在失败。

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

我想查看失败原因,如何使用 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> 

要获取职位详情,您可以使用 Get-Job | Format-List -属性 * ,获取在 Powershell 中执行的作业的完整详细信息。


操作失败,因为您正在使用默认配置和默认命令创建 VM,因此您不必在那里使用 -As Job。请删除 -As Job 它将成功创建。

我使用您正在使用的相同命令(删除 -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 

输出:

参考:

New-AzVM (Az.Compute) | Microsoft Docs


使用 -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

我因为不满足密码条件而失败了,所以,当我检查错误消息时,我可以看到失败的原因。

在我更改满足条件的密码后,作业成功。

在门户中: