Cannot switch a Button in PowerShell
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
}