PowerShell 别名分配工厂
PowerShell alias-assignment factory
我需要支持多个数据库实例,所以我创建了一个 'factory' 函数来为所需的 function/cmdlet:
分配一个别名 (Invoke-DbCmd
)
MySql(虚拟):
function Invoke-MySqlCmd {
param(
[string]$Query
)
write-debug "Invoke-MySqlCmd"
# add implementation
}
甲骨文(虚拟):
function Invoke-OracleCmd {
param(
[string]$Query
)
write-host "Invoke-OracleCmd"
# add implementation
}
'Factory'函数:
function Register-DbCommand {
param(
[ValidateSet('com.oracle','com.microsoft','org.mysql')]
[string]$Namespace
)
# remove existing assignment
Remove-Item alias:\Invoke-DbCmd -ErrorAction SilentlyContinue
$cmd = $null
switch ($Namespace) {
com.microsoft {
write-debug "Invoke-SqlCmd"
$cmd = Get-Command Invoke-SqlCmd
}
com.oracle {
write-debug "Invoke-OracleCmd"
$cmd = Get-Item Function:Invoke-OracleCmd
}
org.mysql {
write-debug "Invoke-MySqlCmd"
$cmd = Get-Item Function:Invoke-MySqlCmd
}
}
# return reference to new alias
Set-Alias -Name Invoke-DbCmd -Value $cmd -PassThru
}
虽然看起来已创建别名:
PS> register-dbcommand -Namespace 'com.oracle'
CommandType Name Version Source
----------- ---- ------- ------
Alias Invoke-DbCmd
别名未列出:
PS> get-alias | ? {$_.Definition -like 'Invoke*'} | select displayname
DisplayName
-----------
?? -> Invoke-NullCoalescing
curl -> Invoke-WebRequest
icm -> Invoke-Command
iex -> Invoke-Expression
ihy -> Invoke-History
ii -> Invoke-Item
irm -> Invoke-RestMethod
iwmi -> Invoke-WmiMethod
iwr -> Invoke-WebRequest
pester -> Invoke-Pester
psake -> Invoke-psake
r -> Invoke-History
wget -> Invoke-WebRequest
尝试使用别名生成异常:
PS> invoke-dbcmd
invoke-dbcmd : The term 'invoke-dbcmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ invoke-dbcmd
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (invoke-dbcmd:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
我错过了什么?
有趣的是,它似乎有效,但您可能应该使用 New-Alias
,因为您要先删除它。您可以添加 -Force
,然后不要删除现有的。
根据您的评论,以上内容似乎无关,请尝试使用 New-Alias -Scope Global
在全局范围内设置它。
我需要支持多个数据库实例,所以我创建了一个 'factory' 函数来为所需的 function/cmdlet:
分配一个别名 (Invoke-DbCmd
)
MySql(虚拟):
function Invoke-MySqlCmd {
param(
[string]$Query
)
write-debug "Invoke-MySqlCmd"
# add implementation
}
甲骨文(虚拟):
function Invoke-OracleCmd {
param(
[string]$Query
)
write-host "Invoke-OracleCmd"
# add implementation
}
'Factory'函数:
function Register-DbCommand {
param(
[ValidateSet('com.oracle','com.microsoft','org.mysql')]
[string]$Namespace
)
# remove existing assignment
Remove-Item alias:\Invoke-DbCmd -ErrorAction SilentlyContinue
$cmd = $null
switch ($Namespace) {
com.microsoft {
write-debug "Invoke-SqlCmd"
$cmd = Get-Command Invoke-SqlCmd
}
com.oracle {
write-debug "Invoke-OracleCmd"
$cmd = Get-Item Function:Invoke-OracleCmd
}
org.mysql {
write-debug "Invoke-MySqlCmd"
$cmd = Get-Item Function:Invoke-MySqlCmd
}
}
# return reference to new alias
Set-Alias -Name Invoke-DbCmd -Value $cmd -PassThru
}
虽然看起来已创建别名:
PS> register-dbcommand -Namespace 'com.oracle'
CommandType Name Version Source
----------- ---- ------- ------
Alias Invoke-DbCmd
别名未列出:
PS> get-alias | ? {$_.Definition -like 'Invoke*'} | select displayname
DisplayName
-----------
?? -> Invoke-NullCoalescing
curl -> Invoke-WebRequest
icm -> Invoke-Command
iex -> Invoke-Expression
ihy -> Invoke-History
ii -> Invoke-Item
irm -> Invoke-RestMethod
iwmi -> Invoke-WmiMethod
iwr -> Invoke-WebRequest
pester -> Invoke-Pester
psake -> Invoke-psake
r -> Invoke-History
wget -> Invoke-WebRequest
尝试使用别名生成异常:
PS> invoke-dbcmd
invoke-dbcmd : The term 'invoke-dbcmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ invoke-dbcmd
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (invoke-dbcmd:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
我错过了什么?
有趣的是,它似乎有效,但您可能应该使用 New-Alias
,因为您要先删除它。您可以添加 -Force
,然后不要删除现有的。
根据您的评论,以上内容似乎无关,请尝试使用 New-Alias -Scope Global
在全局范围内设置它。