我正在寻找一种在 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}}