选项框关闭后不保存选项

时间:2014-09-08 22:34:49

标签: winforms powershell

友好的你好!

我试图获得这个"选项"形式工作。目前,设置不会坚持,并且他们不会转移到主要表单。

我创建了一个启动新表单的函数,该表单将包含所有不同的选项。我很快发现你必须以某种方式保存选项。我试图以不同的方式做到这一点:

1。)将复选框更改为全局变量

2。)分配" $ Global的值:Checkbox_BasicInfo.checked = $ true"存储在函数外部的全局变量。

两者均未成功。有人有主意吗?谢谢!

汇总代码

## VARIABLES

$Global:BasicOptionState = $false

###################################
# GUI                             #
###################################

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  #loading the necessary


################################# MAIN Window
$Form = New-Object System.Windows.Forms.Form  #creating hte form (this will be the "Primary" window)
$Form.Size = New-Object System.Drawing.Size(590,315)  #the size in px of the window length, height
$Form.Text = "Sidekick v" + $version

################################# OPTIONS button
$OptionsButton = New-Object System.Windows.Forms.Button
$OptionsButton.Location = New-Object System.Drawing.Size (450,14)
$OptionsButton.Size = New-Object System.Drawing.Size (110,20)
$OptionsButton.Text = "Options"
$OptionsButton.Add_Click({Options})
$Form.Controls.Add($OptionsButton)

function Options {
$OptionsForm = New-Object System.Windows.Forms.Form  #creating the form (this will be the "Primary" window)
$OptionsForm.Size = New-Object System.Drawing.Size(400,400)  #the size in px of the window length, height
$OptionsForm.Text = "Options"

################################# Computer Information -- Group Box
$groupBox1 = New-Object System.Windows.Forms.GroupBox #create the group box
$groupBox1.Location = New-Object System.Drawing.Size(10,5) #location of the group box (px) in relation to the primary window's edges (length, height)
$groupBox1.size = New-Object System.Drawing.Size(350,200) #the size in px of the group box (length, height)
$groupBox1.text = "Computer Information"
$groupBox1.Anchor = [System.Windows.Forms.AnchorStyles]::Right -bor [System.Windows.Forms.AnchorStyles]::Top -bor
$OptionsForm.Controls.Add($groupBox1) #activate the group box

################################# Computer Information -- Choices
$Global:Checkbox_BasicInfo = New-Object System.Windows.Forms.CheckBox #create the radio button
$Global:Checkbox_BasicInfo.Location = new-object System.Drawing.Point(10,20) #location of the radio button(px) in relation to the group box's edges (length, height)
$Global:Checkbox_BasicInfo.size = New-Object System.Drawing.Size(150,20) #the size in px of the radio button (length, height)
$Global:Checkbox_BasicInfo.Checked = $true #is checked by default
$Global:Checkbox_BasicInfo.Text = "Show Basic Information" #labeling the radio button
$groupBox1.Controls.Add($Global:Checkbox_BasicInfo) #activate the inside the group box

################################# OPTIONS button
$ApplyButton = New-Object System.Windows.Forms.Button
$ApplyButton.Location = New-Object System.Drawing.Size (250,325)
$ApplyButton.Size = New-Object System.Drawing.Size (110,20)
$ApplyButton.Text = "Apply"
$ApplyButton.Add_Click({ApplyFunction})
$OptionsForm.Controls.Add($ApplyButton)

$OptionsForm.Add_Shown({$OptionsForm.Activate()})
[void] $OptionsForm.ShowDialog()
}

## APPLY OPTIONS function
function ApplyFunction {

if ($Global:Checkbox_BasicInfo.checked = $true) {
$Global:BasicOptionState = $true
}

if ($Global:Checkbox_BasicInfo.unchecked = $true) {
$Global:BasicOptionState = $false
}

}

$Form.Controls.Add($outputBox) 
$Form.Add_Shown({$Form.Activate()})

[void] $Form.ShowDialog()  

完整代码(更多背景信息)

import-module activedirectory
## PC or User?
function MonkeyMonkey {
$input = $InputBox.text
$pullobject = get-adobject -filter {(name -like $input) -and ((Objectclass -eq "user") -or (objectclass -eq "computer"))}
if ($pullobject.objectclass -eq "computer") {
pingtest
}
if ($pullobject.objectclass -eq "user") {
$34 = $input
$outputBox.text = UserInformation
}
}

###################################
# USERS                           #
###################################

function UserInformation {
$userdata = Get-ADUser $34 -properties *
$groupmembership = Get-ADPrincipalGroupMembership $34 | Select-Object name
$date = Get-Date
"User Information for:  " + $userdata.name
"`nCaptured on " + $date
"`n------------------------------"
"`nName: " + $userdata.surname + ", " + $userdata.GivenName
"`nFacility: " + $userdata.Company
"`nDepartment: " + $userdata.Department
"`nJob Title: " + $userdata.Title
"`nEmail: " + $userdata.mail
"`n------------------------------"
"`nAccount Enabled: " + $userdata.enabled
"`nLocked Out: " + $userdata.lockedout
"`nLast Logon Date: " + $userdata.lastlogondate
"`nLast Bad Password Attempt: " + $userdata.LastBadPasswordAttempt
"`nPassword Expired: " + $userdata.PasswordExpired
"`nPassword Last Set: " + $userdata.PasswordLastSet
"`nAccount Expiration Date: " + $userdata.AccountExpirationDate
"`nGroup Membership: "
foreach ($group in $groupmembership){
"`n    "+$group.name
}
}

###################################
# COMPUTERS                       #
###################################

## Ping Test to prevent errors when no PC is found
function PingTest {

$outputBox.text="Testing PC Connection.  Please wait..."
$pingresults = Test-Connection $InputBox.text -count 2

if($pingresults.Count -gt 0)
{
GetComputerInfo
}
Else
{
$outputBox.text="**ERROR!**

The PC may either be off the network or the PC hostname you've entered is incorrect.  Please try again."
}

}

## After successful ping, grabs PC info from ComputerInfoScript function and sents to the output box
function GetComputerInfo {
$outputBox.text = "Ping test successful!"
Wait-Event -Timeout .5
$computerhostname = $InputBox.text; #PC input
## CIM setup ##
$outputBox.text = "Connecting CIM Session..."
$cimopt = New-CimSessionOption -Protocol DCOM
$cimsession = New-CimSession -ComputerName $computerhostname -SessionOption $cimopt

## Commands ##
$outputBox.text = "Retrieving Date Information..."
$date = Get-Date

if ($GlobalCheckbox_BasicInfo.Checked -eq $true) {

$outputBox.text = "Retrieving System Information..."
$computerSystem = get-ciminstance -cimsession $cimsession -ClassName Win32_ComputerSystem

$outputBox.text = "Retrieving BIOS Information..."
$computerBIOS = get-ciminstance -cimsession $cimsession -ClassName Win32_BIOS

$outputBox.text = "Retrieving OS Information..."
$computerOS = get-ciminstance -cimsession $cimsession -ClassName Win32_OperatingSystem

$outputBox.text = "Retrieving Processor Information..."
$computerCPU = get-ciminstance -cimsession $cimsession -ClassName Win32_Processor

$outputBox.text = "Retrieving HardDrive Information..."
$computerHDD = get-ciminstance -cimsession $cimsession -ClassName Win32_LogicalDisk -Filter drivetype=3

$outputBox.text = "Retrieving Internet Explorer Version Information..."
$computerIEversion = Get-CimInstance -ClassName CIM_DataFile -Filter "Name = 'C:\\Program Files\\Internet Explorer\\iexplore.exe'" | select -ExpandProperty Version

$outputBox.text = "Retrieving Bitlocker Info..."
$computerBitLock = Get-Service "BDESVC" -ComputerName $computerhostname

$outputBox.text = "Cleaning up..."
Wait-Event -Timeout 1
Remove-CimSession $cimsession
}

if ($Checkbox_Errors.Checked -eq $true) {
$outputBox.text = "Retrieving Event Logs..."
$computerEventErrorsApp = Get-EventLog -ComputerName $computerhostname -LogName Application -EntryType Error -Newest 5 | select timegenerated,source,message 
$computerEventErrorsSys = Get-EventLog -ComputerName $computerhostname -LogName System -EntryType Error -Newest 5 | select timegenerated,source,message
}

$computerinfo = ComputerInfoScript | out-string;
$outputBox.text=$computerinfo #sends gathered computer information to output box
if ($Checkbox_AutoCopy.checked -eq $True) { # auto-copy
$outputBox.text | clip
}
}

## Commands to grab PC information
function ComputerInfoScript {

"System Information for: " + $computerSystem.Name
"Captured on " + $date
"-------------------------------------"
if ($BasicComputerInformation -eq $true) {
"Distinguished Name: " + $computerOu.DistinguishedName
"Manufacturer: " + $computerSystem.Manufacturer
"Model: " + $computerSystem.Model
"Serial Number: " + $computerBIOS.SerialNumber
"CPU: " + $computerCPU.Name
"HDD Capacity: "  + "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
"HDD Space: " + "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
"RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
"Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
"Internet Explorer Version: " + $computerIEversion
"User logged In: " + $computerSystem.UserName
"Last Reboot: " + $computerOS.LastBootUpTime
"Bitlocker Drive Encryption Service:  " + $computerBitLock.status
#COMMENTEDOUT#$computerBitLock2

}

################################# Errors
if ($Checkbox_Errors.Checked -eq $true) {
""
"-------------------------------------"
"Application Event Errors:"
"-------------------------------------"
foreach ($orca in $computerEventErrorsApp) {

"Date:  " + $orca.timegenerated
"Source:  " + $orca.source
"Message:  " 
"    " + $orca.message
""
""
}
""
"-------------------------------------"
"System Event Errors:"
"-------------------------------------"
foreach ($orca in $computerEventErrorsSys) {
"Date:  " + $orca.timegenerated
"Source:  " + $orca.source
"Message:  " 
"    " + $orca.message
""
""
}
}

}
#### removed bracket; keeping note of location ####



## Clipboard Function
function ClipClip {

$outputBox.text | clip

}

## OPTIONS
$Global:BasicOptionState = $true

## APPLY BUTTON FUNCTION


###################################
# GUI                             #
###################################

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  #loading the necessary


################################# Main Window
$Form = New-Object System.Windows.Forms.Form  #creating hte form (this will be the "Primary" window)
$Form.Size = New-Object System.Drawing.Size(590,315)  #the size in px of the window length, height
$Form.Text = "Sidekick v" + $version


################################# INPUT; PC hostname/IP entry
$InputBox = New-Object System.Windows.Forms.TextBox 
$InputBox.Location = New-Object System.Drawing.Size(5,250)
$InputBox.Size = New-Object System.Drawing.Size(485,20)
$InputBox.text = "Type PC Hostname or 3-4 ID"
$InputBox.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor  [System.Windows.Forms.AnchorStyles]::Left 
$Form.Controls.Add($InputBox) 

################################# Information Output Box
$outputBox = New-Object System.Windows.Forms.RichTextBox #creating the text box
$outputBox.Location = New-Object System.Drawing.Size(5,40)
$outputBox.Size = New-Object System.Drawing.Size(565,200)
$outputBox.MultiLine = $True
$outputBox.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor 
                [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left 
$outputBox.ScrollBars = "Vertical"

################################# COPY button
$CopyButton = New-Object System.Windows.Forms.Button
$CopyButton.Location = New-Object System.Drawing.Size (5,14)
$CopyButton.Size = New-Object System.Drawing.Size (110,20)
$CopyButton.Text = "Copy"
$CopyButton.Add_Click({ClipClip})
$Form.Controls.Add($CopyButton)

################################# OPTIONS button
$OptionsButton = New-Object System.Windows.Forms.Button
$OptionsButton.Location = New-Object System.Drawing.Size (450,14)
$OptionsButton.Size = New-Object System.Drawing.Size (110,20)
$OptionsButton.Text = "Options"
$OptionsButton.Add_Click({Options})
$Form.Controls.Add($OptionsButton)

function Options {
$OptionsForm = New-Object System.Windows.Forms.Form  #creating the form (this will be the "Primary" window)
$OptionsForm.Size = New-Object System.Drawing.Size(400,400)  #the size in px of the window length, height
$OptionsForm.Text = "Sidekick Options"

################################# Group Box
$groupBox1 = New-Object System.Windows.Forms.GroupBox #create the group box
$groupBox1.Location = New-Object System.Drawing.Size(10,5) #location of the group box (px) in relation to the primary window's edges (length, height)
$groupBox1.size = New-Object System.Drawing.Size(350,200) #the size in px of the group box (length, height)
$groupBox1.text = "Computer Information"
$groupBox1.Anchor = [System.Windows.Forms.AnchorStyles]::Right -bor [System.Windows.Forms.AnchorStyles]::Top -bor
$OptionsForm.Controls.Add($groupBox1) #activate the group box

################################# Add'l Information Checkboxes
$Global:Checkbox_BasicInfo = New-Object System.Windows.Forms.CheckBox #create the radio button
$Global:Checkbox_BasicInfo.Location = new-object System.Drawing.Point(10,20) #location of the radio button(px) in relation to the group box's edges (length, height)
$Global:Checkbox_BasicInfo.size = New-Object System.Drawing.Size(150,20) #the size in px of the radio button (length, height)
$Global:Checkbox_BasicInfo.Checked = $true #is checked by default
$Global:Checkbox_BasicInfo.Text = "Show Basic Information" #labeling the radio button
$groupBox1.Controls.Add($Global:Checkbox_BasicInfo) #activate the inside the group box

$Checkbox_Errors = New-Object System.Windows.Forms.CheckBox #create the radio button
$Checkbox_Errors.Location = new-object System.Drawing.Point(10,40) #location of the radio button(px) in relation to the group box's edges (length, height)
$Checkbox_Errors.size = New-Object System.Drawing.Size(86,40) #the size in px of the radio button (length, height)
$Checkbox_Errors.Checked = $false #is checked by default
$Checkbox_Errors.Text = "Event Errors" #labeling the radio button
$groupBox1.Controls.Add($Checkbox_Errors) #activate the inside the group box


$ErrorWarning1 = New-Object System.Windows.Forms.Label
$ErrorWarning1.Text = "WARNING: May take 3-5 Minutes"
$ErrorWarning1.ForeColor = "Red"
$ErrorWarning1.AutoSize = $True
$ErrorWarning1.Location = new-object System.Drawing.Point(170,40)
$ErrorWarning1.Visible = $False # This line hides the label initially
$groupbox1.Controls.Add($ErrorWarning1)


################################# OPTIONS button
$ApplyButton = New-Object System.Windows.Forms.Button
$ApplyButton.Location = New-Object System.Drawing.Size (250,325)
$ApplyButton.Size = New-Object System.Drawing.Size (110,20)
$ApplyButton.Text = "Apply"
$ApplyButton.Add_Click({ApplyFunction})
$OptionsForm.Controls.Add($ApplyButton)

$Checkbox_Errors.Add_CheckStateChanged({   ### Checkbox_Errors is the name of the checkbox
$ErrorWarning1.Visible = $Checkbox_Errors.Checked
})

$OptionsForm.Add_Shown({$OptionsForm.Activate()})
[void] $OptionsForm.ShowDialog()
}

################################# Add'l Information Checkboxes
$Checkbox_AutoCopy = New-Object System.Windows.Forms.CheckBox #create the radio button
$Checkbox_AutoCopy.Location = new-object System.Drawing.Point(120,15) #location of the radio button(px) in relation to the group box's edges (length, height)
$Checkbox_AutoCopy.size = New-Object System.Drawing.Size(80,20) #the size in px of the radio button (length, height)
$Checkbox_AutoCopy.Checked = $true #is checked by default
$Checkbox_AutoCopy.Text = "Auto-Copy" #labeling the radio button
$Form.Controls.Add($Checkbox_AutoCopy) #activate the inside the group box

################################# GO button
$GoButton = New-Object System.Windows.Forms.Button
$GoButton.Location = New-Object System.Drawing.Size (495,250)
$GoButton.Size = New-Object System.Drawing.Size (60,20)
$GoButton.Text = "Go"
$GoButton.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor 
$GoButton.Add_Click({MonkeyMonkey}) 
$Form.Controls.Add($GoButton)
$Form.AcceptButton = $GoButton




$Form.Controls.Add($outputBox) 
$Form.Add_Shown({$Form.Activate()})

[void] $Form.ShowDialog()  

0 个答案:

没有答案