다음은 유형 별칭을 추가하기 위해 PowerShell 2.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 네임 스페이스 또는 모듈 (사용자 지정 클래스를 가져 오는 유일한 방법 중 하나)에 대해이를 활용할 수 있습니다. 구문은using namespace Name.Space.Here
또는using module C:\Path\to\manifest
입니다. 유일한 요구 사항은 스크립트의 다른 문 앞에 오는 것입니다 (param 블록 포함)