对于“Get-Msoldomain” powershell命令,我得到以下输出(将其命名为Output#1),其中Name,Status和Authentication是属性名称,下面是它们各自的值。
Name Status Authentication
myemail.onmicrosoft.com Verified Managed
当我将命令与“ConvertTo-Json”一起使用时,如下所示
GetMsolDomain |ConvertTo-Json
我以Json格式获取以下输出(将其称为Output#2)。
{
"ExtensionData": {
},
"Authentication": 0,
"Capabilities": 5,
"IsDefault": true,
"IsInitial": true,
"Name": "myemail.onmicrosoft.com",
"RootDomain": null,
"Status": 1,
"VerificationMethod": 1
}
但是,问题是,如果您在两个输出中都注意到 Status 属性,则该属性将有所不同。 VerificationMethod 属性也是如此。在不使用ConvertTo-JSon的情况下,Powershell给出文本,而在使用ConvertTo-Json的情况下,给出整数。
当我给出以下命令时
get-msoldomain |Select-object @{Name='Status';Expression={"$($_.Status)"}}|ConvertTo-json
我得到的输出为
{
"Status": "Verified"
}
但是,我想要一些东西,这样我就不必为要转换的属性指定任何特定的属性名称,就像我在上面指定的那样
Select-object @{Name='Status';Expression={"$($_.Status)"}}
该行仅转换状态属性,而不转换 VerificationMethod 属性,因为这是我提供的输入。
问题:我可以为“ ConvertTo-Json ”命令集提供一些通用名称,以便它在不显式命名它们的情况下,将Enum属性作为文本而不是整数返回 ALL ,这样我将得到如下所示的内容输出:
{
"ExtensionData": {
},
"Authentication": 0,
"Capabilities": 5,
"IsDefault": true,
"IsInitial": true,
"Name": "myemail.onmicrosoft.com",
"RootDomain": null,
"Status": "Verified",
"VerificationMethod": "DnsRecord"
}
最佳答案
好吧,如果您不介意旅行,可以将其转换为CSV,这将强制输出字符串,然后将其从CSV重新转换回PS Object,最后再转换回Json。
像这样:
Get-MsolDomain | ConvertTo-Csv | ConvertFrom-Csv | ConvertTo-Json
关于json - Powershell-使用ConvertTo-Json保留所有Enum属性的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54022396/