locked
Powershell Scipt with passing an argument RRS feed

  • Question

  • I writing a powershell script to turn off/on an EC2 instance in AWS. I am trying to pass the one of actions from start,status,stop as param value to script.

    For example to execute the PS script, user have to give aws.ps1 start or aws.ps1 stop or aws.ps1 status

    So, I am using the param () definition inside script like below and executing IF, ELSEIF statements based on the user arguments.

    param (
    [Parameter(Mandatory=$true)]
    [string] $Action
    )

    IF ($ACTION -eq "start")

    {echo "Start EC2"}

    ELSEIF ($ACTION -eq "stop")

    {echo "Stop EC2"}

    ELSE

    {echo "Status of EC2"}

    But, while executing the script, it keeps on repeating the prompt for 

    InputObject[0]

    InputObject[1]

    InputObject[2]

    InputObject[3]

    .

    .

    .

    .

    .

    etc.

    What is is I am doing wrong, I am just a starter in Powershell scripting. Can someone help me out.

    Friday, December 21, 2018 11:31 AM

Answers

  • Try writing it correctly.

    param (
        [Parameter(Mandatory = $true)]
        [string]$Action
    )
    
    if($Action -eq 'start'){
        'Start EC2'
    }elseif($Action -eq 'stop'){
        'Stop EC2'
    }else{
        'Status of EC2'
    }

    You must supply an argument when calling the script or it will keep prompting until you do.

    help scripts

    Red all of it carefully


    \_(ツ)_/


    • Marked as answer by devopsn Monday, December 24, 2018 3:32 PM
    • Edited by jrv Monday, December 24, 2018 3:35 PM
    Friday, December 21, 2018 12:08 PM

All replies

  • Did you try to restart the console? Regardless of that you might use a ValidateSet for your parameter. That could reduce possible error because of typos. ;-) :-D

    Live long and prosper!

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

    Friday, December 21, 2018 12:08 PM
  • Try writing it correctly.

    param (
        [Parameter(Mandatory = $true)]
        [string]$Action
    )
    
    if($Action -eq 'start'){
        'Start EC2'
    }elseif($Action -eq 'stop'){
        'Stop EC2'
    }else{
        'Status of EC2'
    }

    You must supply an argument when calling the script or it will keep prompting until you do.

    help scripts

    Red all of it carefully


    \_(ツ)_/


    • Marked as answer by devopsn Monday, December 24, 2018 3:32 PM
    • Edited by jrv Monday, December 24, 2018 3:35 PM
    Friday, December 21, 2018 12:08 PM
  • Thanks @BOfH_666, validating the strings in the script is really helpful for me.
    Monday, December 24, 2018 3:33 PM
  • Thanks a lot @jrv,

    It worked!!!

    Monday, December 24, 2018 3:34 PM
  • Splitting the "else" like you did can cause issues especially with PS2 and earlier.

    A much better way to do this is like this:

    param (
        [ValidateSet('Start','Stop','Status')]
        [string]$Action
    )
    
    switch ($Action){
        Start  { 'Start EC2' }
        Stop   { 'Stop EC2' }
        Status { 'Status of EC2' }
    }
    
    This gives you everything including autocomplete for the parameter.


    \_(ツ)_/

    Monday, December 24, 2018 3:42 PM