I see a lot of questions and answer regarding converting a byte array into string however my case is quite different and I cannot for the life of me find a way to do what I need.
I generate a result set from a certain API and pull it into a variable $jobandtasklist
I then choose the properties I want and export that into a csv file
$jobandtasklist | select Group, Name, Active, Taskname, ExitCodeResult, DatelastExecution, Description, TaskStdOutput, TaskErrOutput | Export-Csv c:\temp\ErrorReport.csv
I then import said csv file
$csvresults = Import-Csv -Path 'C:\temp\ErrorReport.csv'
The TaskStdOutput and TaskErrOutput have values that look like the below
69 120 99 101 112 116 105 111 110 32 105 110 32 84 97 115 107 58 32 78 111 32 102 105 108 101 40 115 41 32 102 111 117 110 100
I want these byte values converted to their string equivalent (I would like the output to be legible to human eyes basically)
I try the below
foreach ($result in $csvresults) {
If ($result.TaskStdOutput -gt 1 ) {
$result.TaskstdOutput = [System.Text.Encoding]::UTF8.GetString($result.TaskStdOutput)
}
else {
if ($result.TaskErroutput -gt 1) {
$result.TaskErrOutput = [System.Text.Encoding]::UTF8.GetString($result.TaskErrOutput)
} } }
but it throws the below error
Cannot convert argument "bytes", with value: "92 92 80 65 82 70 79 82 65 80 48 48 50 92 97 112 120 36 92 120 109 108 92 114 101 113 117 101 115 116 46 120 109 108 32 62 32 92 92 80 65 82 70 79 82 65 80 48 48 50 92 97 112 120 36 92 120 109 108 92 114 101 113 117 101 115 116 50 48 49 55 48 54 48 57 49 54 49 55 46 120 109 108", for "GetString" to type "System.Byte[]": "Cannot convert value "92 92 80 65 82 70 79 82 65 80 48 48 50 92 97 112 120 36 92 120 109 108 92 114 101 113 117 101 115 116 46 120 109 108 32 62 32 92 92 80 65 82 70 79 82 65 80 48 48 50 92 97 112 120 36 92 120 109 108 92 114 101 113 117 101 115 116 50 48 49 55 48 54 48 57 49 54 49 55 46 120 109 108" to type "System.Byte[]". Error: "Cannot convert value "92 92 80 65 82 70 79 82 65 80 48 48 50 92 97 112 120 36 92 120 109 108 92 114 101 113 117 101 115 116 46 120 109 108 32 62 32 92 92 80 65 82 70 79 82 65 80 48 48 50 92 97 112 120 36 92 120 109 108 92 114 101 113 117 101 115 116 50 48 49 55 48 54 48 57 49 54 49 55 46 120 109 108" to type "System.Byte". Error: "Input string was not in a correct format.""
What am i doing wrong here?
For example using [System.Text.Encoding]::UTF8.GetString($jobandtasklist.TaskErrOutput)
Will return all the byte values as string as I want them but that is useless to me as I need these converted per result for them to have any meaning (Be associated with the particular job for example) rather than all the combined TaskStdOutput and TaskErrOutput in one giant blob.
What am i doing wrong here?
Column data imported from a CSV will always be of type [string]
- you first need to parse the list as a sequence of byte values before you can pass it to GetString
. We can take advantage of PowerShell's implicit conversion semantics to perform this task in two simple steps - split and cast:
# start with string containing sequence of decimal byte values
$taskOutput = '69 120 99 101 112 116 105 111 110 32 105 110'
# split the string into individual bytes, then use a cast to `[byte[]]` to convert
$bytes = [byte[]](-split $taskOutput)
# this is now going to work as expect
[Text.Encoding]::UTF8.GetString($bytes) # outputs 'Execution'
PowerShell will automatically figure out that each substring (eg. '116'
) can be parsed as a numeric literal (integer value 116
) which can then by fit into each [byte]
slot in the cast array.
You can do the parsing/conversion for both fields concurrently using calculated properties with Select-Object
:
$importedData = Import-Csv -Path 'C:\temp\ErrorReport.csv' |Select-Object Group, Name, Active, Taskname, ExitCodeResult, DatelastExecution, Description, @{Name='TaskStdOutput'; Expression={[Text.Encoding]::UTF8.GetString([byte[]](-split $_.TaskStdOutput))}}, @{Name='TaskErrOutput'; Expression={[Text.Encoding]::UTF8.GetString([byte[]](-split $_.TaskErrOutput))}}