I'm looking for a more efficient method (for both typing time and performance) of casting data types during a PowerShell Select-Object
.
Currently, I am wrapping each individual property in an expression to cast the data type. I'm confident this isn't the correct way to do this, it just feels dirty...
The reason I am doing this is that I'm sending the data to a REST API which is applying strict validation using a JSON schema. The data in $Data
is unreliable. For example, a property is sometimes a JSON string "12345"
and occasionally an unexpected JSON Integer 12345
.
The REST API then returns a 403 error because it was not expecting an Integer for that key.
$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}}