You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the Get-GceMetadata cmdlet it doesn't seem to write errors to the error stream or throw exceptions when the metadata server returns an error such as 404.
Currently there doesn't seem to be a reasonable way to detect that there was an error.
Test 1 should output a blank result and the console should show the error text (normally in red).
Test 2 shouldn't show any output as script should halt
Actual result
The error response is stored in $Result. When writing the result to the console it dumps a large block of HTML that contains a description of the error (e.g. 404).
Many of the properties you need from the metadata are available directly from the instance object model. However, obtaining a specific metadata attribute by name can be a awkward.
functionGet-MetadataAttribute {
[CmdletBinding()]
param (
[Parameter($Mandatory=$true)] [string] $Name
)
$Instance=Get-GceInstance$Item= ($Instance.Metadata.Items|Where-Object Key -eq$Name)
if ($Item-eq$null) {
Write-Error"Metadata does not contain an attribute named '${Name}'"
}
return$Item.Value
}
The text was updated successfully, but these errors were encountered:
When using the
Get-GceMetadata
cmdlet it doesn't seem to write errors to the error stream or throw exceptions when the metadata server returns an error such as 404.Currently there doesn't seem to be a reasonable way to detect that there was an error.
Steps to reproduce
Expected result
Test 1 should output a blank result and the console should show the error text (normally in red).
Test 2 shouldn't show any output as script should halt
Actual result
The error response is stored in
$Result
. When writing the result to the console it dumps a large block of HTML that contains a description of the error (e.g. 404).Workarounds
Invoke-RestMethod
Get-GceInstance
Many of the properties you need from the metadata are available directly from the instance object model. However, obtaining a specific metadata attribute by name can be a awkward.
The text was updated successfully, but these errors were encountered: