none
Execute part of Script only if parameter is set RRS feed

  • Question

  • Hi there

    I would like to merge two scripts which have actually only one command difference.

    In batch I would add an section and ignore this section if no parameter is given.

    But how to do this in powershell?

    Just for example

    :makedir1
    md 111
    md 222
    md 333
    goto done
    
    :makedir2
    md AAA
    md BBB
    md CCC
    
    :done
    if /i %1 == all call :makedir2

    If I start the Batch only the first 3 dirs are created.

    If I start the Batch with the Parameter "all" all 6 dirs are created.

    But how to do this in Powershell?

    THX

    Wednesday, June 29, 2016 1:14 PM

Answers

All replies

  • What have you tried?

    We will not convert scripts fro you.  We can answer questions and help with issues.  The forum is not for free consulting.

    Also note that you batch file make no sense.  They just make a bunch of folders.

    It sounds like you are asking how to use conditional logic.  If you do the first step of any tutorial you will learn about conditional execution.

    A simple search finds hundreds of examples: https://www.google.com/?gws_rd=ssl#newwindow=1&q=powershell+conditional+logic

    I recommend a tutorial or a book as a starter.  It will save you a lot of time.


    \_(ツ)_/

    Wednesday, June 29, 2016 1:27 PM
  • We will not convert scripts fro you.  We can answer questions and help with issues.  The forum is not for free consulting.

    No, Sir - this never was my purpose. I can do my work myself.

    Also note that you batch file make no sense.  They just make a bunch of folders.

    As I wrote already before this is only an example!

    My intention was to show that the section :makedir2 in this example is only executed when i add the parameter all to the script.

    I do have some books here and I´m working with them - but I couldn´t find the solution there.

    Now at least I have an catchword....

    Wednesday, June 29, 2016 3:11 PM
  • Here's an example:

    function Example-Function {
    
        Param(
            [switch]$DoAll
        )
    
        If ($DoAll) {
    
            Write-Host 'Do everything here if the switch is passed' -ForegroundColor Red
            Write-Host 'Like this' -ForegroundColor Red
    
        }
    
        Write-Host 'This will happen every time' -ForegroundColor Green
        Write-Host 'This too' -ForegroundColor Green
    
    }
    
    
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\6-29-2016> Example-Function
    This will happen every time
    This too
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\6-29-2016> Example-Function -DoAll
    Do everything here if the switch is passed
    Like this
    This will happen every time
    This too

    https://technet.microsoft.com/en-us/library/hh847829(v=wps.630).aspx


    Wednesday, June 29, 2016 3:31 PM
  • Thank you - now I´ve understood the param-switch.


    • Edited by logg.sar Friday, July 1, 2016 12:56 PM
    • Marked as answer by logg.sar Friday, July 1, 2016 12:56 PM
    Friday, July 1, 2016 12:51 PM