你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
默认情况下,Azure PowerShell cmdlet 格式化其输出,以便易于阅读。 但是,可以通过将 cmdlet 输出管道传递给以下 cmdlet 之一来修改或自定义输出格式:
格式化 | 转换 |
---|---|
Format-Custom | ConvertTo-Csv |
格式列表 | ConvertTo-Html |
Format-Table | ConvertTo-Json |
Format-Wide | ConvertTo-Xml |
格式设置在 PowerShell 控制台中显示信息,而转换会生成其他脚本或程序的数据。
表输出格式
默认情况下,Azure PowerShell cmdlet 以表格式输出。 此格式不显示所请求资源的所有信息:
Get-AzVM
ResourceGroupName Name Location VmSize OsType NIC ProvisioningState Zone
----------------- ---- -------- ------ ------ --- ----------------- ----
QueryExample ExampleLinuxVM westus2 Basic_A0 Linux examplelinuxvm916 Succeeded
QueryExample RHELExample westus2 Standard_D2_v3 Linux rhelexample469 Succeeded
QueryExample WinExampleVM westus2 Standard_DS1_v2 Windows winexamplevm268 Succeeded
通过 Format-Table
显示的数据量可能受 PowerShell 会话窗口的宽度影响。 若要将输出限制为特定属性并对其进行排序,可以将属性名称作为参数 Format-Table
提供给:
Get-AzVM -ResourceGroupName QueryExample |
Format-Table -Property Name, ResourceGroupName, Location
Name ResourceGroupName Location
---- ----------------- --------
ExampleLinuxVM QueryExample westus2
RHELExample QueryExample westus2
WinExampleVM QueryExample westus2
列出输出格式
列表输出格式生成两个列,首先是属性名称,然后是值。 对于复杂对象,则会改为显示对象的类型。
Get-AzVM | Format-List
以下输出删除了某些字段。
ResourceGroupName : QueryExample
Id : /subscriptions/.../resourceGroups/QueryExample/providers/Microsoft.Compute/virtualMachines/ExampleLinuxVM
VmId : ...
Name : ExampleLinuxVM
Type : Microsoft.Compute/virtualMachines
Location : westus2
...
HardwareProfile : Microsoft.Azure.Management.Compute.Models.HardwareProfile
InstanceView :
NetworkProfile : Microsoft.Azure.Management.Compute.Models.NetworkProfile
OSProfile : Microsoft.Azure.Management.Compute.Models.OSProfile
...
StatusCode : OK
ResourceGroupName : QueryExample
Id : /subscriptions/.../resourceGroups/QueryExample/providers/Microsoft.Compute/virtualMachines/RHELExample
VmId : ...
Name : RHELExample
Type : Microsoft.Compute/virtualMachines
Location : westus2
...
与 Format-Table
一样,可以提供属性名称,对输出进行排序和限制:
Get-AzVM | Format-List -Property ResourceGroupName, Name, Location
ResourceGroupName : QueryExample
Name : ExampleLinuxVM
Location : westus2
ResourceGroupName : QueryExample
Name : RHELExample
Location : westus2
ResourceGroupName : QueryExample
Name : WinExampleVM
Location : westus2
宽输出格式
宽输出模式仅为每个查询生成一个属性名称。 可以通过将属性作为参数来控制显示哪个属性。
Get-AzVM | Format-Wide
ExampleLinuxVM RHELExample
WinExampleVM
Get-AzVM | Format-Wide -Property ResourceGroupName
QueryExample QueryExample
QueryExample
自定义输出格式
Custom-Format
输出类型旨在格式化自定义对象。 没有任何参数,它的行为类似于 Format-List
,但显示自定义类的属性名称。
Get-AzVM | Format-Custom
以下输出删除了某些字段。
ResourceGroupName : QueryExample
Id : /subscriptions/.../resourceGroups/QueryExample/providers/Microsoft.Compute/virtualMachines/ExampleLinuxVM
VmId : ...
Name : ExampleLinuxVM
Type : Microsoft.Compute/virtualMachines
Location : westus2
Tags : {}
HardwareProfile : {VmSize}
NetworkProfile : {NetworkInterfaces}
OSProfile : {ComputerName, AdminUsername, LinuxConfiguration, Secrets,
AllowExtensionOperations}
ProvisioningState : Succeeded
StorageProfile : {ImageReference, OsDisk, DataDisks}
...
以 Custom-Format
的参数的形式提供属性名称时,会显示已设置为以下值的自定义对象的属性/值对:
Get-AzVM | Format-Custom -Property Name, ResourceGroupName, Location, OSProfile
以下输出删除了某些字段。
class PSVirtualMachineList
{
Name = ExampleLinuxVM
ResourceGroupName = QueryExample
Location = westus2
OSProfile =
class OSProfile
{
ComputerName = ExampleLinuxVM
AdminUsername = ...
AdminPassword =
CustomData =
WindowsConfiguration =
LinuxConfiguration =
class LinuxConfiguration
{
DisablePasswordAuthentication = False
Ssh =
ProvisionVMAgent = True
}
Secrets =
[
]
AllowExtensionOperations = True
}
}
...
class PSVirtualMachineList
{
Name = WinExampleVM
ResourceGroupName = QueryExample
Location = westus2
OSProfile =
class OSProfile
{
ComputerName = WinExampleVM
AdminUsername = ...
AdminPassword =
CustomData =
WindowsConfiguration =
class WindowsConfiguration
{
ProvisionVMAgent = True
EnableAutomaticUpdates = True
TimeZone =
AdditionalUnattendContent =
WinRM =
}
LinuxConfiguration =
Secrets =
[
]
AllowExtensionOperations = True
}
}
转换为其他数据格式
可以使用 ConvertTo-*
系列的 cmdlet 将 Azure PowerShell cmdlet 的结果转换为可供计算机读取的格式。 若要仅从 Azure PowerShell 结果获取某些属性,请在执行转换之前通过管道连接到 Select-Object
cmdlet。 以下示例演示每个转换生成的不同类型的输出。
转换为 CSV
Get-AzVM | ConvertTo-CSV
#TYPE Microsoft.Azure.Commands.Compute.Models.PSVirtualMachineList
"ResourceGroupName","Id","VmId","Name","Type","Location","LicenseType","Tags","AvailabilitySetReference","DiagnosticsProfile","Extensions","HardwareProfile","InstanceView","NetworkProfile","OSProfile","Plan","ProvisioningState","StorageProfile","DisplayHint","Identity","Zones","FullyQualifiedDomainName","AdditionalCapabilities","RequestId","StatusCode"
"QUERYEXAMPLE","/subscriptions/.../resourceGroups/QUERYEXAMPLE/providers/Microsoft.Compute/virtualMachines/ExampleLinuxVM","...","ExampleLinuxVM","Microsoft.Compute/virtualMachines","westus2",,"System.Collections.Generic.Dictionary`2[System.String,System.String]",,,"System.Collections.Generic.List`1[Microsoft.Azure.Management.Compute.Models.VirtualMachineExtension]","Microsoft.Azure.Management.Compute.Models.HardwareProfile",,"Microsoft.Azure.Management.Compute.Models.NetworkProfile","Microsoft.Azure.Management.Compute.Models.OSProfile",,"Succeeded","Microsoft.Azure.Management.Compute.Models.StorageProfile","Compact",,"System.Collections.Generic.List`1[System.String]",,,"...","OK"
"QUERYEXAMPLE","/subscriptions/.../resourceGroups/QUERYEXAMPLE/providers/Microsoft.Compute/virtualMachines/RHELExample","...","RHELExample","Microsoft.Compute/virtualMachines","westus2",,"System.Collections.Generic.Dictionary`2[System.String,System.String]",,,"System.Collections.Generic.List`1[Microsoft.Azure.Management.Compute.Models.VirtualMachineExtension]","Microsoft.Azure.Management.Compute.Models.HardwareProfile",,"Microsoft.Azure.Management.Compute.Models.NetworkProfile","Microsoft.Azure.Management.Compute.Models.OSProfile",,"Succeeded","Microsoft.Azure.Management.Compute.Models.StorageProfile","Compact",,"System.Collections.Generic.List`1[System.String]",,,"...","OK"
"QUERYEXAMPLE","/subscriptions/.../resourceGroups/QUERYEXAMPLE/providers/Microsoft.Compute/virtualMachines/WinExampleVM","...","WinExampleVM","Microsoft.Compute/virtualMachines","westus2",,"System.Collections.Generic.Dictionary`2[System.String,System.String]",,,"System.Collections.Generic.List`1[Microsoft.Azure.Management.Compute.Models.VirtualMachineExtension]","Microsoft.Azure.Management.Compute.Models.HardwareProfile",,"Microsoft.Azure.Management.Compute.Models.NetworkProfile","Microsoft.Azure.Management.Compute.Models.OSProfile",,"Succeeded","Microsoft.Azure.Management.Compute.Models.StorageProfile","Compact",,"System.Collections.Generic.List`1[System.String]",,,"...","OK"
转换为 JSON
默认情况下,JSON 输出不会展开所有属性。 若要更改扩展的属性深度,请使用 Depth
参数。 默认情况下,扩展深度为 2
。
Get-AzVM | ConvertTo-JSON
以下输出删除了某些字段。
[
{
"ResourceGroupName": "QUERYEXAMPLE",
"Id": "/subscriptions/.../resourceGroups/QUERYEXAMPLE/providers/Microsoft.Compute/virtualMachines/ExampleLinuxVM",
"VmId": "...",
"Name": "ExampleLinuxVM",
"Type": "Microsoft.Compute/virtualMachines",
"Location": "westus2",
...
"OSProfile": {
"ComputerName": "ExampleLinuxVM",
"AdminUsername": "...",
"AdminPassword": null,
"CustomData": null,
"WindowsConfiguration": null,
"LinuxConfiguration": "Microsoft.Azure.Management.Compute.Models.LinuxConfiguration",
"Secrets": "",
"AllowExtensionOperations": true
},
"Plan": null,
"ProvisioningState": "Succeeded",
"StorageProfile": {
"ImageReference": "Microsoft.Azure.Management.Compute.Models.ImageReference",
"OsDisk": "Microsoft.Azure.Management.Compute.Models.OSDisk",
"DataDisks": ""
},
"DisplayHint": 0,
"Identity": null,
"Zones": [
],
"FullyQualifiedDomainName": null,
"AdditionalCapabilities": null,
"RequestId": "...",
"StatusCode": 200
},
...
]
转换为 XML
该 ConvertTo-XML
cmdlet 将 Azure PowerShell 响应对象转换为纯 XML 对象,该对象可以像 PowerShell 中的其他任何 XML 对象一样进行处理。
Get-AzVM | ConvertTo-XML
xml Objects
--- -------
version="1.0" encoding="utf-8" Objects
转换为 HTML
将对象转换为 HTML 生成以 HTML 表形式呈现的输出。 HTML 的呈现效果取决于浏览器在呈现不含宽度信息的表时的行为。 不展开任何自定义类对象。
Get-AzVM | ConvertTo-HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/></colgroup>
<tr><th>ResourceGroupName</th><th>Id</th><th>VmId</th><th>Name</th><th>Type</th><th>Location</th><th>LicenseType</th><th>Tags</th><th>AvailabilitySetReference</th><th>DiagnosticsProfile</th><th>Extensions</th><th>HardwareProfile</th><th>InstanceView</th><th>NetworkProfile</th><th>OSProfile</th><th>Plan</th><th>ProvisioningState</th><th>StorageProfile</th><th>DisplayHint</th><th>Identity</th><th>Zones</th><th>FullyQualifiedDomainName</th><th>AdditionalCapabilities</th><th>RequestId</th><th>StatusCode</th></tr>
<tr><td>QUERYEXAMPLE</td><td>/subscriptions/.../resourceGroups/QUERYEXAMPLE/providers/Microsoft.Compute/virtualMachines/ExampleLinuxVM</td><td>...</td><td>ExampleLinuxVM</td><td>Microsoft.Compute/virtualMachines</td><td>westus2</td><td></td><td>System.Collections.Generic.Dictionary`2[System.String,System.String]</td><td></td><td></td><td>System.Collections.Generic.List`1[Microsoft.Azure.Management.Compute.Models.VirtualMachineExtension]</td><td>Microsoft.Azure.Management.Compute.Models.HardwareProfile</td><td></td><td>Microsoft.Azure.Management.Compute.Models.NetworkProfile</td><td>Microsoft.Azure.Management.Compute.Models.OSProfile</td><td></td><td>Succeeded</td><td>Microsoft.Azure.Management.Compute.Models.StorageProfile</td><td>Compact</td><td></td><td>System.Collections.Generic.List`1[System.String]</td><td></td><td></td><td>...</td><td>OK</td></tr>
<tr><td>QUERYEXAMPLE</td><td>/subscriptions/.../resourceGroups/QUERYEXAMPLE/providers/Microsoft.Compute/virtualMachines/RHELExample</td><td>...</td><td>RHELExample</td><td>Microsoft.Compute/virtualMachines</td><td>westus2</td><td></td><td>System.Collections.Generic.Dictionary`2[System.String,System.String]</td><td></td><td></td><td>System.Collections.Generic.List`1[Microsoft.Azure.Management.Compute.Models.VirtualMachineExtension]</td><td>Microsoft.Azure.Management.Compute.Models.HardwareProfile</td><td></td><td>Microsoft.Azure.Management.Compute.Models.NetworkProfile</td><td>Microsoft.Azure.Management.Compute.Models.OSProfile</td><td></td><td>Succeeded</td><td>Microsoft.Azure.Management.Compute.Models.StorageProfile</td><td>Compact</td><td></td><td>System.Collections.Generic.List`1[System.String]</td><td></td><td></td><td>...</td><td>OK</td></tr>
<tr><td>QUERYEXAMPLE</td><td>/subscriptions/.../resourceGroups/QUERYEXAMPLE/providers/Microsoft.Compute/virtualMachines/WinExampleVM</td><td>...</td><td>WinExampleVM</td><td>Microsoft.Compute/virtualMachines</td><td>westus2</td><td></td><td>System.Collections.Generic.Dictionary`2[System.String,System.String]</td><td></td><td></td><td>System.Collections.Generic.List`1[Microsoft.Azure.Management.Compute.Models.VirtualMachineExtension]</td><td>Microsoft.Azure.Management.Compute.Models.HardwareProfile</td><td></td><td>Microsoft.Azure.Management.Compute.Models.NetworkProfile</td><td>Microsoft.Azure.Management.Compute.Models.OSProfile</td><td></td><td>Succeeded</td><td>Microsoft.Azure.Management.Compute.Models.StorageProfile</td><td>Compact</td><td></td><td>System.Collections.Generic.List`1[System.String]</td><td></td><td></td><td>...</td><td>OK</td></tr>
</table>
</body></html>