Background
I am using Windows 11 Pro 21H2 in Japanese locale.
I am writing a PowerShell script to write some data to an Excel file (.xlsx) with Microsoft.ACE.OLEDB like this:
$fileName = "C:\tmp\createtest.xlsx"
$sheetName = "sheet1"
$provider = "Provider=Microsoft.ACE.OLEDB.12.0"
$dataSource = "Data Source = $fileName"
$extend = "Extended Properties=Excel 12.0"
$ddlSQL = "CREATE TABLE [$sheetName] (ID CHAR(4), NAME VARCHAR(20))"
$conn = New-Object System.Data.OleDb.OleDbConnection("$provider;$dataSource;$extend")
$sqlCommand = New-Object System.Data.OleDb.OleDbCommand
$sqlCommand.Connection = $conn
$conn.open()
$sqlCommand.CommandText = $ddlSQL
$sqlCommand.ExecuteNonQuery()
...
$conn.close()
For future error investigations, I want to log error messages of exceptions caused in the script.
Problem
When I ran the script in Japanese locale, I got the following error message. The description (comes from PowerShell) and the cause (comes from Microsoft.ACE.OLEDB) of the exception are written in Japanese:
"0" 個の引数を指定して "ExecuteNonQuery" を呼び出し中に例外が発生しました: "テーブル 'sheet1' は既に存在しています。"
発生場所 C:\tmp\CreateTest.ps1:15 文字:3
+ $sqlCommand.ExecuteNonQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : OleDbException
When I had changed OS locale to English and ran the script, I got the following error message. The description of the exception (comes from PowerShell) is written in English, but the cause of the exception (comes from Microsoft.ACE.OLEDB) is still written in Japanese:
Exception calling "ExecuteNonQuery" with "0" argument(s): "テーブル 'sheet1'
は既に存在しています。"
At C:\tmp\CreateTest.ps1:15 char:3
+ $sqlCommand.ExecuteNonQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : OleDbException
Question
I want to get the error message that both description and cause are written in English when I use English locale. How can I change the whole exception message from Microsoft.ACE.OLEDB according to the OS locale?
Edit
In my environment, Get-Culture, [CultureInfo]::CurrentCulture, and [Threading.Thread]::CurrentThread.CurrentCulture are all English (United States). But the message from Microsoft.ACE.OLEDB is Japanese.
PS C:\tmp> Get-Culture
LCID Name DisplayName
---- ---- -----------
1033 en-US English (United States)
PS C:\tmp> [CultureInfo]::CurrentCulture
LCID Name DisplayName
---- ---- -----------
1033 en-US English (United States)
PS C:\tmp> [Threading.Thread]::CurrentThread.CurrentCulture
LCID Name DisplayName
---- ---- -----------
1033 en-US English (United States)