-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 += 'userusername'
}
if ($Scripts) {
$targetFolder += 'DesktopScripts'
}
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 += 'userUsername' #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 += 'usersusername
}
elseif ($Scripts) {
$targetFolder += 'DesktopScripts'
}
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
}
如果你想让参数做一些互斥的事情,并且只在没有指定的情况下显示帮助,你需要在一个单独的if ... elseif ... elseif ... else
链中链接你的所有检查:
if ($Username) {
$targetFolder += 'userswmur'
}
elseif ($Scripts) {
$targetFolder += 'DesktopScripts'
}
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"
}
我添加了一些丰富多彩的评论。这应该非常接近你要找的。
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 FilesEclipse Adoptiumjdk-17.0.2.8-hotspotbin;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPow
erShellv1.0;C:WINDOWSSystem32OpenSSH;C:Program FilesPuTTY;C:ProgramDatachocolateybin;C:Program FilesGobin;C:Program
Filesdotnet;C:Program FilesEclipse Adoptiumjdk-17.0.2.8-hotspotbin;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSyste
m32WindowsPowerShellv1.0;C:WINDOWSSystem32OpenSSH;C:Program FilesPuTTY;C:ProgramDatachocolateybin;C:Program
FilesGobin;C:Program Filesdotnet;C:Python310Scripts;C:UsersjedurhamAppDataLocalProgramsMicrosoft VS Codebin
.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:Userscurrentuser Folder.
.PARAMETER Scripts
Moves to a hard coded path called 'C:UserscurrentuserDesktopScripts'
.PARAMETER Desktop
Moves to a hard coded path called 'C:UserscurrentuserDesktop'
.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:Userscurrentuser
.EXAMPLE
folder -Scripts
C:> folder -S
You chose the -S flag!! Moving to C:UserscurrentuserDesktopScripts
.EXAMPLE
folder -Desktop
C:> folder -D
You chose the -D flag!! Moving to C:UserscurrentuserDesktop
.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 usercurrent 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 = 'DesktopScripts'
$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)
}
}