Home>

Automation is highly convenient,At the same time, some inevitable errors may be generated automatically.This is why powershell has some mechanisms to prevent and deal with dangers:these mechanisms confirm what to do next.

Commissioning:Simulation operation

If i want to know the impact of a certain command,You can do a trial run.At this time, powershell will not perform any operations that affect the system.Will only tell you if there is no simulation running,What impacts and consequences may have.Pass the -whatif parameter. In fact, many cmdltes support trial runs.

#If you execute stop-process -name * a *, the following processes will be terminated.
stop-process -name * a * -whatif
whatif:perform operation "stop-process" on target "acrord32 (4544)".
whatif:perform operation "stop-process" on target "acrord32 (4836)".
whatif:perform the operation "stop-process" on the target "alipaybsm (2888)".
whatif:perform the operation "stop-process" on the target "alipaysafetran (2808)".
whatif:perform the operation "stop-process" on the target "alipaysecsvc (1656)".
whatif:perform the operation "stop-process" on the target "aliveservice (1684)".
whatif:perform operation "stop-process" on target "cbgrabconnect_x64 (6052)".
whatif:perform the operation "stop-process" on the target "flashutil32_11_2_202_235_activex (1136)".
whatif:perform the operation "stop-process" on the target "iastordatamgrsvc (2920)".
whatif:perform operation "stop-process" on target "iastoricon (3112)".
whatif:perform the operation "stop-process" on the target "lsass (740)".
whatif:perform the operation "stop-process" on the target "notepad ++ (5124)".
whatif:perform operation "stop-process" on target "ravcpl64 (3484)".
whatif:perform operation "stop-process" on target "searchindexer (3788)".
whatif:perform the operation "stop-process" on the target "taskhost (2304)".
whatif:perform the operation "stop-process" on the target "unsecapp (264)".
whatif:perform the operation "stop-process" on the target "unsecapp (2680)".
whatif:perform operation "stop-process" on target "wlanext (1420)".
whatif:perform the operation "stop-process" on the target "zhudongfangyu (1156)".

Of course, if i want your scripts and functions to support simulation,Just a simple integration.Add one more switch parameter.

function mapdrive ([string] $driveletter, [string] $target, [switch] $whatif)
{
  if ($whatif)
  {
    write-host "whatif:creation of a network drive" + "with the letter ${driveletter}:at destination $target"
  }
  else
  {
    new-psdrive $driveletter filesystem $target
  }
}
#First run the simulation:
mapdrive k 127.0.0.1c $-whatif
whatif:creation of a network drive
with letter k:at destination 127.0.0.1c $
#Excuting an order
mapdrive k 127.0.0.1c $
name provider root
---- -------- ----
k filesystem 127.0.0.1c $

Step by step confirmation:query one by one

As you saw before,When using the "*" wildcard,Many tasks may occur in an instant.To prevent erroneous operations,Can be confirmed one by one,Release them one by one.

stop-process -name * cm * -confirm
confirm
Are you sure i want to do this?
Perform the operation "stop-process" on the target "cmd (1012)".
[y] Yes (y) [a] All (a) [n] No (n) [l] No (l) [s] Suspend (s) [?] Help (default is " y "):?
y-Only continue to the next step of the operation.
a-Continue with all steps of the operation.
n-skip this operation and continue to the next operation.
l-skip this operation and all subsequent operations.
s-Pause the current pipeline and return to the command prompt.
Type "exit" to continue the pipeline.
[y] Yes (y) [a] All (a) [n] No (n) [l] No (l) [s] Suspend (s) [?] Help (default is " y "):n
confirm
Are you sure i want to do this?
Perform the operation "stop-process" on the target "cmd (3704)".
[y] Yes (y) [a] All (a) [n] No (n) [l] No (l) [s] Suspend (s) [?] Help (default is " y "):s
ps e:>>>
ps e:>>exit
confirm
Are you sure i want to do this?
Perform the operation "stop-process" on the target "cmd (3704)".
[y] Yes (y) [a] All (a) [n] No (n) [l] No (l) [s] Suspend (s) [?] Help (default is " y "):y
confirm
Are you sure i want to do this?
Perform the operation "stop-process" on the target "cmd (4404)".
[y] Yes (y) [a] All (a) [n] No (n) [l] No (l) [s] Suspend (s) [?] Help (default is " y "):a

The confirmation program offers 6 options.

Yes-Only proceed to the next step of the operation.

All-continue with all steps of the operation.

No-Skip this operation and continue to the next operation.

Not all-skip this operation and all subsequent operations.

Suspend-Pauses the current pipeline and returns to the command prompt.Type "exit" to continue the pipeline.

Operate dangerous operations automatically

Because some operations have a higher risk factor than others,So the developer of the powershell script will perform a risk assessment for each command.For example stop-process, it will terminate the currently running program or process.Its risk factor is set to medium.Because under normal circumstances,Unrecoverable and unpredictable dangers that can arise.For example, the command to delete a user ’s mail in exchange is classified as high risk,Because once the message is deleted, it means that all the contents of the message no longer exist.

You cannot change these risk assessments,But you can respond to this.The default settings of powershell classify automated operations as dangerous operations,Even if you don't specify the -confirm parameter. This standard setting is stored in the global variable $confirmpreference. $confirmpreference can judge and respond to the default setting or other stricter settings.But if you set the value of $confirmpreference to "none", powershell will not ask for confirmation before operation,Even if you face high-risk operations.

#View $confirmpreference supported settings
[enum] ::getnames ($confirmpreference.gettype ())
none
low
medium
high
#View the current $confirmpreference
$confirmpreference
high
confirm
Are you sure i want to do this?
Perform the operation "stop-process" on the target "calc (5144)".
[y] Yes (y) [a] All (a) [n] No (n) [l] No (l) [s] Suspend (s) [?] Help (default is " y "):y

in conclusionThere are two cases.

Hazardous environment:If you are unsure of the danger of the operation to be performed,It's better to set $confirmpreference to low to be a bit more secure.

Security environment:If you are performing a number of unsafe operations in certain situations,For convenience, you can set $confirmpreference to none, because setting -confirm to false for each command is also a tedious thing.However, it is recommended to set $confirmpreference to "none" in the script, so that the authority can be set with the minimum risk.

  • Previous Example analysis of continuous animation effect with jQuery
  • Next File upload function based on Ajax and HTML5 in MVC