Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

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

0
patelbg2001
Asked:
patelbg2001
  • 2
1 Solution
 
brittonvCommented:
Being a newb my self, could it be as a result of you setting the $Node1 Variable within a function?
0
 
brittonvCommented:
Google variabl scopes. I think you need to set node1 as  $Global:node1 =......
0

Featured Post

Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now