Monday 15 April 2013

powershell - Convert date from CSV not work -



powershell - Convert date from CSV not work -

i seek convert date csv file, grab error : can not convert null 'system.datetime' type.

sample reproduce error :

$some_string = "(pdh-csv 4.0) (e. south america daylight time)(120),\\na\process(w3wp_10060)\% processor time,\\na\process(w3wp_10060)\% user time,\\na\process(w3wp_10060)\% privileged time,\\na\process(w3wp_10060)\page faults/sec,\\na\process(w3wp_10060)\working set,\\na\process(w3wp_10060)\private bytes,\\na\process(w3wp_10060)\thread count,\\na\process(w3wp_10060)\handle count`n" + "10/29/2014 09:37:41.056, , , , ,55205888,44847104,26,471`n" + "10/29/2014 09:37:46.931,0,0,0,0.17039241953890988,55209984,44847104,26,471`n" + "10/29/2014 09:37:52.806,0,0,0,0,55209984,44847104,26,471`n" $datasource = $some_string | convertfrom-csv -delim ',' foreach ($datapoint in $datasource) { $date = [datetime]$datapoint.$strdata write-host $date }

[edit]

this way worked:

$date = [datetime]$datapoint.'(pdh-csv 4.0) (e. south america daylight time)(120)'

any suggestion ?

the root cause not using set-psdebug -strict or set-strictmode -version latest. strict mode has several sanity checks:

prohibits references uninitialized variables (including uninitialized variables in strings). prohibits references non-existent properties of object. prohibits function calls utilize syntax calling methods. prohibits variable without name (${}).

in strict mode, you'd sensible errors like

the variable '$strdata' cannot retrieved because has not been set. variable '$date' cannot retrieved because has not been set.

instead of confusing

cannot convert null type "system.datetime".

after specifying strict mode, easy note $strdata variable uninitialized , effort $datapoint's non-existing fellow member futile.

powershell powershell-v3.0

No comments:

Post a Comment