2016-11-29 33 views
0

問題的ConvertTo-CSV -UseCulture忽略當前線程的文化

是否有可能迫使PowerShell中的Windows會話與en-GB文化運行時導出爲CSV法語格式?

更多信息

我希望利用法國文化規則的一些數據導出爲CSV(即CSV的分隔符設置爲分號,也與使用小數逗號的數字,和其他文化的格式差異;所以只使用-Delimiter參數是不夠的)。

我想出了下面的代碼(基於https://stackoverflow.com/a/7052955/361842

function Set-Culture 
{ 
    [CmdletBinding(DefaultParameterSetName='ByCode')] 
    param (
     [Parameter(Mandatory,ParameterSetName='ByCode',Position=1)] 
     [string] $CultureCode 
     , 
     [Parameter(Mandatory,ParameterSetName='ByCulture',Position=1)] 
     [System.Globalization.CultureInfo] $Culture 
    ) 
    begin { 
     [System.Globalization.CultureInfo] $Culture = [System.Globalization.CultureInfo]::GetCultureInfo($CultureCode) 
    } 
    process { 
     [System.Threading.Thread]::CurrentThread.CurrentUICulture = $Culture 
     [System.Threading.Thread]::CurrentThread.CurrentCulture = $Culture 
    } 
} 

function Invoke-CommandInCulture { 
    [CmdletBinding()] 
    param (
     [Parameter(Mandatory,ParameterSetName='ByCode',Position=1)] 
     [string]$CultureCode 
     , 
     [Parameter(Mandatory,Position=2)] 
     [ScriptBlock]$Code 
    ) 
    process { 
     $OriginalCulture = Get-Culture 
     try 
     { 
      Set-Culture $CultureCode 
      Write-Verbose (Get-Culture) #this always returns en-GB 
      Invoke-Command -ScriptBlock $Code 
     } 
     finally 
     { 
      Set-Culture $OriginalCulture 
     } 
    } 
} 

下面的代碼意味着這種方法可行:

Invoke-CommandInCulture -CultureCode 'fr' -Code { 
    [System.Threading.Thread]::CurrentThread.CurrentUICulture 
    [System.Threading.Thread]::CurrentThread.CurrentCulture 
} #shows that the command's thread's culture is French 

Invoke-CommandInCulture -CultureCode 'fr' -Code { 
    get-date 
} #returns the current date in French 

然而PowerShell有它發生了什麼事情

的自己的想法
Invoke-CommandInCulture -CultureCode 'fr' -Code { 
    get-culture 
    "PSCulture: $PSCulture" 
    "PSUICulture: $PSUICulture"   
} #returns my default (en-GB) culture; not the thread's culture 

而這會影響轉換爲CSV的邏輯:

Invoke-CommandInCulture -CultureCode 'fr' -Code { 
    get-process | ConvertTo-CSV -UseCulture 
} #again, uses my default culture's formatting rules; not the FR ones 
+0

本博客解釋了一些奇怪的行爲;即在當前管道完成後重置培養物;儘管到目前爲止我還沒有能夠解決'Export-Csv'的問題,即使有了這些知識/我看到的行爲似乎與此相矛盾... – JohnLBevan

+0

鏈接到上面提到的博客:http:// www .xipher.dk/WordPress /?p = 706 – JohnLBevan

回答

0

解決方法1:自定義功能將值轉換爲字符串在特定文化

這裏有一種變通方法解決方案;每個字段轉換爲字符串使用給定的培養物,然後將字符串值轉換成CSV:

function ConvertTo-SpecifiedCulture { 
    [CmdletBinding()] 
    param (
     [Parameter(Mandatory,ValueFromPipeline)] 
     [PSObject]$InputObject 
     , 
     [Parameter(Mandatory)] 
     [string]$CultureCode 
     , 
     [Parameter(ParameterSetName='DefaultParameter', Position=0)] 
     [System.Object[]]$Property 
    ) 
    begin { 
     [System.Globalization.CultureInfo] $Culture = [System.Globalization.CultureInfo]::GetCultureInfo($CultureCode) 
    } 
    process { 
     if($Property -eq $null) {$Property = $InputObject.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames} 
     $Result = new-object -TypeName PSObject -Property @{} 
     $Property | %{ 
      $Result | Add-Member -MemberType NoteProperty -Name $_ -Value ($InputObject."$_").ToString($Culture) 
     } 
     $Result 
    } 
} 

Get-Process | select -first 2 | ConvertTo-SpecifiedCulture -CultureCode 'fr' | ConvertTo-CSV -Delimiter ';' 

解決方法#2:覆蓋當前區域性以匹配目標文化

另一種選擇是改變當前文化的設置,以便共享所需文化的設置。這感覺更加怪異;儘管取決於情況可能會比上述更清潔/更實用。

例如使用FR的數字格式,我們只需更新當前區域性的數字格式以符合FR的:

$(Get-Culture).NumberFormat = ([System.Globalization.CultureInfo]'FR').NumberFormat 

...我們可以對剩餘的(設置)性能也這樣做:

function Set-CurrentCulture { 
    [CmdletBinding()] 
    param (
     [string]$CultureCode 
    ) 
    begin { 
     $Global:FakedCurrentCulture = $CultureCode #in case we need a reference to the current culture's name 
     [System.Globalization.CultureInfo]$NewCulture = [System.Globalization.CultureInfo]::GetCultureInfo($CultureCode) 
     [System.Globalization.CultureInfo]$ReferenceToCurrentCulture = Get-Culture 
     Write-Verbose "Switching Defintion to $($NewCulture.Name)" 
    } 
    process { 
     #NB: At time of writing, the only settable properties are NumberFormatInfo & DateTimeFormatInfo 
     $ReferenceToCurrentCulture.psobject.properties | ?{$_.isSettable} | %{ 
      $propertyName = $_.Name 
      write-verbose "Setting property $propertyName" 
      write-verbose "- from: $($ReferenceToCurrentCulture."$propertyName")" 
      write-verbose "- to: $($NewCulture."$propertyName")" 
      $ReferenceToCurrentCulture."$propertyName" = $NewCulture."$propertyName" 
     } 
     #ListSeparator 
     $ReferenceToCurrentCulture.TextInfo.psobject.properties | ?{$_.isSettable} | %{ 
      $propertyName = $_.Name 
      write-verbose "Setting property TextInfo.$propertyName" 
      write-verbose "- from: $($ReferenceToCurrentCulture.TextInfo."$propertyName")" 
      write-verbose "- to: $($NewCulture.TextInfo."$propertyName")" 
      $ReferenceToCurrentCulture.TextInfo."$propertyName" = $NewCulture."$propertyName" 
     } 
     #for some reason this errors 
     #Localized, TwoDigitYearMax 
     <# 
     $ReferenceToCurrentCulture.Calendar.psobject.properties | ?{$_.isSettable} | %{ 
      $propertyName = $_.Name 
      write-verbose "Setting property Calendar.$propertyName" 
      write-verbose "- from: $($ReferenceToCurrentCulture.Calendar."$propertyName")" 
      write-verbose "- to: $($NewCulture.Calendar."$propertyName")" 
      $ReferenceToCurrentCulture.Calendar."$propertyName" = $NewCulture."$propertyName" 
     } 
     #> 
    } 
} 

function Reset-CurrentCulture { 
    [CmdletBinding()] 
    param() 
    process { 
     Set-CurrentCulture -CultureCode ((get-culture).Name) 
    } 
} 

function Test-It { 
    [CmdletBinding()] 
    param() 
    begin { 
     write-verbose "Current Culture: $Global:FakedCurrentCulture" 
    } 
    process { 
     1..5 | %{ 
      New-Object -TypeName PSObject -Property @{ 
       Integer = $_ 
       String = "Hello $_" 
       Numeric = 2.139 * $_ 
       Money = (2.139 * $_).ToString('c') 
       Date = (Get-Date).AddDays($_) 
      } 
     } | ConvertTo-Csv -NoTypeInformation 
    } 
} 


Set-CurrentCulture 'fr' -Verbose 
Test-It 
Set-CurrentCulture 'en-GB' -Verbose 
Test-It 
Set-CurrentCulture 'en-US' -Verbose 
Test-It 
Set-CurrentCulture 'ar-DZ' -Verbose 
Test-It 

Reset-CurrentCulture -Verbose 
Test-It 

我們可能潛在進一步看看覆蓋只讀屬性(這是可能的:https://learn-powershell.net/2016/06/27/quick-hits-writing-to-a-read-only-property/)...但這已經感覺非常討厭;所以我不會去那裏,因爲上面的內容足以滿足我的需求。

+0

ps。請參閱http://codereview.stackexchange.com/questions/148463/powershell-export-csv-with-cultural-awareness以獲取關於此代碼的評論以及爲ConvertTo-CSV添加文化支持的代理功能。 – JohnLBevan