Avatar of patelbg2001
patelbg2001

asked on 

Maintain Variable outside of a function statement

I’m looking for some help with my powershell script.
 
I have created a pull-down menu which holds a list servers. Upon selecting a server the script logic runs to validate the server choice.  After this has completed I cannot maintain this selection (variable) outside of the drop down function (Line 79).

I am looking for some help with this, as I would like to use the varibale further down into the script.

Thanks

[array]$DropDownArray1 = get-exchangeserver |? {$_.IsE14OrLater -eq $True} |% {$_.name}

function Return-DropDown {
		$Node1 = $DropDown.SelectedItem
		$Form.Close()
		Write-Host -Fore "Green" "$Node1"
			do {
				#$Node1 = $DropDown.SelectedItem
				#$Form.Close()
				#Write-Host -Fore "Green" "$Node1"
				$p = (get-exchangeserver -identity $Node1).admindisplayversion
				Write-Host -Fore "Green" "$p"
				$q = (get-exchangeserver -identity $Node1).IsMailboxServer
				Write-Host -Fore "Green" "$q"
				$x = (Get-MailboxServer -identity $Node1).DatabaseAvailabilityGroup
				Write-Host -Fore "Green" "$x"
			#
				$a = (get-exchangeserver -identity $Node1).admindisplayversion
				$b = (get-exchangeserver -identity $Node1).IsMailboxServer
				$c = (Get-MailboxServer -identity $Node1).DatabaseAvailabilityGroup
			#
					if ($a -notmatch "14.1") {
										Write-Host -Fore "Yellow" "$Node1 is not an Exchange 2010 Server"
										$input = $true
										Break
										}
						elseif ($b -ne $True) {
												Write-Host -Fore "Yellow" "$Node1 does not have Exchange 2010 Mailbox Server Role installed"
												$input = $True
												Break
											}
						elseif ($c -ne $False) {
												Write-Host -Fore "Yellow" "$Node1 is already part of DAG $x"
												$input = $True
												Break
												}
						else {
							Write-Host -Fore "Green" "$Node1 is not part of DAG"
							$input = $False
						}
					} While ($input)
			}
#
[Void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[Void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$Form = New-Object System.Windows.Forms.Form

$Form.width = 300
$Form.height = 150
$Form.Text = ”DropDown”

$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(100,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)
#
#
ForEach ($ExSrv1 in $DropDownArray1) {
	$DropDown.Items.Add($EXSrv1)
}
#
$Form.Controls.Add($DropDown)

$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,40)
$DropDownLabel.Text = "Exchange 2010 Server"
$Form.Controls.Add($DropDownLabel)

$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,30)
$Button.Text = "Select Server"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)

$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
#
Write-Host -Fore "Green" "$Node1"

Open in new window

PowershellExchange

Avatar of undefined
Last Comment
brittonv
Avatar of brittonv
brittonv
Flag of United States of America image

Being a newb my self, could it be as a result of you setting the $Node1 Variable within a function?
ASKER CERTIFIED SOLUTION
Avatar of brittonv
brittonv
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Exchange
Exchange

Exchange is the server side of a collaborative application product that is part of the Microsoft Server infrastructure. Exchange's major features include email, calendaring, contacts and tasks, support for mobile and web-based access to information, and support for data storage.

213K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo