Execution some Functions in Powershell Script

Ivaylo Stefanov 136 Reputation points
2021-03-26T09:30:11.65+00:00

Hi there,

I need to write a script in which I need to define different functions. In addition, the script must have a menu in which to choose which function to perform. I wrote the following script:

# Main menu, allowing user selection  
    function Show-Menu  
    {  
         param (  
               [string]$Title = 'Choose Which  to Start'  
         )  
         cls  
         Write-Host "================ $Title ================"  
          
      Write-Host "1: Press '1' to Stop LAGOServvices"  
      Write-Host "2: Press '2' to Start LAGOServvices"  
      Write-Host "3: Press '3' to Clean Temp-, Logs-, LocalhostFolders"  
      Write-Host "4: Press '4' to Start to Test LAGOWebsSrvices"  
      Write-Host "5: Press '5' to Restart LAGOServices"  
      Write-Host "6: Press '6' to Update LAGOServices"  
        
      Write-Host "Q: Press 'Q' to quit."  
    }  
      
    #Functions go here  
      
    Function StopLAGOServvices {  
     }  
       
    Function StartLAGOServices {  
    }  
      
    Function CleanTempfolder {  
     }  
      
    Function TestWebservices {  
    }  
      
    Function RestartLAGOServices {  
    }  
    Function UpdateLAGOServices {  
     }  
      
    #Main menu loop  
    do  
    {  
         Show-Menu  
         $input = Read-Host "Please make a selection"  
         switch ($input)  
         {  
               '1' {  
                    cls  
                    StopLAGOServvices  
               } '2' {  
                    cls  
                    StartLAGOServices  
               } '3' {  
                    cls  
                    CleanTempfolder  
        } '4' {  
     cls  
     TestWebservices  
        } '5' {  
     cls  
     RestartLAGOServices  
        } '6' {  
     cls  
     UpdateLAGOServices  
               } 'q' {  
                    return  
               }  
         }  
         pause  
    }  
    until ($input -eq 'q')  

This work and can choose option 1, 2, 3 etc. When a choose 1 is executed first function. I want to defined for example, function 5 which brings all other function togheter and execute all other. Something like this:
Function RestartLAGOServices { Function Stop LAGOServvices + Funcction Start LAGOServvices + Fuction Clean Temp-, Logs-, LocalhostFolders + Function Test LAGOWebsSrvices"}.
Can i use parameter for this purpose? For example:
$parameter = @(stop, start, restart, update)
if ($parameter -like start) { start Function StartLAGOServvices }
elseif ($parameter -like stop ) {stop Function Function StopLAGOServvices }
One more thing:
How define in menu, if i choose nothing it will be automatic continue to execute some script?

Thanks to each of you who will help me!

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2021-03-26T15:10:08.973+00:00

    See if this works:

    # Main menu, allowing user selection
    function Show-Menu {
        param (
            [string]$Title = 'Choose Which  to Start'
        )
        Clear-Host
        Write-Host "================ $Title ================"
        Write-Host "1: Press '1' to Stop LAGOServvices"
        Write-Host "2: Press '2' to Start LAGOServvices"
        Write-Host "3: Press '3' to Clean Temp-, Logs-, LocalhostFolders"
        Write-Host "4: Press '4' to Start to Test LAGOWebsSrvices"
        Write-Host "5: Press '5' to Restart LAGOServices"
        Write-Host "6: Press '6' to Update LAGOServices"
    
        Write-Host "Q: Press 'Q' to quit."
    }
    
    #Functions go here
    Function StopLAGOServvices {
    }
    Function StartLAGOServices {
    }
    Function CleanTempfolder {
    }
    Function TestWebservices {
    }
    Function RestartLAGOServices {
        StopLAGOServvices
        StartLAGOServices
        CleanTempfolder
        TestWebservices
    }
    Function UpdateLAGOServices {
    }
    
    #Main menu loop
    do {
        Show-Menu
        $input = Read-Host "Please make a selection"
        Clear-Host
        switch ($input) {
            '1' {StopLAGOServvices;break}
            '2' {StartLAGOServices; break}
            '3' {CleanTempfolder; break}
            '4' {TestWebservices; break}
            '5' {RestartLAGOServices; break}
            '6' {UpdateLAGOServices; break}
            'q' {break} # do nothing
            default{
                Write-Host "You entered '$input'" -ForegroundColor Red
                Write-Host "Please select one of the choices from the menu." -ForegroundColor Red}
        }
        Pause
    } until ($input -eq 'q')
    
    0 comments No comments

  2. Ivaylo Stefanov 136 Reputation points
    2021-03-26T15:37:03.107+00:00

    Hi,

    Thank you so much RichMatheisen-8856, You help me every time. I testing and writing what happend.

    0 comments No comments

  3. Ivaylo Stefanov 136 Reputation points
    2021-03-29T11:03:57.063+00:00

    Hi RichMatheisen-8856,

    thank you, it's works. Can you help me with something else.
    I need to automate the process, but i have no idea how make this. May be when i use "switch with casesensitve parameter and array".

    I can't just restart the services ... the script always does everything ...

    I want to have a script that I can call with a parameter:
    restart - restart services
    stop - stop services
    start - start services
    clean - clean folders
    test - test webservices
    update - update

    if I don't specify a parameter, restart is used automatically.

    I need a big IF THEN ELSE ELSE around my script ...

    If $ param1 = restart ...
    Elseif $ param1 = stop ...
    Elseif $ param1 = start ..

    I can define function or define scripts 5-6 scripts for each value of process.

    With powershel script with menu and function, user must log in and choose function start oder stop, oder restart etc.
    I'm at a dead end and I don't know how to proceed. I would be really grateful if someone helped me.

    0 comments No comments

  4. Ivaylo Stefanov 136 Reputation points
    2021-03-29T13:39:26.063+00:00

    I try to explain little more clearly.
    I have a Script with diferent function:

     Function StopLAGOServvices {
         }
         Function StartLAGOServices {
         }
         Function CleanTempfolder {
         }
         Function TestWebservices {
         }
         Function RestartLAGOServices {
             StopLAGOServvices
             StartLAGOServices
             CleanTempfolder
             TestWebservices
         }
         Function UpdateLAGOServices {
         }
    

    I want to can define parameter with switch, something this:

    Param(
            [Parameter(Mandatory=$True)]
            [string]$StringParameter
        )
    
        Switch ( $Param )
        {
                      Start { Function StartLAGOServices }
                      Stop { … }
                      Restart { … }
                      Clean { … }
                      Update { … }
        }
    

    I want to start script with task shedule for example 00:30 with option start "function startlago..." oder stop "function stoplago.."

    Another option is to use 5 different scripts and when I select the appropriate parameter to run the corresponding script - restartlago.sp1

     Param(
                [Parameter(Mandatory=$True)]
                [string]$StringParameter
            )
    
            Switch ( $Param )
            {if ($Param -like Start) {
                  start startlago.ps1
                 }
              elseif ($Param -like Stop) {
                   start stoplagp.ps1
                 }
              elseif ($Param -like Clean) {
                    start cleanfolder.ps1
                 }
        }
    

    When i set up the task in shedule task i need to specife the right command. For example: execute the script restartlgo.sp1 with option start at 23:00 and script execute "start startlago.ps1". When i want to setup execute the script restartlgo.sp1 with option stop at 23:00 and script execute "stop stoplago.ps1".

    I hope this explanation is clearer.

    Thank you advance all of yours!