Dynamisches Erstellen von Aliasnamen in PowerShell

Dynamisches Erstellen von Aliasnamen in PowerShell

Ich möchte einige Aliase dynamisch erstellen, aber mein Code funktioniert nicht. Hier ist der Code:

# Drives
$drives = ("a","b","c","d","e")
foreach ($drive in $drives) {
    New-Item -Path alias:\ -Name $drive -Value GoToDrive($drive) #.GetNewClosure()
}

function GoToDrive($drive) {
    $formatted = "$($drive):\"
    if (Test-Path $formatted) {
        Set-Location $formatted
    } else {
        Write-Host "`"$formatted`" does not exist."
    }
}

Wenn ich „a“ oder „b“ oder einen der Buchstaben in $drives eingebe, sollte mein Arbeitsverzeichnis auf den Buchstaben dieses Laufwerks geändert werden (z. B. A:).

Der Fehler, den ich jetzt bekomme, ist dieser:

New-Item : A positional parameter cannot be found that accepts argument 'a'.
At C:\Users\prubi\OneDrive\Documentos\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:17 char:5
+     New-Item -Path alias:\ -Name $drive -Value GoToDrive($drive) #.Ge ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-Item], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : A positional parameter cannot be found that accepts argument 'b'.
At C:\Users\prubi\OneDrive\Documentos\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:17 char:5
+     New-Item -Path alias:\ -Name $drive -Value GoToDrive($drive) #.Ge ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-Item], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : A positional parameter cannot be found that accepts argument 'c'.
At C:\Users\prubi\OneDrive\Documentos\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:17 char:5
+     New-Item -Path alias:\ -Name $drive -Value GoToDrive($drive) #.Ge ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-Item], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : A positional parameter cannot be found that accepts argument 'd'.
At C:\Users\prubi\OneDrive\Documentos\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:17 char:5
+     New-Item -Path alias:\ -Name $drive -Value GoToDrive($drive) #.Ge ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-Item], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : A positional parameter cannot be found that accepts argument 'e'.
At C:\Users\prubi\OneDrive\Documentos\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:17 char:5
+     New-Item -Path alias:\ -Name $drive -Value GoToDrive($drive) #.Ge ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-Item], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand

Kann mir bitte jemand helfen, das zum Laufen zu bringen?

Antwort1

-Value GoToDrive($drive)Das unmittelbare Problem besteht darin, dass PowerShell dies als Angabe des Schalters -Value 'GoToDrive'und auch als Angabe eines Positionsparameters interpretiert $drive. (Das ist bizarr und nicht intuitiv, ja.) Das Einschließen GoToDrive($drive)in Klammern würde versuchen, die noch nicht vorhandene GoToDriveFunktion aufzurufen und dann die Ergebnisse als Argument für zu verwenden -Value, was nicht das ist, wonach Sie suchen, selbst wenn GoToDrivees zuvor definiert wurde. Ein weiteres Problem besteht darin, dass Aliase dem von ihnen aufgerufenen Befehl keine Argumente liefern können; sie sind nur alternative Namen für Befehle.

Sie müssen Befehle dynamisch ausführen, die die Verknüpfungsfunktionen erstellen:

# This is the exact same GoToDrive function you've been using
function GoToDrive($drive) {
    $formatted = "$($drive):\"
    if (Test-Path $formatted) {
        Set-Location $formatted
    } else {
        Write-Host "`"$formatted`" does not exist."
    }
}

# This does the magic
'a', 'b', 'c', 'd', 'e' | % {iex "function $_ {GoToDrive '$_'}"}

Invoke-Expression, oder iexkurz gesagt, führt sein zur Laufzeit ermitteltes Argument aus, als hätten Sie es selbst in die Befehlszeile eingegeben. Die letzte Zeile führt also aus function a {GoToDrive 'a'}, dann function b {GoToDrive 'b'}und so weiter.

verwandte Informationen