自动化具有高度方便的特点,同时也可能会自动产生一些不可避免的错误。这也就是powershell为什么会有一些专门来防止和处理危险的机制:这些机制会对接下来要执行的操作进行确认。
试运行:模拟操作
如果你想知道一个确定的命令会产生什么影响,你可以进行试运行。这时,powershell不会执行任何对系统有影响的操作,只会告诉你如果没有模拟运行,可能产生什么影响和后果。通过-whatif 参数。事实上,许多cmdltes都支持试运行。
#如果执行stop-process -name *a*会终止下面的进程。 stop-process -name *a* -whatif whatif: 对目标“acrord32 (4544)”执行操作“stop-process”。 whatif: 对目标“acrord32 (4836)”执行操作“stop-process”。 whatif: 对目标“alipaybsm (2888)”执行操作“stop-process”。 whatif: 对目标“alipaysafetran (2808)”执行操作“stop-process”。 whatif: 对目标“alipaysecsvc (1656)”执行操作“stop-process”。 whatif: 对目标“aliveservice (1684)”执行操作“stop-process”。 whatif: 对目标“cbgrabconnect_x64 (6052)”执行操作“stop-process”。 whatif: 对目标“flashutil32_11_2_202_235_activex (1136)”执行操作“stop-process”。 whatif: 对目标“iastordatamgrsvc (2920)”执行操作“stop-process”。 whatif: 对目标“iastoricon (3112)”执行操作“stop-process”。 whatif: 对目标“lsass (740)”执行操作“stop-process”。 whatif: 对目标“notepad++ (5124)”执行操作“stop-process”。 whatif: 对目标“ravcpl64 (3484)”执行操作“stop-process”。 whatif: 对目标“searchindexer (3788)”执行操作“stop-process”。 whatif: 对目标“taskhost (2304)”执行操作“stop-process”。 whatif: 对目标“unsecapp (264)”执行操作“stop-process”。 whatif: 对目标“unsecapp (2680)”执行操作“stop-process”。 whatif: 对目标“wlanext (1420)”执行操作“stop-process”。 whatif: 对目标“zhudongfangyu (1156)”执行操作“stop-process”。
当然如果你想让自己的脚本和函数也支持模拟运行,只需要进行简单的整合。多增加一个switch参数。
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 } } # 首先进行模拟运行: mapdrive k 127.0.0.1c$ -whatif whatif: creation of a network drive with letter k: at destination 127.0.0.1c$ # 执行命令 mapdrive k 127.0.0.1c$ name provider root ---- -------- ---- k filesystem 127.0.0.1c$
逐步确认:逐个查询
正如之前看到的那样,在使用“*”通配符时,可能会在瞬间产生许多任务。为了防止产生失误操作,可以逐个进行确认,逐个进行放行。
stop-process -name *cm* -confirm 确认 是否确实要执行此操作? 对目标“cmd (1012)”执行操作“stop-process”。 [y] 是(y) [a] 全是(a) [n] 否(n) [l] 全否(l) [s] 挂起(s) [?] 帮助 (默认值为“y”): ? y - 仅继续执行操作的下一步骤。 a - 继续执行操作的所有步骤。 n - 跳过此操作并继续执行下一操作。 l - 跳过此操作及所有后续操作。 s - 暂停当前管道并返回到命令提示符。键入“exit”可继续执行该管道。 [y] 是(y) [a] 全是(a) [n] 否(n) [l] 全否(l) [s] 挂起(s) [?] 帮助 (默认值为“y”): n 确认 是否确实要执行此操作? 对目标“cmd (3704)”执行操作“stop-process”。 [y] 是(y) [a] 全是(a) [n] 否(n) [l] 全否(l) [s] 挂起(s) [?] 帮助 (默认值为“y”): s ps e:>>> ps e:>>> exit 确认 是否确实要执行此操作? 对目标“cmd (3704)”执行操作“stop-process”。 [y] 是(y) [a] 全是(a) [n] 否(n) [l] 全否(l) [s] 挂起(s) [?] 帮助 (默认值为“y”): y 确认 是否确实要执行此操作? 对目标“cmd (4404)”执行操作“stop-process”。 [y] 是(y) [a] 全是(a) [n] 否(n) [l] 全否(l) [s] 挂起(s) [?] 帮助 (默认值为“y”): a
确认程序提供了6个选项。
是 – 仅继续执行操作的下一步骤。
全是 – 继续执行操作的所有步骤。
否 – 跳过此操作并继续执行下一操作。
不全是 – 跳过此操作及所有后续操作。
挂起 – 暂停当前管道并返回到命令提示符。键入“exit”可继续执行该管道。
帮助 – 提供帮助信息
自动对危险的操作进行操作
因为一些操作的危险系数会比其它的操作的危险系数高,所以powershell脚本的开发者会每一条命令进行风险评估。例如stop-process,它会终止当前正在运行的程序或者进程。它的危险系数被设置为中等。因为正常情况下,可能会产生的不可恢复并且不可预知的危险。例如exchange中的命令删除用户的邮件被归类为高危险性,因为邮件一旦删除那就意味着所有的邮件内容都不复存在。
你不可能改变这些风险评估,但是你可以对此作出回应。powershell的默认设置会将自动化的操作归类为危险操作,即使你没有指定-confirm参数。这一标准的设置被存储在全局变量$confirmpreference中。$confirmpreference可以对默认设置或者其它更严格的设置作出判断与回应。但是如果你将$confirmpreference的值设置为“none”,powershell就不会进行操作前询问确认,即使可能面临高风险的操作。
#查看$confirmpreference支持的设置 [enum]::getnames($confirmpreference.gettype()) none low medium high #查看当前的$confirmpreference $confirmpreference high 确认 是否确实要执行此操作? 对目标“calc (5144)”执行操作“stop-process”。 [y] 是(y) [a] 全是(a) [n] 否(n) [l] 全否(l) [s] 挂起(s) [?] 帮助 (默认值为“y”): y
总结一下,就会有两种情况。
危险环境:如果你对要执行的操作的危险性不确定,最好将$confirmpreference 设置为low,这样可以更安全一点。
安全环境:如果你在某些情况下要执行大量无关安全的操作,为了方便起见可以将$confirmpreference设置为none,因为针对每个命令都去设置-confirm 为false也是一件很繁琐的事情。但是建议最好在脚本中将$confirmpreference设置为”none”,这样可以将放权设置在最小的风险下。
发表评论