Powershell组合框将变量传递给函数

时间:2016-11-21 01:47:05

标签: powershell variables combobox

下面是我现在写的剧本,虽然它大部分都有效。我的组合框如果语句变量在语句之外不起作用。

我在If语句中使用了写主机并且它可以正常工作,但是在下面的函数中我无法得到变量的值:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  

Add-Type -AssemblyName System.Windows.Forms 
Add-Type -AssemblyName System.Drawing 
$MyForm = New-Object System.Windows.Forms.Form 
$MyForm.Text="New User Creation" 
$MyForm.Size = New-Object System.Drawing.Size(300,600) 


########  Create Button  #########     

    $mCreate = New-Object System.Windows.Forms.Button 
            $mCreate.Text="Create" 
            $mCreate.Top="500" 
            $mCreate.Left="8" 
            $mCreate.Anchor="Left,Top" 
    $mCreate.Size = New-Object System.Drawing.Size(100,23) 
    $MyForm.Controls.Add($mCreate) 




 ########  Location Combo  Box  #########  

    $mlocation = New-Object System.Windows.Forms.Combobox 
            $mlocation.Text="Location" 
            $mlocation.Top="177" 
            $mlocation.Left="105" 
            $mlocation.Anchor="Left,Top" 
    $mlocation.Size = New-Object System.Drawing.Size(125,23) 
    $MyForm.Controls.Add($mlocation) 
    $sites2 = @("AA Select Location","Wayville","Port Melbourne","Darwin","Salisbury")|sort
    $mlocation.DataSource = $sites2
    $MyForm.Controls.Add($mlocation)

# ------------------------------------------------------------------------------- 
#                             Change triggered function
# -------------------------------------------------------------------------------

$mlocation_SelectedIndexChanged=
{
If ($mlocation.text -eq "Wayville") {

        $Street = "Street address" 
        $state="SA"
        $Postcode="5000"
        $Country="Australia"

        Write-Host "$Street"

}
Elseif ($mlocation.text -eq "Darwin") {
        $street= "street address 2"
        $City= "  City"
        $state="NT"
        $Postcode="0888"
        $Country="Australia"

        Write-Host "$state ,$city,$street,$postecode"

}
}
################ MUST CREATE BEFORE ASSIGN ################ 
$mlocation.add_SelectedIndexChanged($mlocation_SelectedIndexChanged)





function pingInfo {


Write-Host $street


$first=$mfirstname.text;
$last=$mlastname.text;
$phone=$telephone.text;
$pass=$Assembly.text;
$Manager=$mManager.text;
$expire=$Expiredate.text;


If ($mpassword.Checked)
{
$length = 10
$specialchar = 0

Add-Type -AssemblyName System.Web
$Assembly = [System.Web.Security.Membership]::GeneratePassword($length,$specialchar)
Write-Host $Assembly
 } 

 ###### The No Email Checkbox will determin if the account is created under Exchange or just AD  ###############

If ($mNoEmail.Checked)

#################### this section is for No Email  ##############

{



                         [System.Windows.Forms.MessageBox]::Show("No Email $street $City $state $Postcode $Country $expire $first $last $phone $Assembly $Manager","My Dialog Box")

      } 

  ######################### This section has Email  ###############

  Else {
                         [System.Windows.Forms.MessageBox]::Show("$street $City $state $Postcode $Country $expire $first $last $phone $Assembly $Manager","My Dialog Box")
  }



                 } #end pingInfo

 ############################################## end functions


   $mCreate.Add_Click({pingInfo})


          $mCancel.Add_Click(
    {    
    $MyForm.Close()
    }
 ) 



       $MyForm.ShowDialog()

0 个答案:

没有答案