首页 > 解决方案 > PoweShell:为 Select-Object 中的属性转换数据类型

问题描述

我正在寻找一种在 PowerShell 期间转换数据类型的更有效的方法(对于键入时间和性能)Select-Object

目前,我将每个单独的属性包装在一个表达式中以转换数据类型。我相信这不是正确的方法,它只是感觉很脏......

我这样做的原因是我将数据发送到 REST API,该 API 使用 JSON 模式应用严格验证。里面的数据$Data不可靠。例如,属性有时是 JSON 字符串"12345",有时是意外的 JSON Integer 12345

REST API 然后返回 403 错误,因为它不期望该密钥的整数。

$Results = $Data | select ` 
    @{Name = 'Name'; expression = {[string]$_.DisplayName}}, 
    @{Name = 'Version'; expression = {[string]$_.DisplayVersion}},  
    @{Name = 'HelpLink'; expression = {[string]$_.HelpLink}}, 
    @{Name = 'InstallLocation'; expression = {[string]$_.InstallLocation}}, 
    @{Name = 'InstallSource'; expression = {[string]$_.InstallSource}}, 
    @{Name = 'Language'; expression = {[int]$_.Language}},  
    @{Name = 'DisplayIcon'; expression = {[string]$_.DisplayIcon}}, 
    @{Name = 'UninstallString'; expression = {[string]$_.UninstallString}}, 
    @{Name = 'WindowsInstaller'; expression = {[int]$_.WindowsInstaller}},
    @{Name = 'AppGUID'; expression = {[string]$_.APP_GUID}},  
    @{Name = 'URLInfoAbout'; expression = {[string]$_.URLInfoAbout}}, 
    @{Name = 'Vendor'; expression = {[string]$_.Publisher}}, 
    @{Name = 'InstallDate'; expression = {[int]$_.InstallDate}},
    @{Name = 'EstimatedSize'; expression = {[int]$_.EstimatedSize}},
    @{Name = 'VersionMajor'; expression = {[string]$_.VersionMajor}},
    @{Name = 'VersionMinor'; expression = {[string]$_.VersionMinor}},
    @{Name = 'SystemComponent'; expression = {[int]$_.SystemComponent}},
    @{Name = 'NoModify'; expression = {[string]$_.NoModify}},
    @{Name = 'NoRepair'; expression = {[string]$_.NoRepair}},
    @{Name = 'ModifyPath'; expression = {[string]$_.ModifyPath}},
    @{Name = 'BundleVersion'; expression = {[string]$_.BundleVersion}},
    @{Name = 'EngineVersion'; expression = {[string]$_.EngineVersion}}

标签: powershellcastingselect-object

解决方案


我只会转换需要为 type 的属性int。由于 PowerShell 是一种基于动态类型的语言,因此您可以执行以下操作:

$obj = [PSCustomObject] @{ Number = "123" }
$obj.Number.GetType() # Type is string
$obj.Number = [int] $obj.Number
$obj.Number.GetType() # Type is int

Output:
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object
True     True     Int32                                    System.ValueType

您可以在线找到此示例。因此,您应该能够使用这种方法:

$Data.Language =  [int] $Data.Language

简而言之,您已经转换了需要为 type 的属性int

更新 1

如果您的对象具有“扁平”层次结构,您可以尝试以下操作:

$obj = [PSCustomObject]@{
    IntNr = "123"
    DecNr = "4,56"
    Str   = "abc"
}

$result = $obj.PSObject.Properties | ForEach-Object {
    [int] $parsedInt = 0
    [decimal] $parsedDec = 0.0
    if ([int]::TryParse($_.Value, [ref]$parsedInt)) {
        $_.Value = $parsedInt
    }
    elseif ([decimal]::TryParse($_.Value, [ref]$parsedDec)) {
        $_.Value = $parsedDec
    }
    $_
}

$result

倾倒时的输出$result

 Value           : 123
 MemberType      : NoteProperty
 IsSettable      : True
 IsGettable      : True
 TypeNameOfValue : System.Int32
 Name            : IntNr
 IsInstance      : True

 Value           : 456
 MemberType      : NoteProperty
 IsSettable      : True
 IsGettable      : True
 TypeNameOfValue : System.Decimal
 Name            : DecNr
 IsInstance      : True

 Value           : abc
 MemberType      : NoteProperty
 IsSettable      : True
 IsGettable      : True
 TypeNameOfValue : System.String
 Name            : Str
 IsInstance      : True

该示例可通过此链接在线获取。


推荐阅读