PowerShell2.0で機能してタイプエイリアスを追加するコードを次に示します。しかし、問題はそれがスコープされていないということです。いくつかの追加作業を行うことで、名前空間を「インポート解除」できますが、これで良いスタートを切ることができます。
function Add-TypeAccelerator {
[CmdletBinding()]
param(
[Parameter(Position=1,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
[String[]]$Name,
[Parameter(Position=2,Mandatory=$true,ValueFromPipeline=$true)]
[Type]$Type,
[Parameter()]
[Switch]$Force
)
process {
$TypeAccelerators = [Type]::GetType('System.Management.Automation.TypeAccelerators')
foreach ($a in $Name) {
if ( $TypeAccelerators::Get.ContainsKey($a) ) {
if ( $Force ) {
$TypeAccelerators::Remove($a) | Out-Null
$TypeAccelerators::Add($a,$Type)
}
elseif ( $Type -ne $TypeAccelerators::Get[$a] ) {
Write-Error "$a is already mapped to $($TypeAccelerators::Get[$a])"
}
}
else {
$TypeAccelerators::Add($a, $Type)
}
}
}
}
using
ステートメントが導入されました。これは、.net名前空間またはモジュール(カスタムクラスをインポートする唯一の方法の1つ)に利用できます。構文はusing namespace Name.Space.Here
またはusing module C:\Path\to\manifest
です。唯一の要件は、スクリプト内の他のステートメント(paramブロックも含む)の前にあることです