Do Variables Persist

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 10 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Lery154
Posts: 48
Last visit: Fri Feb 20, 2015 12:36 pm

Do Variables Persist

Post by Lery154 »

Please take the following example. In the PowerShell ISE I run the following:
PowerShell Code
Double-click the code block to select all.
$ComputerName = '192.168.1.51'
$vercon = Test-Connection -ComputerName $ComputerName -Quiet -Count 1
if ($vercon -eq $false) {[System.Windows.Forms.MessageBox]::Show("$computername is not responding to a ping!  Please verify machine is online.", "Verify Connection",0, 'Stop')}
if ($ComputerName -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}") {
		if ($credential -eq $null) 
		{$credential = Get-Credential -Message "Please enter credentials to access $computername.  You can enter your credentials assuming you're an administrator on $computername."}
		do something
	}else {
		do something else
		}
When that is run as a PowerShell script or inside the PowerShell ISE the credential variable persists. Meaning that the first time I run it, I'm prompted for credentials. The second time the credential variable is not null, therefore it does not prompt me and moves into the else block.

When I put the same code into a form script, I'm prompted for credentials every time I click my button. It's acting like the credential variable is null every time. Is this normal?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Do Variables Persist

Post by jvierra »

All variables are local to the function that hey are defined in.
Use $global:credential
User avatar
Lery154
Posts: 48
Last visit: Fri Feb 20, 2015 12:36 pm

Re: Do Variables Persist

Post by Lery154 »

jvierra wrote:All variables are local to the function that hey are defined in.
Use $global:credential
Use it where/how? I'm not having any luck. Use my sample script if you could?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Do Variables Persist

Post by jvierra »

Everywhere. It is declaring and referencing a global variable that persists in all scopes.
User avatar
Lery154
Posts: 48
Last visit: Fri Feb 20, 2015 12:36 pm

Re: Do Variables Persist

Post by Lery154 »

jvierra wrote:Everywhere. It is declaring and referencing a global variable that persists in all scopes.
I'm not following. I need an example. If you copy and paste the following into PowerShell ISE it works. It prompts you for your credentials once and then when run again it does not prompt you.
PowerShell Code
Double-click the code block to select all.
if ($global:credential -eq $null){
		Get-Credential}
	else {Get-Service -Name bits}
Take that same code and put it into a button click event. Click your button. It prompts you for credentials. Click the button again. It continues to prompt you. It should not do this.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Do Variables Persist

Post by jvierra »

Follow the exact instructions. Every reference to $credential needs to be global not just that one.

THis is closer to what you are trying to do. YOu are getting caught up in an unnecessary "if" storm. PowerShell was designed to simplify and eliminate this cascade of redundant tests.
PowerShell Code
Double-click the code block to select all.
$button1_Click={
	$ComputerName = 'omega'
	$message='Enter your credentails'
	If(Test-Connection -ComputerName $ComputerName -Quiet -Count 1){
		if(-not $global:credential){
			$global:credential=Get-Credential -Message $message
		}else{
			[System.Windows.Forms.MessageBox]::Show('Credentials already saved')
		}
	}else{
		[System.Windows.Forms.MessageBox]::Show('COMPUTER NOT RESPONDING')
	}
}
Last edited by jvierra on Tue Jul 23, 2013 7:36 am, edited 1 time in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Do Variables Persist

Post by jvierra »

I would add another design critique. Add the test for the computer to the validation code for the box where you enter the computer name.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Do Variables Persist

Post by jvierra »

Sorry about the edits. This editor is a bit on the aggravating side any time it is necessary to edit something. I missed the unfortunate merge of old and new posts.

Be sure you have the latest version posted
User avatar
Lery154
Posts: 48
Last visit: Fri Feb 20, 2015 12:36 pm

Re: Do Variables Persist

Post by Lery154 »

Thanks I got it all. This now works perfectly, thank you.

The only corner that I've now backed myself into is if the end user enters the credentials incorrectly. The variable has data so it does not prompt again. I'll have to figure out some error handling.
This topic is 10 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked