Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@didenko
Last active April 1, 2017 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save didenko/af33971bfa513f07a01c5fbc323a44e6 to your computer and use it in GitHub Desktop.
Save didenko/af33971bfa513f07a01c5fbc323a44e6 to your computer and use it in GitHub Desktop.
Confirmation PowerShell script

The confirm.ps1 is a wrapper script so that any other program can be wrapped into getting user confirmation before it is run. My use case is that I have disabled MS Windows Server system reboot or shutdown in the Start menu so that Administrators do not kill the production server by mistake when logging out. Nonetheless they are provided a desktop shortcut to reboot the server if really needed.

$command = $args[0]
$title = "Please, confirm:"
$message = "Would you like to execute the " + $command + "?"
$yes = New-Object `
System.Management.Automation.Host.ChoiceDescription `
" &Yes ", `
$( "Runs the command: " + $args )
$no = New-Object `
System.Management.Automation.Host.ChoiceDescription `
" &No ", `
"Aborts the action."
$options = [System.Management.Automation.Host.ChoiceDescription[]]( $yes, $no )
$result = $host.ui.PromptForChoice( $title, $message, $options, 1 )
switch ($result)
{
0 { $("" + $args) | Invoke-Expression }
1 {"Cancelled the command: " + $args }
}
powershell.exe -nologo -command "&" C:\bin\confirm.ps1 shutdown /r /t 15 /f /d p:0:0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment