Skip to content

Commit 7ca83a6

Browse files
authored
Get-AzVMBootDiagnostics api change to use RetrieveBootDiagnosticsData() (#13233)
* initial dev * intermediate work * get-azvmbootdiagnosticsdata dev, testing (maybe) , and changelog. No help doc update needed. * test if removing file path and cmd in test removes CI failures * updating to 12.7.0 updating Azure.Storage.Blobs to 12.7.0 from 12.6.0 to address CI failure. * adding test with local file path back in * remove get cmd from test * back to 12.6.0 * 12.7.0-preview.1 * Update Compute.csproj * attempt 12.5.0 * 12.7.0-preview * 12.7.0-preview.1 * added back in local file path test to see if any effect on the CI checks. * commented out boot commented out the cmd utilizing the local file path to my personal computer. Seems to be causing a test error in Test windows > Test. Removing to test it. * to parity * adding required assemblies * removed last test cmd recorded * Update Az.Compute.psd1 * try 12.6.0 with required assemblies. * 12.7.0-preview.1 * simplified to CloudBlob * removed unnecessary assmblies * test updates, LivOnly. * fix linux * windows serial log
1 parent 211fadd commit 7ca83a6

File tree

5 files changed

+4348
-26
lines changed

5 files changed

+4348
-26
lines changed

src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,5 +346,12 @@ public void TestVirtualMachineImageListTopOrderExpand()
346346
{
347347
TestRunner.RunTestScript("Test-VirtualMachineImageListTopOrderExpand");
348348
}
349+
350+
[Fact]
351+
[Trait(Category.AcceptanceType, Category.LiveOnly)]
352+
public void TestVirtualMachineBootDiagnostics()
353+
{
354+
TestRunner.RunTestScript("Test-VirtualMachineBootDiagnostics");
355+
}
349356
}
350357
}

src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4412,3 +4412,69 @@ function Test-VirtualMachineImageListTopOrderExpand
44124412

44134413
}
44144414

4415+
<#
4416+
.SYNOPSIS
4417+
This test can only run in Record mode. SEveral lines need to be uncommented for it to test the cmdlet.
4418+
Downloads the managed boot diagnostics of a Windows machine to a local file path.
4419+
#>
4420+
function Test-VirtualMachineBootDiagnostics
4421+
{
4422+
# Setup
4423+
$rgname = Get-ComputeTestResourceName;
4424+
$loc = Get-ComputeVMLocation;
4425+
4426+
try
4427+
{
4428+
New-AzResourceGroup -Name $rgname -Location $loc -Force;
4429+
4430+
# VM Profile & Hardware
4431+
$vmsize = 'Standard_DS1_v2';
4432+
$vmname = 'vm' + $rgname;
4433+
# Create the file path on your machine, then set this variable to it.
4434+
# $localPath = "C:\Users\adsandor\Documents\bootDiags"
4435+
4436+
$p = New-AzVMConfig -VMName $vmname -VMSize $vmsize;
4437+
4438+
# NRP
4439+
$subnet = New-AzVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24";
4440+
$vnet = New-AzVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -Subnet $subnet;
4441+
$vnet = Get-AzVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname;
4442+
$subnetId = $vnet.Subnets[0].Id;
4443+
#1
4444+
$pubip = New-AzPublicIpAddress -Force -Name ('pubip' + $rgname) -ResourceGroupName $rgname -Location $loc -AllocationMethod Dynamic -DomainNameLabel ('pubip' + $rgname);
4445+
$pubip = Get-AzPublicIpAddress -Name ('pubip' + $rgname) -ResourceGroupName $rgname;
4446+
$pubipId = $pubip.Id;
4447+
$nic = New-AzNetworkInterface -Force -Name ('nic' + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId -PublicIpAddressId $pubip.Id;
4448+
$nic = Get-AzNetworkInterface -Name ('nic' + $rgname) -ResourceGroupName $rgname;
4449+
$nicId = $nic.Id;
4450+
4451+
$p = Add-AzVMNetworkInterface -VM $p -Id $nicId;
4452+
4453+
# OS & Image
4454+
$user = "Foo2";
4455+
$password = $PLACEHOLDER;
4456+
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force;
4457+
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
4458+
$computerName = 'test';
4459+
4460+
# Windows OS test case.
4461+
$p = Set-AzVMOperatingSystem -VM $p -Windows -ComputerName $computerName -Credential $cred;
4462+
4463+
$imgRef = Get-DefaultCRPImage -loc $loc;
4464+
$p = ($imgRef | Set-AzVMSourceImage -VM $p);
4465+
4466+
# Virtual Machine
4467+
$vm = New-AzVM -ResourceGroupName $rgname -Location $loc -VM $p;
4468+
Assert-NotNull $vm;
4469+
4470+
# Get Managed Boot Diagnostics
4471+
# uncomment this when running locally.
4472+
# Get-AzVmBootDiagnosticsData -ResourceGroupName $rgname -Name $vmname -Windows -LocalPath $localPath;
4473+
}
4474+
finally
4475+
{
4476+
# Cleanup
4477+
Clean-ResourceGroup $rgname
4478+
}
4479+
}
4480+

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachineTests/TestVirtualMachineBootDiagnostics.json

Lines changed: 4258 additions & 0 deletions
Large diffs are not rendered by default.

src/Compute/Compute/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* New cmdlet `Get-AzDiskEncryptionSetAssociatedResource`
2828
* Added `Tier` and `LogicalSectorSize` optional parameters to the New-AzDiskConfig cmdlet.
2929
* Added `Tier`, `MaxSharesCount`, `DiskIOPSReadOnly`, and `DiskMBpsReadOnly` optional parameters to the `New-AzDiskUpdateConfig` cmdlet.
30+
* Modified `Get-AzVmBootDiagnostics` cmdlet to use the new RetrieveBootDiagnosticsData API instead of directly accessing the BootDiagnostics properties on the virtual machine.
3031

3132
## Version 4.5.0
3233
* Fixed issue in `Update-ASRRecoveryPlan` by populating FailoverTypes

src/Compute/Compute/VirtualMachine/Operation/GetAzureVMBootDiagnosticsDataCommand.cs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
using Microsoft.Azure.Commands.Compute.Common;
2323
using Microsoft.Azure.Commands.Compute.Models;
2424
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
25+
using Microsoft.Azure.Management.Compute;
2526
using Microsoft.Azure.Management.Storage.Version2017_10_01;
2627
using Microsoft.WindowsAzure.Commands.Sync.Download;
2728
using Microsoft.WindowsAzure.Storage.Auth;
2829
using Microsoft.WindowsAzure.Storage.Blob;
2930

31+
3032
namespace Microsoft.Azure.Commands.Compute
3133
{
3234
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "VMBootDiagnosticsData", DefaultParameterSetName = WindowsParamSet)]
@@ -107,7 +109,7 @@ public override void ExecuteCmdlet()
107109
|| result.Body.DiagnosticsProfile.BootDiagnostics == null
108110
|| result.Body.DiagnosticsProfile.BootDiagnostics.Enabled == null
109111
|| !result.Body.DiagnosticsProfile.BootDiagnostics.Enabled.Value
110-
|| result.Body.DiagnosticsProfile.BootDiagnostics.StorageUri == null)
112+
)
111113
{
112114
ThrowTerminatingError
113115
(new ErrorRecord(
@@ -130,25 +132,26 @@ public override void ExecuteCmdlet()
130132
null));
131133
}
132134

135+
//console log
133136
if (this.Windows.IsPresent
134137
|| (this.Linux.IsPresent && !string.IsNullOrEmpty(this.LocalPath)))
135138
{
136-
var screenshotUri = new Uri(result.Body.InstanceView.BootDiagnostics.ConsoleScreenshotBlobUri);
137-
var localFile = this.LocalPath + screenshotUri.Segments[2];
138-
DownloadFromBlobUri(screenshotUri, localFile);
139-
}
139+
var bootDiagnostics = this.VirtualMachineClient.RetrieveBootDiagnosticsData(this.ResourceGroupName, this.Name);
140+
var localPathTest = this.LocalPath;
141+
var localFile = this.LocalPath + new Uri(bootDiagnostics.ConsoleScreenshotBlobUri).Segments[2];
140142

143+
DownloadFromBlobUri(new Uri(bootDiagnostics.ConsoleScreenshotBlobUri), localFile);
144+
}
141145

146+
//serial log
147+
var bootDiagnosticsSerial = this.VirtualMachineClient.RetrieveBootDiagnosticsData(this.ResourceGroupName, this.Name);
148+
var logUri = new Uri(bootDiagnosticsSerial.SerialConsoleLogBlobUri);
149+
var localFileSerial = (this.LocalPath ?? Path.GetTempPath()) + logUri.Segments[2];
150+
DownloadFromBlobUri(logUri, localFileSerial);
142151
if (this.Linux.IsPresent)
143152
{
144-
var logUri = new Uri(result.Body.InstanceView.BootDiagnostics.SerialConsoleLogBlobUri);
145-
146-
var localFile = (this.LocalPath ?? Path.GetTempPath()) + logUri.Segments[2];
147-
148-
DownloadFromBlobUri(logUri, localFile);
149-
150153
var sb = new StringBuilder();
151-
using (var reader = new StreamReader(localFile))
154+
using (var reader = new StreamReader(localFileSerial))
152155
{
153156
string line;
154157
while ((line = reader.ReadLine()) != null)
@@ -165,25 +168,12 @@ public override void ExecuteCmdlet()
165168
private void DownloadFromBlobUri(Uri sourceUri, string localFileInfo)
166169
{
167170
BlobUri blobUri;
168-
string storagekey = "";
169171
if (!BlobUri.TryParseUri(sourceUri, out blobUri))
170172
{
171173
throw new ArgumentOutOfRangeException("Source", sourceUri.ToString());
172174
}
173175

174-
var storageClient = AzureSession.Instance.ClientFactory.CreateArmClient<StorageManagementClient>(
175-
DefaultProfile.DefaultContext, AzureEnvironment.Endpoint.ResourceManager);
176-
177-
178-
var storageService = storageClient.StorageAccounts.GetProperties(this.ResourceGroupName, blobUri.StorageAccountName);
179-
if (storageService != null)
180-
{
181-
var storageKeys = storageClient.StorageAccounts.ListKeys(this.ResourceGroupName, storageService.Name);
182-
storagekey = storageKeys.GetKey1();
183-
}
184-
185-
StorageCredentials storagecred = new StorageCredentials(blobUri.StorageAccountName, storagekey);
186-
var blob = new CloudBlob(sourceUri, storagecred);
176+
var blob = new CloudBlob(sourceUri);
187177

188178
blob.DownloadToFileAsync(localFileInfo, FileMode.Create).ConfigureAwait(false).GetAwaiter().GetResult();
189179
}

0 commit comments

Comments
 (0)