2016-09-21 31 views
1

私はExcel(.xlsx)ファイルを操作して.csvファイルからデータをコピーするpowershellスクリプトを持っています。ただし、地域と言語の形式を米国(ノルウェー)に変更すると、スクリプトは正常に実行されます。私の質問は、PowerShellスクリプトでこれをどうやってやるかです。地域の設定を変更することなく、スクリプトをオフィスの他の人が利用できるようにしたい。powershelの地域と言語のフォーマットを切り替えよう

私が試してみました:

$RegKeyPath = "HKCU:\Control Panel\International" 
Set-ItemProperty -Path $RegKeyPath -Name sCountry -Value "en-US" 

しかし、下図に示すように、これは地域の設定を変更しない:

+2

Excel処理の実際の問題は何ですか?ロケールはリストや小数点の区切り文字などを変更します。これらは他の方法でも克服することができます。 – vonPryz

+2

Excelでのロケールの問題は、通常、呼び出しスレッドに対してen-USカルチャを設定することで克服できます。システム全体の地域設定を変更する必要はありません。 –

+0

powershellスクリプトをバッチから呼び出す: Powershell.exe -executionpolicy remotesigned -myscript .ps1%var1%%var2% 地域コードを米国に設定して呼び出すと、すべてが期待どおりに実行されます。地域設定がノルウェーに設定されている場合、次のエラーがpowershellから繰り返し表示されます: "古い形式または無効なタイプライブラリ(HRESULTの例外:0x80028018(TYPE_E_INVDATAREAD))" – Ericvb86

答えて

0

あなたは私たちが特定のコマンドレットを持っているPowerShellで国際モジュールを使用することができます要件を満たすために。

Internation Module Usage

Internation module Script

オルタナティブ: それを設定するには、以下の機能を使用します。

Function Set-RegionSettings 
{ 
    [CmdletBinding()] 
    Param 
    (
     [Parameter(Mandatory=$true)] 
     [String]$Country, 
     [Parameter(Mandatory=$true)] 
     [String]$ShortDate, 
     [Parameter(Mandatory=$true)] 
     [String]$LongDate, 
     [Parameter(Mandatory=$true)] 
     [String]$ShortTime, 
     [Parameter(Mandatory=$true)] 
     [String]$TimeFormat, 
     [Parameter(Mandatory=$true)] 
     [String]$FirstDayOfWeek 
    ) 

    $RegKeyPath = "HKCU:\Control Panel\International" 
    If ($Country) 
    { 
     Set-ItemProperty -Path $RegKeyPath -Name sCountry -Value "$Country" 
     Write-Verbose "Successfully changed value of country." 
    } 

    If ($ShortDate) 
    { 
     Set-ItemProperty -Path $RegKeyPath -Name sShortDate -Value "$ShortDate" 
     Write-Verbose "Successfully changed value of short date." 
    } 

    If($LongDate) 
    { 
     Set-ItemProperty -Path $RegKeyPath -Name sLongDate -Value "$LongDate" 
     Write-Verbose "Successfully changed value of long date." 
    } 

    If($ShortTime) 
    { 
     Set-ItemProperty -Path $RegKeyPath -Name sShortTime -Value "$ShortTime" 
     Write-Verbose "Successfully changed value of short time." 
    } 

    If($TimeFormat) 
    { 
     Set-ItemProperty -Path $RegKeyPath -Name sTimeFormat -Value "$TimeFormat" 
     Write-Verbose "Successfully changed value of time format." 
    } 

    If($FirstDayOfWeek) 
    { 
     Set-ItemProperty -Path $RegKeyPath -Name iFirstDayOfWeek -Value "$FirstDayOfWeek" 
     Write-Verbose "Successfully changed value of first day of week." 
    } 

    $sCountry = (Get-ItemProperty -Path $RegKeyPath -Name sCountry).sCountry 
    $sShortDate = (Get-ItemProperty -Path $RegKeyPath -Name sShortDate).sShortDate 
    $sLongDate = (Get-ItemProperty -Path $RegKeyPath -Name sLongDate).sLongDate 
    $sShortTime = (Get-ItemProperty -Path $RegKeyPath -Name sShortTime).sShortTime 
    $sTimeFormat = (Get-ItemProperty -Path $RegKeyPath -Name sTimeFormat).sTimeFormat 
    $iFirstDayOfWeek = (Get-ItemProperty -Path $RegKeyPath -Name iFirstDayOfWeek).iFirstDayOfWeek 

    $Obj = New-Object -TypeName PSObject -Property @{ 
    "Country" = $sCountry 
    "Short date" = $sShortDate 
    "Long date" = $sLongDate 
    "Short time" = $sShortTime 
    "Long time" = $sTimeFormat 
    "First day of week" = $iFirstDayOfWeek 
    } 

    Write-Host "The current date and time formats:" 
    $Obj 
} 

USAGE:

Set-RegionSettings -ShortDate "M/d/yyyy" -LongDate "dddd,MMMM d,yyyy" -ShortTime "h:mm tt" -TimeFormat "h:mm:ss tt" -FirstDayOfWeek "Sunday" -Country "United States" 
を0

希望します。

関連する問題