Powershell组合框将变量传递给函数
下面是我一直在写的脚本,虽然它的大部分工作。如果语句变量未在语句外运行,则显示我的组合框 我在If语句中使用了写主机,它可以工作,但在下面的函数中,我无法获得变量的值:Powershell组合框将变量传递给函数,powershell,variables,combobox,Powershell,Variables,Combobox,下面是我一直在写的脚本,虽然它的大部分工作。如果语句变量未在语句外运行,则显示我的组合框 我在If语句中使用了写主机,它可以工作,但在下面的函数中,我无法获得变量的值: [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") Add-T
[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()
?@GlenMclean:如果JosefZ的指针解决了你的问题,我鼓励你发布自己的答案解释这个解决方案,以便未来的读者可以从中受益。我还鼓励您将问题简化为一个。解决方案非常简单,
$mlocation\u SelectedIndexChanged={If($mlocation.text-eq“Wayville”){$Global:Street=“Street Address”$Global:City=“Wayville”}Elseif($mlocation.text-eq“Darwin”){$Global:Street=“Street Address”
the$Global:允许在if约束之外使用变量statement@GlenMclean请遵循MKLMMONT0的鼓励,发表你自己的答案而不是评论。然后,考虑接受最有用的答案。解释为什么这很重要。(但是你必须等待48个小时来接受你自己的答案)。此外,考虑查看<代码>开关>代码>命令,而不是IF/EL,如果您当前正在执行,因为您将有一个选项列表来考虑“.@ GlenMclean的替代方案”。@:如果JosefZ的指针解决了您的问题,我鼓励您发布自己的答案来解释解决方案,以便将来读者可以从中受益。我也鼓励你把你的问题简化成一个问题。解决方案非常简单,$mlocation\u SelectedIndexChanged={If($mlocation.text-eq“Wayville”){$Global:Street=“Street Address”$Global:City=“Wayville”}Elseif($mlocation.text-eq“Darwin”){$Global:Street=“Street Address”
the$Global:允许在if约束之外使用变量statement@GlenMclean请遵循MKLMMONT0的鼓励,发表你自己的答案而不是评论。然后,考虑接受最有用的答案。解释为什么这很重要。(但是你必须等待48个小时来接受你自己的答案)。此外,考虑查看