Powershell - 使用ConvertTo-Json保留所有枚举属性的文本 [英] Powershell - Retain the text of all Enum properties with ConvertTo-Json

查看:450
本文介绍了Powershell - 使用ConvertTo-Json保留所有枚举属性的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 Get-Msoldomain powershell命令 - 让我得到以下输出(让它调用它输出#1 )其中名称,状态和身份验证是属性名称以下是各自的值。

For "Get-Msoldomain" powershell command-let I get the below output (lets call it Output#1) where Name, Status and Authentication are the property names and below are their respective values.

Name                    Status   Authentication

myemail.onmicrosoft.com Verified Managed

当我使用下面的命令ConvertTo-Json

When I use the command with "ConvertTo-Json" like below

GetMsolDomain |ConvertTo-Json

I得到以下输出(让它调用输出#2 )在Json格式。

I get the below output (lets call it Output#2) in Json Format.

{
    "ExtensionData":  {

                      },
    "Authentication":  0,
    "Capabilities":  5,
    "IsDefault":  true,
    "IsInitial":  true,
    "Name":  "myemail.onmicrosoft.com",
    "RootDomain":  null,
    "Status":  1,
    "VerificationMethod":  1
}

然而,问题是如果您在两个输出中注意到状态属性,则不同。对于 VerificationMethod 属性也是一样。不使用ConvertTo-JSon Powershell给出文本,并使用ConvertTo-Json给出整数。

However, the problem is, that if you notice the Status property in both the outputs, it's different. Same happens for VerificationMethod property. Without using the ConvertTo-JSon Powershell gives the Text, and with using ConvertTo-Json it gives the integer.

当我给出以下命令

get-msoldomain |Select-object @{Name='Status';Expression={"$($_.Status)"}}|ConvertTo-json

我的输出为

{
    "Status":  "Verified"
}

但是,我想要一些东西,所以我不需要指定任何特定的属性名称进行转换,我上面指定的方式为

However, I want something so that I don't have to specify any specific property name for it to be converted , the way I am specifying above as

Select-object @{Name='Status';Expression={"$($_.Status)"}}

此行仅转换状态属性而不是 VerificationMethod 属性,因为这是我提供的输入。

This line is transforming only the Status Property and not the VerificationMethod property because that is what I am providing as input .

问题:是否有一些通用的,可以给 ConvertTo-Json 命令,以便它返回 ALL 枚举属性为文本而不是整数,没有明确命名它们,所以我得到如下所示的输出:

Question: Is there something generic that I can give to the "ConvertTo-Json" commandlet, so that It returns ALL the Enum properties as Texts and not Integers, without explicitly naming them, so that I get something like below as the output:

{
    "ExtensionData":  {

                      },
    "Authentication":  0,
    "Capabilities":  5,
    "IsDefault":  true,
    "IsInitial":  true,
    "Name":  "myemail.onmicrosoft.com",
    "RootDomain":  null,
    "Status":  "Verified",
    "VerificationMethod":  "DnsRecord"
}


推荐答案

嗯,如果你不介意一点点旅行:)你可以将它转换成CSV,这将强制字符串输出,然后重新转换为CSV,最后返回到Json,

Well, if you don't mind to take a little trip :) you can convert it to CSV which will force the string output, then re-convert it to CSV and finally back to Json,

像这样:

Get-MsolDomain | ConvertTo-Csv | ConvertFrom-Csv | ConvertTo-Json

这篇关于Powershell - 使用ConvertTo-Json保留所有枚举属性的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆