Powershell – Self-Destruct Button…

While it usually is the case that the owner or creator of a tool or device would like to keep the item he or she has created safe and sound, occasionally one might create a one-time-use tool that is designed to service its purpose for only a brief time, and then be eliminated — in case further use of the tool is harmful to the purpose for which it was originally invented.  Imagine a wrecking ball for instance; while extremely useful during demolition phase, you don’t want it lying about swinging the big iron ball near your new glass windows when construction is nearly completed, do you?

In these cases, I am reminded of Dr. Heinz Doofenshmirtz, the famous inventor.  For those that know me, it will likely come as no surprise that I’m a big fan of the Doctor’s prolific and voluminous work.  One interesting (and perhaps little-known) facet of his work is that each of his inventions holds an essential – but often overlooked – element of self-mitigation.  It has been said that this feature was sometimes erroneously invoked before the completion of its ascribed task, but — and possibly due to the nature of the feature — scant evidence of this is available.  Nonetheless, he was diligent in making sure that the feature was always available, so none of his tools could cause harm beyond its intended usage.

So it is in this vein — and in honor (and in the spirit) of Dr. Doofenshmirtz — that today I’ve written what might perhaps be one of the more intriguing and perhaps jocular scripts yet:  A PowerShell Self-Destruct Button. 

This is a script that prompts you whether or not it should self-destruct; and then if so, promptly deletes itself.  Enjoy:

 

$CurrentScriptFullPathName = $MyInvocation.MyCommand.Definition
$CurrentScriptName = $MyInvocation.MyCommand.Name
clear
write ""
write "Self-Destruct option: "
$SelfDestructChoice = Read-Host "Do you wish for this script ($CurrentScriptName) to destroy itself upon completion? (Enter Yes or No)"
if ("$SelfDestructChoice" -eq "No")
{
  write ""
  write "Okay, this script will not Self-Destruct."
  write "Keeping: $CurrentScriptFullPathName"
  write ""
}
elseif ("$SelfDestructChoice" -eq "Yes")
{
  write ""
  write "You pushed the Self-Destruct button! This script will now attempt to delete itself..." 
  #write ""
  write "Deleting: $CurrentScriptFullPathName"
  Remove-Item $CurrentScriptFullPathName
  Start-Sleep 3
  write ""
  write "Validating removal...  Is $CurrentScriptName still present?"
  Start-Sleep 3
  Test-Path $CurrentScriptFullPathName
  write ""
  Start-Sleep 3
}
else
{
  write ""
  write "Invalid OverRide choice: "$SelfDestructChoice" is not valid.  Must be Yes or No. Exiting!"
  write ""
  exit
}
write "Complete! Exiting..."
Start-Sleep 3

To walk through this script a bit:  During processing, it first gathers up PathName and ScriptName variable values to help it understand where it is and what it might delete (you don’t want to delete the wrong file!).  Then, it asks you the deadly-serious question of whether or not it should be deleted upon completion.  If not, exit.  If so, self-destruct/delete (and validate).  All that, and a few formatting items to make it all nice.

There you go!  You can copy the text into a file, run it, and amaze your friends.  Maybe amaze the entire Tri-State Area!

🙂

 

2017-07-27T00:01:03+00:00 May 29th, 2013|Uncategorized|

About the Author:

Jeremy is just a regular guy that likes to occasionally tell the world about stuff.