Maintain Variable outside of a function statement

Posted on 2011-10-04
Last Modified: 2012-05-12
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.


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

function Return-DropDown {
		$Node1 = $DropDown.SelectedItem
		Write-Host -Fore "Green" "$Node1"
			do {
				#$Node1 = $DropDown.SelectedItem
				#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
						elseif ($b -ne $True) {
												Write-Host -Fore "Yellow" "$Node1 does not have Exchange 2010 Mailbox Server Role installed"
												$input = $True
						elseif ($c -ne $False) {
												Write-Host -Fore "Yellow" "$Node1 is already part of DAG $x"
												$input = $True
						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) {

$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"

$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"

Write-Host -Fore "Green" "$Node1"

Open in new window

Question by:patelbg2001
    LVL 8

    Expert Comment

    Being a newb my self, could it be as a result of you setting the $Node1 Variable within a function?
    LVL 8

    Accepted Solution

    Google variabl scopes. I think you need to set node1 as  $Global:node1 =......

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Set OWA language and time zone in Exchange for individuals, all users or per database.
    Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
    In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
    The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now