Dynamische Erstellung von Aliasnamen in PowerShell

443
prubini87

Ich möchte einige Aliase dynamisch erstellen, aber ich kann meinen Code nicht zum Laufen bringen. 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 $ Laufwerken eingebe, sollte sich mein Arbeitsverzeichnis in den Buchstaben dieses Laufwerks ändern (zB: A :).

Der Fehler, den ich jetzt bekomme, ist folgender:

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 jemand helfen, dass dies funktioniert?

3

1 Antwort auf die Frage

3
Ben N

Das unmittelbare Problem ist, dass PowerShell -Value GoToDrive($drive)den Schalter -Value 'GoToDrive'als auch einen Positionsparameter festlegt $drive. (Dies ist bizarr und nicht intuitiv, ja.) Enclosing GoToDrive($drive)in Klammern würde versuchen, die bis jetzt noch nicht existent nennen GoToDriveFunktion und dann die Ergebnisse als Argument verwendet für -Value, das ist nicht das, was sie ist, nachdem auch wenn GoToDrivezuvor definiert worden. Ein anderes Problem ist, dass Aliase keine Argumente für den Befehl liefern können, den sie aufrufen. 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' | % "} 

Invoke-Expressionoder, kurz iexgesagt, führt sein Argument zur Laufzeit bestimmt aus, als hätten Sie es selbst in die Befehlszeile eingegeben. So dass letzte Zeile ausführt function a , dann function b , und so weiter.

Vielen Dank für die Aufklärung und den Funktionscode! Ich akzeptiere deine als die richtige Antwort. prubini87 vor 5 Jahren 0