Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

powershell - run scripts depending on what has previously run

Posted on 2016-11-08
3
41 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 84

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 84

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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…

856 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