question

IvayloStefanov avatar image
0 Votes"
IvayloStefanov asked RichMatheisen-8856 commented

Execution some Functions in Powershell Script

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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')
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IvayloStefanov avatar image
0 Votes"
IvayloStefanov answered

Hi,

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IvayloStefanov avatar image
0 Votes"
IvayloStefanov answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IvayloStefanov avatar image
0 Votes"
IvayloStefanov answered RichMatheisen-8856 commented

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!





· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Use the "switch" statement.

If you have other questions you should create a new question. Mark as the answer post that solved your problem -- that'll help anyone that's looking for the same information.

0 Votes 0 ·