powershell - 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}}
解决方案
我只会转换需要为 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
该示例可通过此链接在线获取。
推荐阅读
- java - Initialize part of an array in java
- php - 为什么需要两个请求来传输文件?
- javascript - 用JS替换网页中的单词
- keras - keras 中用于文本分类的卷积 2d 的维度错误
- postgresql - 为每个新用户/项目创建/更改表
- sql - SQL Server 中的 LIMIT N 与 TOP N
- angular - Angular Telerik Kendo Grid不显示第一级以外的数据
- javascript - 流星无法解析某些模块
- r - 如何在 r 中将 PostgreSQL bytea 列十六进制解码为 int16/uint16?
- linux - 在 Linux 中线程化时对函数的未定义引用