You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Get-DnsClientCache经Select-Object输出仍保留字符串类型

解决Get-DnsClientCache经Select-Object后RecordType/Status变整数的问题

问题核心在于:RecordType和Status本身是枚举类型,直接运行Get-DnsClientCache时,PowerShell会自动显示枚举的友好字符串名称;但通过Select-Object提取属性时,默认会返回枚举的底层整数值,导致显示不符合预期。

不用复杂的Case或If-Else逻辑,以下两种简单方法即可保留字符串输出:

方法一:通过计算属性调用ToString()

Get-DnsClientCache | Select-Object @(
    '*'  # 保留所有原始属性
    @{Name='RecordType'; Expression={ $_.RecordType.ToString() }}
    @{Name='Status'; Expression={ $_.Status.ToString() }}
) -ExcludeProperty RecordType, Status

直接调用枚举对象的ToString()方法返回对应字符串名称,同时剔除原来的整数值属性。

方法二:强制类型转换为字符串

Get-DnsClientCache | Select-Object *, 
    @{N='RecordType'; E={ [string]$_.RecordType }},
    @{N='Status'; E={ [string]$_.Status }} -ExcludeProperty RecordType, Status

PowerShell对枚举类型的字符串转换会自动解析为友好名称,写法比第一种更简洁。

只提取特定属性的示例

如果不需要保留所有属性,把*换成目标属性列表即可:

Get-DnsClientCache | Select-Object Name, 
    @{N='RecordType'; E={ [string]$_.RecordType }},
    @{N='Status'; E={ [string]$_.Status }},
    Section, TimeToLive

内容的提问来源于stack exchange,提问作者Christian McGhee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 09:12:08