first of all, I am really new to PowerShell and C# and currently trying out stuff to learn it. I was creating form with the System.Windows.Forms Class and made a small window including two buttonds and a check box. One of the buttons will stay hidden while the other is visible to kinda swap actions as I did not figure another way to do it. The first Button activates, if the check box is $true, then it triggers the shutdown.exe /s process and changes a variable for the second button. I got it to make the first button work properly, but the second does not seem to do anything at all. Would you mind going over my code to tell me where I made a mistake?
PowerShell.exe -windowstyle hidden {
# Start of code
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
# Declaration of the form
$folderForm = New-Object System.Windows.Forms.Form
$folderForm.StartPosition = 'Manual'
$folderForm.Location = '2267,1107'
# Declaration of the Check Box
$shutdownCheck = New-Object System.Windows.Forms.CheckBox
$shutdownCheck.Location = New-Object System.Drawing.Size(23,23)
$shutdownCheck.Size = '27,27'
$folderForm.Controls.Add($shutdownCheck)
# Declaration of the Shutdown-Button
$shutdownButton = New-Object System.Windows.Forms.Button
$shutdownButton.Text = 'Feierabendmodus aktivieren'
$shutdownButton.Location = '120,23'
$shutdownButton.Size = '160,23'
$folderForm.Controls.Add($shutdownButton)
# Declaration of the Stop-Button
$stopButton = New-Object System.Windows.Forms.Button
$stopButton.Text = 'Sequenz abbrechen!'
$stopButton.Location = '120,23'
$stopButton.Size = '160,23'
$stopButton.Visible = $false
$folderForm.Controls.Add($stopButton)
function TestClick
{
$varX = $true
#Start
if ($shutdownCheck.Checked -eq $true -and $varX -eq $true)
{
# BalloonTip
[reflection.assembly]::loadwithpartialname('System.Windows.Forms')
[reflection.assembly]::loadwithpartialname('System.Drawing')
$notify = new-object system.windows.forms.notifyicon
$notify.Icon = [System.Drawing.SystemIcons]::Information
$notify.visible = $true
$notify.showballoontip(20,'Filler','More Filler',
[system.windows.forms.tooltipicon]::None)
# Shutdown-Prozess
Start-Process shutdown.exe /s
#Stop-Process powershell_ise -force
$shutdownButton.Visible = $false
$stopButton.Visible = $true
$shutdownCheck.Checked = $false
$varX = $false
}
elseif ($shutdownCheck.Checked -eq $false -and $varX -eq $false)
{
# Stop
Start-Process shutdown.exe /a
$shutdownButton.Text = 'Filler'
$stopButton.Visible = $false
$shutdownButton.Visible = $true
$varX = $true
}
}
$shutdownButton.Add_Click({TestClick})
$stopButton.Add_Click({TestClick})
$folderForm.ShowDialog()
# End of code
}
CodePudding user response:
The code below creates a button whose text changes after it's clicked - it's your code with some modifications. Try the following to see if it meets your needs:
PowerShell.exe -windowstyle normal {
# Start of code
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
# Declaration of the form
$folderForm = New-Object System.Windows.Forms.Form
$folderForm.StartPosition = 'Manual'
$folderForm.Location = '267,107'
$folderForm.Size = '315, 200'
$folderForm.Text = 'Shutdown'
# Declaration of the Shutdown-Button
$shutdownButton = New-Object System.Windows.Forms.Button
$shutdownButton.Text = 'Shutdown'
$shutdownButton.Location = '75,23'
$shutdownButton.Size = '150,23'
$folderForm.Controls.Add($shutdownButton)
function TestClick
{
#Start
if ($shutdownButton.Text -eq 'Shutdown')
{
# BalloonTip
[reflection.assembly]::loadwithpartialname('System.Windows.Forms')
[reflection.assembly]::loadwithpartialname('System.Drawing')
$notify = new-object system.windows.forms.notifyicon
$notify.Icon = [System.Drawing.SystemIcons]::Information
$notify.visible = $true
$notify.showballoontip(20,'Shutdown','Shutting down the computer.',
[system.windows.forms.tooltipicon]::None)
#ToDo: replace the code below with desired code
# Shutdown-Prozess
$shutdownButton.Text = 'Abort Shutdown'
#Start-Process shutdown.exe /t 60
Start-Process shutdown.exe /?
}
else
{
# BalloonTip
[reflection.assembly]::loadwithpartialname('System.Windows.Forms')
[reflection.assembly]::loadwithpartialname('System.Drawing')
$notify = new-object system.windows.forms.notifyicon
$notify.Icon = [System.Drawing.SystemIcons]::Information
$notify.visible = $true
$notify.showballoontip(20,'Shutdown Aborted','Shutdown has been aborted.',
[system.windows.forms.tooltipicon]::None)
# Stop
$shutdownButton.Text = 'Shutdown'
Start-Process shutdown.exe /a
}
}
$shutdownButton.Add_Click({TestClick})
$folderForm.ShowDialog()
# End of code
}
CodePudding user response:
Two issues:
$varXis getting set to true every time you click- The TestClick function is only updating a local-scope
$varX(only within that function)
# your current code (shortened)
function TestClick
{
$varX = $true ## getting set every button click (in local scope)
if ($shutdownCheck.Checked -eq $true -and $varX -eq $true)
{
$varX = $false ## local scope
}
elseif ($shutdownCheck.Checked -eq $false -and $varX -eq $false)
{
$varX = $true ## local scope
}
}
To fix, try doing something like this instead:
function TestClick
{
if ($shutdownCheck.Checked -eq $true -and $varX -eq $true)
{
$Script:varX = $false ## Scope the variable so the function sets it properly
}
elseif ($shutdownCheck.Checked -eq $false -and $varX -eq $false)
{
$Script:varX = $true ## here too
}
}
$varX = $true ## Move the initial setting outside the function
I suggest skipping the function and global status all together, and simply use the different buttons you created:
# shutdown button
$shutdownButton.Add_Click({
if ($shutdownCheck.Checked) {
Start-Process shutdown.exe /s
$shutdownButton.Visible = $false
$shutdownCheck.Checked = $false
$stopButton.Visible = $true
}
})
# stop button (hidden until shutdown clicked)
$stopButton.Add_Click({
Start-Process shutdown.exe /a
$shutdownButton.Visible = $true
$shutdownButton.Text = 'Stopped'
$stopButton.Visible = $false
})
$folderForm.ShowDialog()
# End of code
