Link to home
Start Free TrialLog in
Avatar of Pete
PeteFlag for United Kingdom of Great Britain and Northern Ireland

asked on

powershell - run scripts depending on what has previously run

I am trying to write some code that runs one of two functions. If function1 is run then something is set that can be referred to throughout the rest of the script and other things run according to that. Can anyone make sense of that?

-----

function menu ()
{
         Write-Host "Press '1' to run FUNCTION1"
         Write-Host "Press '2' to run FUNCTION2"
 
         $input = Read-Host "Please make a selection"
 
         switch ($input)
         {
             '1' {
                     FUNCTION1
                 }
             '2' {
                     FUNCTION2
                 }
          }
}


function FUNCTION1 ()
SOMEWHERE IN HERE SOMETHING IS SET THAT SPECIFIES FUNCTION1 HAS RUN
{
next
}


function FUNCTION2 ()
SOMEWHERE IN HERE SOMETHING IS SET THAT SPECIFIES FUNCTION2 HAS RUN
{
next
}


funtion next ()
{
IF FUNCTION1 HAS RUN THEN DO THIS....
IF FUNCTION2 HAS RUN THEN DO THIS....
}
Avatar of oBdA
oBdA

There are several possibilities to do that; the best depends on what it is exactly you want to achieve.
This, for example, sets some variables with a "Script" scope, so that they're available all over the script:
Function menu() {
	$Script:Function1_Executed = $False
	$Script:Function2_Executed = $False
	Write-Host "Press '1' to run FUNCTION1"
	Write-Host "Press '2' to run FUNCTION2"
 	$input = Read-Host "Please make a selection"
 
	switch ($input) {
		'1' {
			 FUNCTION1
		}
		'2' {
			FUNCTION2
		}
	}
}

Function FUNCTION1() {
	$Script:Function1_Executed = $True
	next
}

Function FUNCTION2() {
	$Script:Function2_Executed = $True
	next
}

Function next() {
	If ($Script:Function1_Executed) {
	}
	If ($Script:Function2_Executed) {
	}
}

Open in new window

Avatar of Pete

ASKER

Thanks, the way your code is displayed is better, do you just reply to the Q with the 'CODE' button?

anyway that looks promising.
- Is the $Script a built in command?
- Why do you need to specify $Script:Function1_Executed and $Script:Function2_Executed as both false in the function menu? Does this mean any function in this script must reference $Script:Function1_Executed and $Script:Function2_Executed and then specify one or both as $false or $true?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial