Solved

powershell - run scripts depending on what has previously run

Posted on 2016-11-08
3
17 Views
Last Modified: 2016-11-08
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....
}
0
Comment
Question by:Pete
  • 2
3 Comments
 
LVL 83

Expert Comment

by:oBdA
ID: 41878792
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

1
 
LVL 1

Author Comment

by:Pete
ID: 41878828
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
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41878844
"$Script:Variable" (as well as some others, see "Get-Help about_scopes") defines a variable's scope. Without the $Script scope, the variable would only be set in the function's scope, and then forgotten once the function is done.
$Script:Function1_Executed and $Script:Function2_Executed are set to $False in the Menu function so that they're properly initialized before Next is called.
If you want to keep their state over several runs of the Menu function, you should initialize them in the main part of the script. It just seems like you want to check from which function "Next" was called.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
Synchronize a new Active Directory domain with an existing Office 365 tenant
In this Micro Tutorial viewers will learn how they can get their files copied out from their unbootable system without need to use recovery services. As an example non-bootable Windows 2012R2 installation is used which has boot problems.
In this Micro Tutorial viewers will learn how to restore their server from Bare Metal Backup image created with Windows Server Backup feature. As an example Windows 2012R2 is used.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now