Sunday, June 5, 2022

[SOLVED] Powershell script if else statements

Issue

-U returns the else echo statement and I can't figure out why. Everything else works if just seems to be ignoring my first if statement. The script functions for folder navigation. -U should return the /users/username directory. Thanks in advance for the help.

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
}

EDIT: Here is the code updated with the else if statement that doesn't work, it is ignoring the first if statement.

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
      }


Solution

If you want parameters to do something mutually exclusive and show help only if none are specified, you need to chain all your checks in a single if ... elseif ... elseif ... else chain:

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



Answered By - n0rd
Answer Checked By - Mary Flores (WPSolving Volunteer)