强制文本框并输出消息

问题描述 投票:0回答:1

感谢自从我加入以来给予的大力帮助。公平地说,我用谷歌搜索直到手指酸痛,但没有找到我要找的东西,所以我在这里。我是使用 PowerShell 的 Windows 窗体新手,我试图强制使用文本框,但没有成功。此外,我希望如果忽略某种消息,则会提醒用户填写该字段。我的所有搜索都返回了 C# 或 html 或其他语言,或使用 Sapien 的 PowerShell Studio。函数中的其他所有内容都按预期工作,只需将文本框设为强制即可。无论如何,这是我的代码:

#region Prereqs
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
#end region Prereqs

Add-type -AssemblyName system.windows.forms
#define object variables
$formobject = [System.Windows.forms.form] #this sets the variable so no need to keep tying system.windows.forms.form
$lblobjpuid = [System.Windows.Forms.Label]
$TextobjOutput = [System.Windows.Forms.textbox]
$textboxobjInst = [System.Windows.Forms.textbox]
$TxtboxObjPUID = [System.Windows.Forms.TextBox]
$TxtboxObjPath = [System.Windows.Forms.TextBox]
$btnobjTrellix = [System.Windows.Forms.Button]
$btnobjMECM = [System.Windows.Forms.Button]
$btnobjGPO= [System.Windows.Forms.Button]
$btnobjDisableFW = [System.Windows.Forms.Button]
$btnobjVerifyTrellix = [System.Windows.Forms.Button]
$btnobjShowNIC = [System.Windows.Forms.Button]
$btnobjAppVerify = [System.Windows.Forms.Button]
$btnobjclear = [System.Windows.Forms.Button]
$btnobjExit = [System.Windows.Forms.Button]


#build main form
$LSForm=New-object $formobject
$LSForm.clientsize= '700,450' #set the height and width
$LSForm.text ='Post Lift & Shift Actions'
$LSForm.backcolor='white'
$DefaultFont='Verdana,10'
$LSForm.Font=$DefaultFont

#build PUID label object
$lblPUID=new-object $lblobjpuid
$lblPUID.Location=New-Object system.drawing.point(10,10)
$lblPUID.Text="Enter your PUID"
$lblPUID.AutoSize=$true

<#build Output label object
$LabelOutPut=new-object $lblobjOutput
$LabelOutPut.Location=New-Object system.drawing.point(10,200)
$LabelOutPut.AutoSize=$true
$LabelOutPut.MaximumSize.Width='100'
#>
#build output textbox object
$TxtBoxOutput=New-object $TextobjOutput
$TxtBoxOutput.Multiline=$true
$TxtBoxOutput.WordWrap=$true
$TxtBoxOutput.ReadOnly=$true
$TxtBoxOutput.Autosize=$true
$TxtBoxOutput.Size=[drawing.size]::new(250,200)
$TxtBoxOutput.text=''
$TxtBoxOutput.Location=New-Object System.Drawing.Point(10,200)
$TxtBoxOutput.BackColor="White"
$TxtBoxOutput.font = "verdana,10"


#build PUID textbox object
$PUIDTextbox=new-object $TxtboxObjPUID
$PUIDTextbox.Location=new-object System.Drawing.Point (130,10)

#build Trellix button object
$BTNTrellix=new-object $btnobjTrellix
$BTNTrellix.Text='Install Trellix for BPUS'
$BTNTrellix.AutoSize=$true
$BTNTrellix.Location=New-Object system.drawing.point(10,120)
$BTNTrellix.ForeColor='black'
$BTNTrellix.BackColor='#45edea'
$BTNTrellix.font=$DefaultFont

#build MECM install button object
$BTNMECM=new-object $btnobjMECM
$BTNMECM.Text='Install BPUS MECM Agent'
$BTNMECM.AutoSize=$true
$BTNMECM.Location=New-Object system.drawing.point(10,80)
$BTNMECM.ForeColor='black'
$BTNMECM.BackColor='#45edea'
$BTNMECM.Font=$DefaultFont

#build query GPO button object
$BTNGPO=new-object $btnobjGPO
$BTNGPO.Text='Pull GPO Info'
$BTNGPO.AutoSize=$true
$BTNGPO.Location=New-Object system.drawing.point(205,80)
$BTNGPO.ForeColor='black'
$BTNGPO.BackColor='#45edea'
$BTNGPO.Font=$DefaultFont

#build verify Apps button object
$BTNVERIFYAPP=new-object $btnobjAppVerify
$BTNVERIFYAPP.Text='Verify Legacy Apps Removed'
$BTNVERIFYAPP.AutoSize=$true
$BTNVERIFYAPP.Location=New-Object system.drawing.point(10,40)
$BTNVERIFYAPP.ForeColor='white'
$BTNVERIFYAPP.BackColor='RED'
$BTNVERIFYAPP.Font = New-Object System.Drawing.Font("Verdana",10,[System.Drawing.FontStyle]::Bold) 

#build verify Show NICs button object
$BTNSHOWNIC=new-object $btnobjShowNIC
$BTNSHOWNIC.Text='Show NICs'
$BTNSHOWNIC.AutoSize=$true
$BTNSHOWNIC.Location=New-Object system.drawing.point(185,120)
$BTNSHOWNIC.ForeColor='black'
$BTNSHOWNIC.BackColor='#45edea'
$BTNSHOWNIC.Font=$DefaultFont

#build verify Disable Firewall button object
$BTNDISFW=new-object $btnobjDisableFW
$BTNDISFW.Text='Disable/Show Firewall'
$BTNDISFW.AutoSize=$true
$BTNDISFW.Location=New-Object system.drawing.point(10,160)
$BTNDISFW.ForeColor='black'
$BTNDISFW.BackColor='#45edea'
$BTNDISFW.Font=$DefaultFont


#build verify Trellix button object
$BTNVERIFYTRELLIX=new-object $btnobjVerifyTrellix
$BTNVERIFYTRELLIX.Text='Verify Trellix Agent'
$BTNVERIFYTRELLIX.AutoSize=$true
$BTNVERIFYTRELLIX.Location=New-Object system.drawing.point(185,160)
$BTNVERIFYTRELLIX.ForeColor='black'
$BTNVERIFYTRELLIX.BackColor='#45edea'
$BTNVERIFYTRELLIX.Font=$DefaultFont



#build clear all button object
$Btnclear=new-object $btnobjclear
$Btnclear.Text='Clear all'
$Btnclear.AutoSize=$true
$Btnclear.Location=New-Object system.drawing.point(300,400)
$Btnclear.ForeColor='black'
$Btnclear.BackColor='Yellow'
$Btnclear.Font=$DefaultFont
$BTNclear.Font = New-Object System.Drawing.Font("Verdana",10,[System.Drawing.FontStyle]::Bold) 

#build exit button object
$Btnx=new-object $btnobjExit
$Btnx.Text='E&xit'
$Btnx.AutoSize=$true
$Btnx.Location=New-Object system.drawing.point(400,400)
$Btnx.ForeColor='black'
$Btnx.BackColor='Green'
$btnx.Font='verdana,10'
$Btnx.Font = New-Object System.Drawing.Font("Verdana",10,[System.Drawing.FontStyle]::Bold) 

#build instruction textbox object
$Textboxinst=New-object $textboxobjInst
$Textboxinst.Multiline=$true
$Textboxinst.WordWrap=$true
$Textboxinst.ReadOnly=$true
$Textboxinst.Autosize=$true
$Textboxinst.Size=[drawing.size]::new(300,350)
$Textboxinst.text="This Post Lift & Shift Actions applet will allow you to perform various tasks as per the button names. Press click the Verify Apps button FIRST to confirm ALL legacy apps are removed. The Show NIC button shows the status of the NIC. Click Verify Trellix Agent button and make sure the 'UserProperty 2' field matches the zone. The Disable/Show Firewall button will disable the firewall if it is enabled, then show the status, make sure it shows 'False'. The GPO button displays the policies in a gridview, be patient while it loads. The Trellix and MECM buttons installs for BPUS ONLY. DO NOT USE THOSE FOR ANY OTHER ZONES!"
$Textboxinst.Location=New-Object System.Drawing.Point(350,10)
$Textboxinst.BackColor="White"
$Textboxinst.font = "verdana,10"



#build functions for the buttons to perform
Function Verify_Trellix{

 $AgentStatus = CMD /C "C:\Program Files\McAfee\Agent\cmdagent.exe" "-x" | Select-String -Pattern 'Userproperty2:'
 #$TxtBoxOutput.Text=$AgentStatus
 
$TxtBoxOutput.Text = $AgentStatus -join "`r`n"  # Join the lines with carriage return and newline
 }

Function Show_NICs{
#Get-NetIPAddress |Out-GridView
$NIC_Status = Get-NetAdapter * | Select-Object -Property "Name", "Status" | ForEach-Object {
    $_.Name + ": " + $_.Status
}
$TxtBoxOutput.Text = $NIC_Status -join "`r`n"  # Join the lines with carriage return and newline
}

#Show GPO Information
Function Show_GPO{
gpresult /scope computer /z |Out-GridView |Format-Table -AutoSize
}
#Disable and show firewall status
Function FireWall {
$FWStatus = (Get-NetFirewallProfile).Enabled 
if($FWStatus -eq "True"){
Set-NetFirewallProfile -Profile Domain,Private,Public -Enabled False -Verbose 
}
Get-NetFirewallProfile | Select-Object -Property "Name", "Enabled" | ForEach-Object {
    $_.Name + ": " + $_.Enabled
}
$TxtBoxOutput.Text=$FWStatus -join "`r`n" 
}
#Install BPUS MECM Agent
Function Install_MECM {
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
start-process -FilePath "$ScriptPath\Source_Files\Client\ccmsetup.exe" -ArgumentList 'SMSSITECODE=EAM','SMSMP=HTTPS://BPUS001INVP003.BP-US.LOCAL','DNSSUFFIX=bp-us.local', 'SMSCACHESIZE=10', 'SMSCACHEFLAGS=PERCENTDISKSPACE', '/NOSERVICE','/UsePKICert','/NoCRLCheck','/source:$ScriptPath\Source_Files\Client'
}

Function Verify_Apps{
Param(
    [Parameter(Mandatory=$true)]$PUID
    )
$PUID=$PUIDTextbox.Text
$RegKey = 'Lift&Shift'
$registryPath = "HKLM:\SOFTWARE\Eurofins\Lift&Shift";
If ( !(Test-Path $registryPath) ) { New-Item -Path $registryPath -Force; };
New-ItemProperty -Path $registryPath -Name  $RegKey -Value "Lift&shift performed by:$PUID" -PropertyType String –Force;
get-package | Out-GridView
}

#Install Trellix
Function Install_Trellix {
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
& $scriptPath\Source_Files\Trellix\Install_ALL.ps1
}

#clear all text fields
Function ClearTxtBoxes {
$PUIDTextbox.Text = ""
}



##Assign functions to the button
$BTNVERIFYTRELLIX.Add_Click({Verify_Trellix})
$Btnclear.Add_Click({ClearTxtBoxes})
$BTNMECM.Add_Click({Install_MECM})
$Btnx.Add_Click({
$LSForm.FormClosing;$LSForm.Close()
})
$BTNSHOWNIC.Add_Click({Show_NICs})
$BTNGPO.Add_Click({Show_GPO})
$BTNDISFW.Add_Click({FireWall})
$BTNVERIFYAPP.Add_Click({Verify_Apps})
$BTNTrellix.Add_Click({Install_Trellix})






#create an array to accept all the controls you want to appear on the form
#$NICForm.Controls.AddRange(@($lbltitle, $BTNTrellix, $lbltitle1, $lbltitle2, $lbltitle3, $TxtboxOldName, $TxtboxNewName, $TxtboxIPAdd, $BtnRenameNIC, $BtnDisable, $BtnIPAdd, $btnclear, $BTNMECM, $Textboxinst, $BTNx))
$LSForm.Controls.AddRange(@($TxtBoxOutput,$BTNTrellix, $btnclear, $lblPUID, $BTNMECM, $Textboxinst, $BTNx, $BTNMECM, $PUIDTextbox, $BTNGPO, $BTNVERIFYAPP, $BTNSHOWNIC, $BTNDISFW, $BTNVERIFYTRELLIX))

$LSForm.showdialog() #displays the form
$LSForm.dispose() #cleans up form

我添加参数只是为了使该框成为强制性的,但它所做的只是提示并等待控制台中的输入。 reg 键确实会根据文本框的输入进行填充,因此可以正常工作。该功能被分配给按钮单击。我认为通过添加参数,如果他们跳过文本框并单击按钮,至少会出错。但同样,发生的只是在控制台中等待输入,然后一旦我输入内容并按 Enter 键,它就允许输入表单。预先感谢

powershell winforms
1个回答
0
投票

为 PowerShell 函数设置参数

Mandatory
对于 WinForm 应用程序来说除了挂起它之外没有任何影响,因为调用该函数将等待用户输入。

让用户向文本框提供输入的方式有多种,但没有“强制”的概念。例如,其中一种方法可以是禁用“必须在文本框中输入”的所有按钮,并且仅在填充所述文本框后才启用它们。为此,您可以使用 .TextChanged 事件

,在事件处理程序中,逻辑是,如果 
.Text 为空或空白区域,则禁用所有应被视为强制的按钮,否则启用它们。
逻辑的简单示例:

# this code goes right after `$LSForm.Controls.AddRange...` # add all buttons considered mandatory here $mandatoryButtons = @( $BTNTrellix $BTNMECM $BTNx $BTNMECM # and so on ) # all mandatory buttons should start in disabled state because the # PUID textbox is empty when the form loads $mandatoryButtons | ForEach-Object { $_.Enabled = $false } $PUIDTextbox.Add_TextChanged({ if ([string]::IsNullOrWhiteSpace($this.Text)) { $mandatoryButtons | ForEach-Object { $_.Enabled = $false } return } $mandatoryButtons | ForEach-Object { $_.Enabled = $true } }) $LSForm.ShowDialog()

© www.soinside.com 2019 - 2024. All rights reserved.