Windows Powershell 通過函數擴充別名_PowerShell

來源:互聯網
上載者:User

在Powershell中設定別名的確方便快捷,但是在設定別名的過程中並設定參數的相關資訊。儘管別名會自動識別參數,但是如何把經常使用的參數預設設定在別名裡面呢?例如Test-Connection -Count 2 -ComputerName,讓-”-Count 2″ 固化在別名中。

這時簡單的別名無法完成上述需求,可以通過函數來完成它,並且一旦把函數拉過來,定義別名會變得更加靈活。

PS C:\PS> function test-conn { Test-Connection -Count 2 -ComputerName $args}PS C:\PS> Set-Alias tc test-connPS C:\PS> tc localhostSource    Destination   IPV4Address   IPV6Address               Bytes  Time(ms)------    -----------   -----------   -----------               -----  --------test-me-01  localhost    127.0.0.1    ::1                   32    0test-me-01  localhost    127.0.0.1    ::1                   32    0

有了函數牽線,別名可以完成更進階更強大的功能,其中$args為參數的預留位置,經測試,發現這個預留位置必須以$args命名,否則不能識別,會拋出異常:
Cannot validate argument on parameter ‘ComputerName'. The argument is null or empty. Supply an arg
nt that is not null or empty and then try the command again.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.