Skip to content

Instantly share code, notes, and snippets.

@crftr
Created May 14, 2012 21:54
Show Gist options
  • Save crftr/2697589 to your computer and use it in GitHub Desktop.
Save crftr/2697589 to your computer and use it in GitHub Desktop.
For when you want to easily schedule a local shutdown. e.g. a windows sleep timer. This is a simple UI to assist.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Shutdown Timer"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(100,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "Set Timer"
$OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,40)
$objLabel.Text = "Shutdown in how many minutes? (type 'cancel' to stop a pending timer)"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,60)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$tool="shutdown"
if($x -eq 'cancel'){
$cmdLine = "-a"
invoke-expression "$tool $cmdLine"
}
elseif($x -match '^\d+$'){
$secondsTilShutdown = [System.Convert]::ToInt32($x) * 60
$cmdLine = "-s -t $secondsTilShutdown -f"
invoke-expression "$tool $cmdLine"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment