在Powershell中設(shè)置別名的確方便快捷,但是在設(shè)置別名的過(guò)程中并設(shè)置參數(shù)的相關(guān)信息。盡管別名會(huì)自動(dòng)識(shí)別參數(shù),但是如何把經(jīng)常使用的參數(shù)默認(rèn)設(shè)定在別名里面呢?例如Test-Connection -Count 2 -ComputerName,讓-”-Count 2″ 固化在別名中。
這時(shí)簡(jiǎn)單的別名無(wú)法完成上述需求,可以通過(guò)函數(shù)來(lái)完成它,并且一旦把函數(shù)拉過(guò)來(lái),定義別名會(huì)變得更加靈活。
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
有了函數(shù)牽線(xiàn),別名可以完成更高級(jí)更強(qiáng)大的功能,其中$args為參數(shù)的占位符,經(jīng)測(cè)試,發(fā)現(xiàn)這個(gè)占位符必須以$args命名,否則不能識(shí)別,會(huì)拋出異常:
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.
新聞熱點(diǎn)
疑難解答
圖片精選