ฉันสามารถปรับแต่งข้อผิดพลาด 'ไม่รู้จักเป็นชื่อของ cmdlet' ใน Powershell ได้หรือไม่

สมมติว่าฉันพิมพ์ผิดในบรรทัดคำสั่ง:

whih foo

Powershell ส่งคืน:

whih : The term 'whih' 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
+ whih mocha
+ ~~~~
+ CategoryInfo          : ObjectNotFound: (whih:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

ข้อความขนาดยาวมีประโยชน์สำหรับสคริปต์ แต่สำหรับการใช้งานเชลล์เชิงโต้ตอบ ฉันต้องการปิดด้วยข้อความที่สั้นกว่า เช่น:

'whih' isn't a cmdlet, function, script file, or operable program.

ฉันสามารถปิดข้อผิดพลาดและเปลี่ยนเป็นสิ่งที่สั้นกว่านี้ได้หรือไม่


person mikemaccana    schedule 02.08.2018    source แหล่งที่มา
comment
ต้องถาม แต่ฉันจะถือว่า try block ไม่ใช่สิ่งที่คุณต้องการ (ออกแบบมาเพื่อจุดประสงค์นี้) และคุณกำลังมองหาที่จะเปลี่ยนการใช้คำฟุ่มเฟือยที่แท้จริงของข้อความ   -  person Matt    schedule 02.08.2018
comment
@Matt ถูกต้องมันมีไว้สำหรับการใช้งานแบบโต้ตอบตามที่กล่าวไว้ ใช่แล้ว ฉันไม่ต้องการรวมทุกคำสั่งไว้ในบล็อก try แค่มองหาบางอย่างสำหรับ $profile   -  person mikemaccana    schedule 02.08.2018


คำตอบ (1)


ใช่ คุณสามารถสกัดกั้น CommandNotFoundException ด้วย a CommandNotFoundAction!

$ExecutionContext.InvokeCommand.CommandNotFoundAction = {
  param($Name,[System.Management.Automation.CommandLookupEventArgs]$CommandLookupArgs)  

  # Check if command was directly invoked by user
  # For a command invoked by a running script, CommandOrigin would be `Internal`
  if($CommandLookupArgs.CommandOrigin -eq 'Runspace'){
    # Assign a new action scriptblock, close over $Name from this scope 
    $CommandLookupArgs.CommandScriptBlock = {
      Write-Warning "'$Name' isn't a cmdlet, function, script file, or operable program."
    }.GetNewClosure()
  }
}
person Mathias R. Jessen    schedule 02.08.2018
comment
นั่นง่ายกว่าสิ่งที่ฉันคิดจะทำ - person Matt; 02.08.2018
comment
การใช้พร็อกซี Format-Default หรือการค้นหาไฟล์ภาษา/ข้อมูลที่เกี่ยวข้องกับข้อความนั้น ฉันไม่คิดว่าคุณจะสร้างสิ่งกระตุ้นแบบนั้นได้ - person Matt; 02.08.2018
comment
@ Matt ahh นั่นทั้งฉลาดและไม่เหมาะสมอย่างยิ่ง: P - person Mathias R. Jessen; 02.08.2018
comment
อย่างแท้จริง. อย่างไรก็ตามมันเป็นทางเลือกเดียวที่ฉันคิดออก ตอนนี้ฉันต้องค้นหาสิ่งที่คุณทำที่นี่เพื่อดูว่าฉันจะใช้มันได้อย่างไร :) - person Matt; 02.08.2018
comment
@Matt โทรหาฉันบน Twitter (หรือ gitter) หากคุณต้องการแรงบันดาลใจเพิ่มเติม;) - person Mathias R. Jessen; 02.08.2018
comment
ชอบ +1. $Name จะขึ้นต้นด้วยกริยา Get- เสมอโดยอัตโนมัติหรือไม่ - person ; 02.08.2018
comment
@LotPings เฉพาะเมื่อคำค้นหาไม่อยู่ในรูปแบบ Verb-Noun (ข้อผิดพลาด CommandDiscovery API เกิดขึ้นหลังจากลอง สองครั้ง หนึ่งครั้งเหมือนเดิม หนึ่งครั้งโดยเติม Get- นำหน้า) - person Mathias R. Jessen; 02.08.2018
comment
@LotPings PowerShell soft-aliases คำสั่งทั้งหมดโดยไม่มีกริยาเป็น Get- คุณสามารถสังเกตสิ่งนี้ได้โดยพิมพ์ service ที่พรอมต์ - person Maximilian Burszley; 02.08.2018
comment
คำตอบที่ยอดเยี่ยมและทำงานได้อย่างสมบูรณ์แบบ เหตุใด .GetNewClosure() จึงจำเป็น? ขอบคุณ! - person mikemaccana; 03.08.2018
comment
นอกจากนี้: สิ่งนี้ยังส่งผลต่อสคริปต์ ps1 ด้วย มีวิธีที่จะเรียกใช้เฉพาะการใช้งานเชิงโต้ตอบหรือไม่? - person mikemaccana; 03.08.2018
comment
@mikemaccana .GetNewClosure() จำเป็นต้องปิดตัวแปร $Name อัปเดตคำตอบเพื่อตอบคำถามของคุณทั้งสอง - person Mathias R. Jessen; 03.08.2018
comment
ขอบคุณ @ MathiasR.Jessen - ได้รับการยอมรับว่าเป็นคำตอบเนื่องจากตรงตามข้อกำหนดทั้งหมด! - person mikemaccana; 03.08.2018