डायनामिक रूप से PowerShell में उपनाम बनाना


3

मैं गतिशील रूप से कुछ उपनाम बनाना चाहता हूं, लेकिन मुझे अपना कोड काम करने के लिए नहीं मिल सकता है। यहाँ कोड है:

# 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."
    }
}

जब मैं "a", या "b", या किसी भी अक्षर को $ ड्राइव में टाइप करता हूं, तो उसे मेरी कार्य निर्देशिका को उस ड्राइव के अक्षर में बदलना चाहिए (उदाहरण: A :)।

अब मुझे जो त्रुटि मिल रही है, वह यह है:

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

किसी ने मुझे काम करने में मदद कर सकते हैं, कृपया?

जवाबों:


3

तत्काल समस्या यह है कि पावरशेल -Value GoToDrive($drive)स्विच को निर्दिष्ट करने के साथ-साथ -Value 'GoToDrive'एक स्थितिगत पैरामीटर भी व्याख्या कर रहा है $drive। (यह विचित्र और अकल्पनीय है, हाँ।) GoToDrive($drive)कोष्ठक में संलग्न करने के रूप में अभी तक कोई नहीं- GoToDriveसमारोह में फोन करने की कोशिश करेंगे और फिर परिणाम के लिए तर्क के रूप में उपयोग करें -Value, जो कि तब भी नहीं है जब आप GoToDriveपहले से परिभाषित किए गए थे। एक और समस्या यह है कि उपनाम उस आदेश को तर्क नहीं दे सकते हैं जिसे वे कहते हैं; वे केवल आदेशों के लिए वैकल्पिक नाम हैं।

आपको शॉर्टकट फ़ंक्शंस बनाने वाले आदेशों को गतिशील रूप से निष्पादित करने की आवश्यकता है:

# 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, या iexसंक्षेप में, अपने निर्धारित-ए-रनटाइम तर्क को चलाता है जैसे कि आपने इसे स्वयं कमांड लाइन पर टाइप किया था। ताकि अंतिम पंक्ति निष्पादित हो function a {GoToDrive 'a'}, फिर function b {GoToDrive 'b'}, और इसी तरह।


स्पष्टीकरण और कार्य संहिता के लिए बहुत बहुत धन्यवाद! मैं आपको सही जवाब के रूप में स्वीकार कर रहा हूं।
— प्रबीनी87
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.