[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

powershell - run scripts depending on what has previously run

Posted on 2016-11-08
3
Medium Priority
?
69 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 85

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 85

Accepted Solution

by:
oBdA earned 2000 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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…

873 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