locked
Script error RRS feed

  • Question

  • I am trying to learn powershell my book has a lab and i can not get my code to execute without an error.

    here is the error, I can't figure out what i am doing wrong any help would be appreciated.

    Elseif : The term 'Elseif' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling

    of the name, or if a path was included, verify that the path is correct and try again.

    At C:\Users\Administrator\Scripts\Get-Inventorya.ps1:39 char:1

    + Elseif ( $menu_choice -eq "X" )

    + ~~~~~~

        + CategoryInfo          : ObjectNotFound: (Elseif:String) [], CommandNotFoundException

        + FullyQualifiedErrorId : CommandNotFoundException

    #Initialization
    # This section contains variable declarations and assignments
    $menu_choice = “”
    $menu = @”
    				Inventory System Menu
    ===================================================
    		A - Installed Software Inventory
    		B - Basic Hardware Inventory
    				X - Exit
    ===================================================
    “@
    
    #Main Body
    #This section contains the code that implements that main function of the script.#
    $menu
    $menu_choice = Read-Host -Prompt “Choose a menu option”
    $sure = Read-Host -Prompt “Are you sure you want to exit? (Y)”
    if ($sure -eq “Y”)
    {
    Write-Host “This I can do.”
    Write-Host “Exiting…”
    }
    Else
    {
    Write-Host "Not Exiting :)"
    }
    ElseIf ( $menu_choice -eq “X” )
    {
        $sure = Read-Host -prompt "are you sure you want to exit? (Y)"
        {
    Write-Host “This I can do.”
    Write-Host “Exiting…”
    }
    Else
    {
    Write-host "Not Exiting :)"
    }
    }



    Ray Sawyer MCSA

    Friday, May 26, 2017 6:50 PM

Answers

  • Hi Ray Sawyer.

    The ElseIf block cannot be written after an Else block: the Else block must be the last alternative, since it will contain code that will be executed if none of the previous ElseIf blocks can satisfy the tested condition.

    Bye.


    Luigi Bruno
    MCP, MCTS, MOS, MTA

    Friday, May 26, 2017 7:03 PM

All replies

  • help about_if

    Read it and look at the examples and rules.


    \_(ツ)_/

    Friday, May 26, 2017 6:57 PM
  • ElseIf has to follow If.

    Edit: and you should ask your money back if the book teaches you to use Write-Host.  ;-)


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Friday, May 26, 2017 6:58 PM
    Friday, May 26, 2017 6:57 PM
  • Hi Ray Sawyer.

    The ElseIf block cannot be written after an Else block: the Else block must be the last alternative, since it will contain code that will be executed if none of the previous ElseIf blocks can satisfy the tested condition.

    Bye.


    Luigi Bruno
    MCP, MCTS, MOS, MTA

    Friday, May 26, 2017 7:03 PM
  • Thanks that pointed me in the right direction.

    Ray Sawyer MCSA

    Friday, May 26, 2017 7:15 PM
  • Thanks that pointed me in the right direction.

    Ray Sawyer MCSA

    You're welcome.

    Bye.


    Luigi Bruno
    MCP, MCTS, MOS, MTA

    Friday, May 26, 2017 7:17 PM