2016-06-02 17 views
2

Ich brauche mehrere Datenbankinstanzen zu unterstützen, so habe ich eine 'Fabrik' Funktion einen Alias ​​(Invoke-DbCmd) auf die gewünschte Funktion/cmdlet zuzuweisen:Power alias-Zuweisung Fabrik

MySql (dummy):

function Invoke-MySqlCmd { 
    param(
     [string]$Query 
    ) 
    write-debug "Invoke-MySqlCmd" 

    # add implementation 
} 

Oracle (dummy):

function Invoke-OracleCmd { 
    param(
     [string]$Query 
    ) 
    write-host "Invoke-OracleCmd" 

    # add implementation 
} 

'Fabrik' Funktion:

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 

} 

Während es scheint, dass der Alias ​​erstellt:

PS> register-dbcommand -Namespace 'com.oracle' 
CommandType  Name            Version Source 
-----------  ----            ------- ------ 
Alias   Invoke-DbCmd 

Der Alias ​​ist nicht aufgelistet:

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 

Versuche, die Alias ​​zu verwenden, erzeugen eine Ausnahme:

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 

Was vermisse ich?

Antwort

1

Es ist interessant, dass es scheint zu funktionieren, aber Sie sollten wahrscheinlich New-Alias verwenden, da Sie es zuerst entfernen. Sie könnten -Force hinzufügen und dann auch nicht entfernen.

Basierend auf Ihren Kommentaren scheint das oben genannte nicht verwandt zu sein, indem Sie versuchen, es im globalen Bereich mit New-Alias -Scope Global zu setzen.

+0

Deaktiviert die Zeile "Remove-Item". Die letzte Zeile wurde in 'New-Alias ​​-Name Invoke-DbCmd -Value $ cmd -Force -PassThru' geändert. Gleiches Verhalten. – craig

+0

@craig siehe bearbeiten – briantist

+0

'Scope Global' hat den Trick gemacht. Ich habe auch meine ursprünglichen Syntaxelemente aktiviert. – craig