我想創建一個 powershell 函式或 cmdlet,它可以讓我快速導航到檔案和函式,如下例所示。有人可以告訴我“/ D”會被稱為什么嗎?是引數嗎?
C:\Windows\system32> folder /help
Usage: Select directory folder [/? | /Username | /S | /D]
No args Display help. This is the same as typing /?.
/? Display help. This is the same as not typing any options.
/Username Change to the "Username" directory
/S Change to the "scripts" directory
/D Change to the "desktop" directory
C:\Windows\system32> folder /username
C:\Users\username> folder /S
C:\Users\username\desktop\scripts> folder /D
C:\Users\username\desktop>
這就是我到目前為止所擁有的。這只會帶我到桌面目錄,我將如何撰寫它以將其命名為“檔案夾”并添加修飾符/引數?
function desktop { cd "C:\users\username\desktop" }
這功能如下:
C:\Windows\system32> desktop
C:\Users\username\desktop>
uj5u.com熱心網友回復:
是的,在 PowerShell 術語/D中稱為引數(其他 shell 可能將其稱為option或flag,正如Abraham Zinala 所指出的那樣),但請注意 PowerShell 僅接受-引數名稱 sigil,因此它必須是-D(在呼叫時你' 可以自由使用-dor -D,因為引數名稱在 PowerShell 中不區分大小寫)。
充當開/關開關的引數(在其他 shell 中稱為標志,即不帶引數的引數),在 PowerShell中稱為開關 [引數] 。
請注意,雖然您可以在 PowerShell 中定義單字母引數名稱,但習慣上使用較長的描述性名稱,例如-Desktop:
感謝 PowerShell 所稱的彈性語法,您仍然可以僅
-d在呼叫時使用,只要d明確暗示完整的目標引數名稱(例如,-Desktop)。或者,您可以通過使用屬性裝飾引數宣告來顯式宣告
-d為 的別名。-Desktop[Alias()]
這是一個示例函式,其作業方式與您指定的稍有不同,但以更符合 PowerShell 的慣用方式運行:
對當前用戶的主檔案夾進行無引數呼叫更改。
- 要定位其他用戶的主檔案夾,請將用戶名傳遞給
-u/-UserName引數。
- 要定位其他用戶的主檔案夾,請將用戶名傳遞給
在給定呼叫中選擇的任何用戶的主檔案夾都可以選擇與/或/組合,以便更改為目標用戶的桌面 / 腳本檔案夾。
-Desktop-d-Scripts-s要顯示幫助,請使用PowerShell 默認支持的
-?(或Get-Help folder),顯示命令的語法。- 要向幫助添加口頭描述,您可以對函式使用基于注釋的幫助 - 請參閱概念about_Comment_Based_Help幫助主題。
Push-Location而不是Set-Location(其內置別名是cd)用于更改位置,以啟用回傳到上一個檔案夾Pop-Location(其內置別名是popdandpopl)下面的函式是所謂的高級函式,這意味著它就像一個(二進制)cmdlet;有關詳細資訊,請參閱概念about_Functions_Advanced和about_Functions_Advanced_pa??rameters幫助主題。
function folder {
[CmdletBinding(DefaultParameterSetName='Default')]
param(
[Alias('u')]
[string] $Username
,
[Alias('s')]
[Parameter(ParameterSetName='Scripts')]
[switch] $Scripts
,
[Parameter(ParameterSetName='Desktop')]
[Alias('d')]
[switch] $Desktop
)
$targetFolder =
if ($Username) {
Join-Path (Split-Path -LiteralPath $HOME) $Username
} else {
$HOME
}
if ($Scripts) {
$targetFolder = '\Desktop\Scripts'
} elseif ($Desktop) {
$targetFolder = '\Desktop'
}
Push-Location -LiteralPath $targetFolder
}
示例呼叫:
folder # change to current user's home folder
folder -d # change to current user's desktop folder
folder -u jdoe -s # change to jdoe's scripts folder
folder -? # show help
uj5u.com熱心網友回復:
終于修好了!
我需要一個標志或一個跟蹤變數,本質上是在第一個 if 陳述句之前定義 $targetFolder。我想知道它是否試圖將目標檔案夾定義為整個 if 陳述句。在此處查看多個 if 陳述句的 windows 檔案
function display-path {Get-ChildItem Env:Path }
<#
**One of these can be used to navigate to the username directory. The first will only allow you to navigate to one user, the second allows for you to select the user.**
$targetFolder =
if ($Username) {
$targetFolder = '\user\Username' #replace with your username
}
if ($Username) {
Join-Path (Split-Path -LiteralPath $HOME) $Username
} else {
$HOME
} #>
function folder {
[CmdletBinding(DefaultParameterSetName='Default')]
param(
[Alias('u')]
[switch] $Username
,
[Alias('s')]
[Parameter(ParameterSetName='Scripts')]
[switch] $Scripts
,
[Parameter(ParameterSetName='Desktop')]
[Alias('d')]
[switch] $Desktop
,
[Alias('h')]
[Parameter(ParameterSetName = 'help')]
[switch]$Help
,
[Alias('r')]
[Parameter(ParameterSetName = 'root')]
[switch]$root
)
$targetFolder = $env:USERPROFILE
if ($Username) {
$targetFolder = $env:USERPROFILE
$u = ' -U'
}
elseif ($Scripts) {
$targetFolder = '\Desktop\Scripts'
}
elseif ($Desktop) {
$targetFolder = '\Desktop'
}
elseif ($root) {
## same as other but we can use $env:homedrive for the root of C:
$targetFolder = $env:HOMEDRIVE '\'
$r = ' -R '
}
elseif ($Help) {
echo "
-H Display help. This is the same as not typing any options.
-U Change to the 'Username' directory
-S Change to the 'scripts' directory
-D Change to the 'desktop' directory"
}
else {
echo "
-H Display help. This is the same as not typing any options.
-U Change to the 'Username' directory
-S Change to the 'scripts' directory
-D Change to the 'desktop' directory"
}
Push-Location -LiteralPath $targetFolder
}
隨意使用它為您自己的導航,感謝您的幫助!這是代碼的 github 鏈接:https ://github.com/semiexperienced/Windows-Powershell-Alias-and-Navigation
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/468811.html
