2016-10-19 23 views
0

我已經把下列我$PROFILE需要一起傳遞函數「別名」參數

function dir 
{ 
    Get-ChildItem -Force $args 
} 

我要的是一個簡單的「目錄」命令,該命令列出所有文件,包括隱藏和系統文件。

但是,只要我傳遞一些參數一起:

dir \ -Directory 

我獎勵其次是這個錯誤我的根的完整列表:

Get-ChildItem : Cannot find path 'C:\Users\myuser-Directory' because it does not exist.

我的目的是當然是我的小自制「別名」將擴展到這:

Get-ChildItem -Force \ -Directory 

而是「-Directory」get s被視爲字符串文字。我怎樣才能讓我的dir函數按照我想要的方式傳遞參數(而不是字符串數組)?

回答

1

$args是一個數組。如果你使用這樣的:

Get-ChildItem -Force $args 

這是因爲如果你這樣做是相同的:

Get-ChildItem -Force -Path '\', '-Directory' 

其中工程(在某種程度上),因爲參數-Path接受數組輸入。

使用splatting,以避免這一缺陷:

Get-ChildItem -Force @args 
相關問題