Powershell 脚本 if else 语句

Powershell script if else statements

-U returns else echo 语句,我不明白为什么。如果似乎只是忽略了我的第一个 if 语句,其他一切都有效。用于文件夹导航的脚本函数。 -U 应该 return /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
      }

如果您希望参数做一些互斥的事情并且仅在指定 none 时显示帮助,您需要将所有检查链接在一个 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"
    }

我添加了一些丰富多彩的评论。这应该非常接近您要查找的内容。

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)
  }
}