Solved

powershell - run scripts depending on what has previously run

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

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.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In this Micro Tutorial viewers will learn how to use Windows Server Backup to create full image of their system. Tutorial shows how to install Windows Server Backup Feature on Windows 2012R2 and how to configure scheduled Bare Metal Recovery backup.…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

726 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