iainbrighton / PScribo

PowerShell documentation framework
MIT License
231 stars 35 forks source link

Date formatting is US on non-US systems outputting to HTML and Word #68

Closed wightsci closed 6 years ago

wightsci commented 6 years ago

It appears that when outputting DateTime information PScribo does not honour local date formatting conventions, except when outputting to Text. Dates appear in US mm/dd/yyyy format in Word and HTML output but appear correctly in dd/mm/yyyy format in Text format on my English GB setup.

iainbrighton commented 6 years ago

Hi @wightsci, hmmm this shouldn't occur 😞. Can you provide an example that shows this?

wightsci commented 6 years ago

Hi, Here's the code I'm running:

PS C:\Users\MyUser\desktop> Document 'All Devices not logged on recently' {

$computers = Get-SCCMCollectionMembers -collectionId ZZZ9999 | % {Get-ADComputer -Identity $_.Name -Pro LastLogonDate} |Select Name,LastLogonDate | Sort LastLogonDate $computers| Table -Columns Name,lastLogonDate -Headers 'Computer Name', 'Last Logon Date' -Width 0 } | Export-Document -Path ~\Desktop -Format Word,Html,Text -Verbose VERBOSE: The path provided as a string was divided to: directory part: C:\Users\MyUser\ ; file name part: Desktop . VERBOSE: [ 14:01:00:569 ] [ Export ] - Invoking 'Word' plugin. VERBOSE: [ 14:01:00:570 ] [ Word ] - Document 'All Devices not logged on recently' processing started. VERBOSE: [ 14:01:00:573 ] [ Word ] - Processing PScribo.Table 'RHWCDISP02'. VERBOSE: [ 14:01:00:820 ] [ Word ] - Processing document part '/word/document.xml'. VERBOSE: [ 14:01:00:824 ] [ Word ] - Writing document part '/word/document.xml'. VERBOSE: [ 14:01:00:829 ] [ Word ] - Processing document part '/word/styles.xml'. VERBOSE: [ 14:01:00:830 ] [ Word ] - Writing document part '/word/styles.xml'. VERBOSE: [ 14:01:00:833 ] [ Word ] - Processing document part '/word/settings.xml'. VERBOSE: [ 14:01:00:836 ] [ Word ] - Writing document part '/word/settings.xml'. VERBOSE: [ 14:01:00:839 ] [ Word ] - Generating package relationships. VERBOSE: [ 14:01:00:840 ] [ Word ] - Saving file 'C:\Users\MyUser\Desktop\All Devices not logged on recently.docx'. VERBOSE: [ 14:01:00:847 ] [ Word ] - Document 'All Devices not logged on recently' processing completed. VERBOSE: [ 14:01:00:849 ] [ Word ] - Total processing time '0.28' seconds. VERBOSE: [ 14:01:00:851 ] [ Export ] - Invoking 'Html' plugin. VERBOSE: [ 14:01:00:852 ] [ Html ] - Document 'All Devices not logged on recently' processing started. VERBOSE: [ 14:01:00:864 ] [ Html ] - Processing PScribo.Table 'RHWCDISP02'. VERBOSE: [ 14:01:00:879 ] [ Html ] - Document 'All Devices not logged on recently' processing completed. VERBOSE: [ 14:01:00:881 ] [ Html ] - Saving file 'C:\Users\MyUser\Desktop\All Devices not logged on recently.html'. VERBOSE: [ 14:01:00:886 ] [ Html ] - Total processing time '0.03' seconds. VERBOSE: [ 14:01:00:889 ] [ Export ] - Invoking 'Text' plugin. VERBOSE: [ 14:01:00:890 ] [ Text ] - Document 'All Devices not logged on recently' processing started. VERBOSE: [ 14:01:00:891 ] [ Text ] - Processing PScribo.Table 'RHWCDISP02'. VERBOSE: [ 14:01:00:933 ] [ Text ] - Document 'All Devices not logged on recently' processing completed. VERBOSE: [ 14:01:00:934 ] [ Text ] - Saving file 'C:\Users\MyUser\Desktop\All Devices not logged on recently.txt'. VERBOSE: [ 14:01:00:937 ] [ Text ] - Total processing time '0.04' seconds.

The results are shown below. The first is Word, Second HTML, Third Text.

image

iainbrighton commented 6 years ago

@wightsci Ah - I've worked out what's going on. The content is cast to a string and for DateTime objects this defaults to US date format in the Word and Html output. The Text output uses Format-Table under the hood which obviously calls .ToString() behind the scenes. Compare these two outputs generated on a en-GB system

PS C:\Users\Administrator> [string] (Get-Date)
12/06/2017 21:20:10

PS C:\Users\Administrator> (Get-Date).ToString()
06/12/2017 21:20:21

To solve your problem in the interim, you can manually force the conversion to a localised string at document creation time. You will see this problem outside of table output, e,g,:

Document 'All Devices not logged on recently' {
    Paragraph "Now is '$(Get-Date)' (cast) vs '$((Get-Date).ToString())' (to localised)"
    $computers = Get-ADComputer -Filter * -Pro LastLogonDate | Sort LastLogonDate | Select Name, @{ Name = 'LastLogonDate'; Expression = { $_.LastLogonDate.ToString() } }
    $computers | Table -Columns Name,lastLogonDate -Headers 'Computer Name', 'Last Logon Date' -Width 0
} | Export-Document -Path ~\Desktop -Format Word,Html,Text -Verbose

Note: I've had to swap the select and sort so that it's still sorted correctly!