AWS と対話するための powershell モジュールを作成しています。ほとんどの関数は、使用する資格情報であるパラメータを取る必要があります - awsAccessKeyId
、awsSecretKey
およびcredentialsFile
.
モジュール内の各関数にこれらのパラメーターをコピー/貼り付けするのは退屈です。
CommonParameters
これらがモジュールがエクスポートする一連の関数用であることを宣言する方法はありますか?
また、必要なすべての関数から呼び出せるように、共通の (つまり、重複した) パラメータ セット処理の switch-statement を抽出する方法はありますか?
関数の例を次に示します。
function New-S3Client {
[CmdletBinding(DefaultParametersetName="credentialsFile")]
param
(
[parameter(Mandatory=$true, ParameterSetName="specifyKey")] [string]$accessKey,
[parameter(Mandatory=$true, ParameterSetName="specifyKey")] [string]$secretKey,
[parameter(ParameterSetName="credentialsFile")] [string]$credentialsFile = "$env:USERPROFILE\.aws\credentials"
)
switch($PsCmdlet.ParameterSetName)
{
"specifyKey" {
$env:awsAccessKeyId = $accessKey
$env:awsSecretKey = $secretKey
break
}
"credentialsFile" {
$env:awsAccessKeyId = Read-ValueForKeyFromFile -from $credentialsFile -field AWSAccessKeyId
$env:awsSecretKey = Read-ValueForKeyFromFile -from $credentialsFile -field AWSSecretKey
break
}
}
$config = New-Object Amazon.S3.AmazonS3Config
$config.WithServiceURL("https://s3.eu-west-1.amazonaws.com")
$client = New-Object Amazon.S3.AmazonS3Client($env:awsAccessKeyId, $env:awsSecretKey, $config)
return $client
}
CommonParameters
パラメータ 2 ~ 4 を に抽出し、次に switch ブロックをいくつかの一般的な関数に抽出したいと思います。