-U回傳 else echo 陳述句,我不知道為什么。如果似乎忽略了我的第一個 if 陳述句,其他一切都有效。檔案夾導航的腳本函式。-U應該回傳 /users/username 目錄。在此先感謝您的幫助。
function display-path {Get-ChildItem Env:Path }
function folder {
[CmdletBinding(DefaultParameterSetName='Default')]
param(
[Alias('u')]
[Parameter(ParameterSetName='User')]
[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 =
if ($Username) {
$targetFolder = '\user\username'
}
if ($Scripts) {
$targetFolder = '\Desktop\Scripts'
}
elseif ($Desktop) {
$targetFolder = '\Desktop'
}
if ($root) {
$targetFolder = 'c:\'
}
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
}
編輯: 這是用不起作用的 else if 陳述句更新的代碼,它忽略了第一個 if 陳述句。
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 =
if ($Username) {
$targetFolder = '\users\username
}
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
}
uj5u.com熱心網友回復:
如果您希望引數做一些互斥的事情并僅在未指定時才顯示幫助,您需要將所有檢查鏈接在一個if ... elseif ... elseif ... else鏈中:
if ($Username) {
$targetFolder = '\user\swmur'
}
elseif ($Scripts) {
$targetFolder = '\Desktop\Scripts'
}
elseif ($Desktop) {
$targetFolder = '\Desktop'
}
elseif ($root) {
$targetFolder = 'c:\'
}
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"
}
uj5u.com熱心網友回復:
我添加了一些豐富多彩的評論。這應該非常接近您正在尋找的內容。
function display-path {
<#
.SYNOPSIS
quick shortcut to get env:PATH
.DESCRIPTION
Call Get-ChildItem with the the -path set to "env:Path" which actually just outputs out the child items of the current folder...
.EXAMPLE
display-path | Format-List
Name : Path
Value : C:\Program Files\Eclipse Adoptium\jdk-17.0.2.8-hotspot\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPow
erShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\PuTTY\;C:\ProgramData\chocolatey\bin;C:\Program Files\Go\bin;C:\Program
Files\dotnet\;C:\Program Files\Eclipse Adoptium\jdk-17.0.2.8-hotspot\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\Syste
m32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\PuTTY\;C:\ProgramData\chocolatey\bin;C:\Program
Files\Go\bin;C:\Program Files\dotnet\;C:\Python310\Scripts;C:\Users\jedurham\AppData\Local\Programs\Microsoft VS Code\bin
.NOTES
not sure why anyone needs this
#>
Get-ChildItem -Path Env:Path
}
function folder {
<#
.SYNOPSIS
shortcut to move between folder someone uses often
.DESCRIPTION
shortcut to move between folder someone uses often.
can be used to quickly navigate to common directories.
.PARAMETER Username
Moves to the C:\Users\currentuser\ Folder.
.PARAMETER Scripts
Moves to a hard coded path called 'C:\Users\currentuser\Desktop\Scripts'
.PARAMETER Desktop
Moves to a hard coded path called 'C:\Users\currentuser\Desktop\'
.PARAMETER Help
Displays this file.
.PARAMETER root
Moves to the root of the current drive.
.EXAMPLE
folder -Username
C:> folder -U
You chose the -U flag!! Moving to C:\Users\currentuser\
.EXAMPLE
folder -Scripts
C:> folder -S
You chose the -S flag!! Moving to C:\Users\currentuser\Desktop\Scripts
.EXAMPLE
folder -Desktop
C:> folder -D
You chose the -D flag!! Moving to C:\Users\currentuser\Desktop\
.EXAMPLE
folder -root
C:\> folder -r
You chose the -R flag!! Moving to C:\
.NOTES
Needs a lot of work ....
v0.01
#>
[CmdletBinding(DefaultParameterSetName = 'Default')]
param(
[Alias('u')]
[Parameter(ParameterSetName = 'User')]
[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
)
$switchoutput = 'You chose the{0} flag!! Moving to {1}{2}'
if ($Username) {
## you need more comments in your code
## are you just trying to move the \user\current logged in?
## just use $env:USERPROFILE
$targetFolder = $env:USERPROFILE
$u = ' -U'
Write-Output -InputObject ($switchoutput -f $U, $targetFolder, '')
}
elseif ($Scripts) {
## a little tougher here because you need to hard code this path
## we could also ask for it ask an addendum to this switch :P
## ill do it this way
$targetFolder = $env:USERPROFILE
$s = ' -S '
## it might be better to define this else
$scriptspath = 'Desktop\Scripts'
$targetFolder = $env:USERPROFILE $scriptspath
Write-Output -InputObject ($switchoutput -f $S, $targetFolder, '')
}
elseif ($Desktop) {
## same as above
## it might be better to define this else
$desktop = '\Desktop\'
$targetFolder = $env:USERPROFILE $desktop
$d = ' -D '
Write-Output -InputObject ($switchoutput -f $d, $targetFolder, '')
}
elseif ($root) {
## same as other but we can use $env:homedrive for the root of C:
$targetFolder = $env:HOMEDRIVE '\'
$r = ' -R '
Write-Output -InputObject ($switchoutput -f $r, $targetFolder, '')
}
else {
Write-Output -InputObject "
-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"
-R Change to the Root of home directory"
}
if (Test-Path -Path $targetFolder) {
Set-Location -LiteralPath $targetFolder -Verbose
}
else {
Write-Output -InputObject ('{0} was not found :( exiting' -f $targetFolder)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/468987.html
上一篇:如何從設備名稱串列中獲取最大值
