-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xFailOverCluster: Localization support, localized strings in xCluster…
…Disk and some cleanup (#111) - Changes to xFailOverCluster - Added a common resource helper module with helper functions for localization. - Added helper functions; Get-LocalizedData, New-InvalidResultException, New-ObjectNotFoundException, New-InvalidOperationException and New-InvalidArgumentException. - Fixed lint error MD034 and fixed typos in README.md. - Changes to xClusterDisk - Enabled localization for all strings ([issue #84](#84)). - Changes to xClusterNetwork - Replaced the URL for the parameter Role in README.md. The new URL is a more generic description of the possible settings for the Role parameter. The previous URL was still correct but focused on Hyper-V in particular. - Fixed typos in parameter descriptions in README.md.
- Loading branch information
Showing
6 changed files
with
512 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,265 @@ | ||
<# | ||
.SYNOPSIS | ||
Creates and throws an invalid argument exception. | ||
.PARAMETER Message | ||
The message explaining why this error is being thrown. | ||
.PARAMETER ArgumentName | ||
The name of the invalid argument that is causing this error to be thrown. | ||
#> | ||
function New-InvalidArgumentException | ||
{ | ||
[CmdletBinding()] | ||
param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.String] | ||
$Message, | ||
|
||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.String] | ||
$ArgumentName | ||
) | ||
|
||
$argumentException = New-Object -TypeName 'ArgumentException' ` | ||
-ArgumentList @($Message, $ArgumentName) | ||
|
||
$newObjectParameters = @{ | ||
TypeName = 'System.Management.Automation.ErrorRecord' | ||
ArgumentList = @($argumentException, $ArgumentName, 'InvalidArgument', $null) | ||
} | ||
|
||
$errorRecord = New-Object @newObjectParameters | ||
|
||
throw $errorRecord | ||
} | ||
|
||
<# | ||
.SYNOPSIS | ||
Creates and throws an invalid operation exception. | ||
.PARAMETER Message | ||
The message explaining why this error is being thrown. | ||
.PARAMETER ErrorRecord | ||
The error record containing the exception that is causing this terminating error. | ||
#> | ||
function New-InvalidOperationException | ||
{ | ||
[CmdletBinding()] | ||
param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.String] | ||
$Message, | ||
|
||
[Parameter()] | ||
[ValidateNotNull()] | ||
[System.Management.Automation.ErrorRecord] | ||
$ErrorRecord | ||
) | ||
|
||
if ($null -eq $ErrorRecord) | ||
{ | ||
$invalidOperationException = New-Object -TypeName 'InvalidOperationException' ` | ||
-ArgumentList @($Message) | ||
} | ||
else | ||
{ | ||
$invalidOperationException = New-Object -TypeName 'InvalidOperationException' ` | ||
-ArgumentList @($Message, $ErrorRecord.Exception) | ||
} | ||
|
||
$newObjectParameters = @{ | ||
TypeName = 'System.Management.Automation.ErrorRecord' | ||
ArgumentList = @( | ||
$invalidOperationException.ToString(), | ||
'MachineStateIncorrect', | ||
'InvalidOperation', | ||
$null | ||
) | ||
} | ||
|
||
$errorRecordToThrow = New-Object @newObjectParameters | ||
|
||
throw $errorRecordToThrow | ||
} | ||
|
||
<# | ||
.SYNOPSIS | ||
Creates and throws an object not found exception. | ||
.PARAMETER Message | ||
The message explaining why this error is being thrown. | ||
.PARAMETER ErrorRecord | ||
The error record containing the exception that is causing this terminating error. | ||
#> | ||
function New-ObjectNotFoundException | ||
{ | ||
[CmdletBinding()] | ||
param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.String] | ||
$Message, | ||
|
||
[Parameter()] | ||
[ValidateNotNull()] | ||
[System.Management.Automation.ErrorRecord] | ||
$ErrorRecord | ||
) | ||
|
||
if ($null -eq $ErrorRecord) | ||
{ | ||
$exception = New-Object -TypeName 'System.Exception' ` | ||
-ArgumentList @($Message) | ||
} | ||
else | ||
{ | ||
$exception = New-Object -TypeName 'System.Exception' ` | ||
-ArgumentList @($Message, $ErrorRecord.Exception) | ||
} | ||
|
||
$newObjectParameters = @{ | ||
TypeName = 'System.Management.Automation.ErrorRecord' | ||
ArgumentList = @( | ||
$exception.ToString(), | ||
'MachineStateIncorrect', | ||
'ObjectNotFound', | ||
$null | ||
) | ||
} | ||
|
||
$errorRecordToThrow = New-Object @newObjectParameters | ||
|
||
throw $errorRecordToThrow | ||
} | ||
|
||
<# | ||
.SYNOPSIS | ||
Creates and throws an invalid result exception. | ||
.PARAMETER Message | ||
The message explaining why this error is being thrown. | ||
.PARAMETER ErrorRecord | ||
The error record containing the exception that is causing this terminating error. | ||
#> | ||
function New-InvalidResultException | ||
{ | ||
[CmdletBinding()] | ||
param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.String] | ||
$Message, | ||
|
||
[Parameter()] | ||
[ValidateNotNull()] | ||
[System.Management.Automation.ErrorRecord] | ||
$ErrorRecord | ||
) | ||
|
||
if ($null -eq $ErrorRecord) | ||
{ | ||
$exception = New-Object -TypeName 'System.Exception' ` | ||
-ArgumentList @($Message) | ||
} | ||
else | ||
{ | ||
$exception = New-Object -TypeName 'System.Exception' ` | ||
-ArgumentList @($Message, $ErrorRecord.Exception) | ||
} | ||
|
||
$newObjectParameters = @{ | ||
TypeName = 'System.Management.Automation.ErrorRecord' | ||
ArgumentList = @( | ||
$exception.ToString(), | ||
'MachineStateIncorrect', | ||
'InvalidResult', | ||
$null | ||
) | ||
} | ||
|
||
$errorRecordToThrow = New-Object @newObjectParameters | ||
|
||
throw $errorRecordToThrow | ||
} | ||
|
||
<# | ||
.SYNOPSIS | ||
Retrieves the localized string data based on the machine's culture. | ||
Falls back to en-US strings if the machine's culture is not supported. | ||
.PARAMETER ResourceName | ||
The name of the resource as it appears before '.strings.psd1' of the localized string file. | ||
For example: | ||
For WindowsOptionalFeature: MSFT_WindowsOptionalFeature | ||
For Service: MSFT_ServiceResource | ||
For Registry: MSFT_RegistryResource | ||
For Helper: xSQLServerHelper | ||
.PARAMETER ScriptRoot | ||
Optional. The root path where to expect to find the culture folder. This is only needed | ||
for localization in helper modules. This should not normally be used for resources. | ||
#> | ||
function Get-LocalizedData | ||
{ | ||
[CmdletBinding()] | ||
param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.String] | ||
$ResourceName, | ||
|
||
[Parameter()] | ||
[ValidateNotNullOrEmpty()] | ||
[System.String] | ||
$ScriptRoot | ||
) | ||
|
||
if ( -not $ScriptRoot ) | ||
{ | ||
$resourceDirectory = Join-Path -Path $PSScriptRoot -ChildPath $ResourceName | ||
$localizedStringFileLocation = Join-Path -Path $resourceDirectory -ChildPath $PSUICulture | ||
} | ||
else | ||
{ | ||
$localizedStringFileLocation = Join-Path -Path $ScriptRoot -ChildPath $PSUICulture | ||
} | ||
|
||
if (-not (Test-Path -Path $localizedStringFileLocation)) | ||
{ | ||
# Fallback to en-US | ||
if ( -not $ScriptRoot ) | ||
{ | ||
$localizedStringFileLocation = Join-Path -Path $resourceDirectory -ChildPath 'en-US' | ||
} | ||
else | ||
{ | ||
$localizedStringFileLocation = Join-Path -Path $ScriptRoot -ChildPath 'en-US' | ||
} | ||
} | ||
|
||
Import-LocalizedData ` | ||
-BindingVariable 'localizedData' ` | ||
-FileName "$ResourceName.strings.psd1" ` | ||
-BaseDirectory $localizedStringFileLocation | ||
|
||
return $localizedData | ||
} | ||
|
||
Export-ModuleMember -Function @( | ||
'New-InvalidArgumentException', | ||
'New-InvalidOperationException', | ||
'New-ObjectNotFoundException', | ||
'New-InvalidResultException', | ||
'Get-LocalizedData' ) |
Oops, something went wrong.