官术网_书友最值得收藏!

Risk mitigation parameters

PowerShell has two risk mitigation parameters called WhatIf and Confirm. They are very useful for testing complicated scripts without risking the code running amok. By appending WhatIf and Confirm, you get a preview of what could have happened without risking the damage. Let's take a real-life example of a file deletion using a wildcard. Consider that there are some files, and you plan to delete them. But you want to ensure that you are deleting the right set of files that you intend to delete. Because you are using a wildcard, the consequences could be very serious. Hence, it is always prudent to ensure that you are not risking the run of the command. Lets assume that you want to remove file*.txt files from some directory; you can use WhatIf something like following:

PS C:\>Get-Childitem C:\somedata\file*.txt -Recurse | Remove-Item -WhatIf

In the example, we used file*.txt (with a wildcard), and the command did not make any permanent change when you appended the command with WhatIf. The command run is just letting us know that if you run it without WhatIf, it is going to delete all those five files. Likewise, you can use Confirm by appending at the end of the command to get a confirmation if the specific file can be deleted or not:

PS C:\>Get-Childitem C:\somedata\file*.txt -Recurse | Remove-Item -Confirm

These two risk mitigation parameters are really powerful when you start rolling out the script to hundreds of servers, and it will help to ease your anxiety a little bit.

主站蜘蛛池模板: 高清| 通辽市| 贵州省| 南阳市| 肇庆市| 宝山区| 丰原市| 三穗县| 黄冈市| 汉源县| 郓城县| 秦皇岛市| 谢通门县| 西华县| 湖州市| 元氏县| 城市| 铜梁县| 新干县| 区。| 高邮市| 漳浦县| 镇远县| 新巴尔虎左旗| 宜丰县| 南溪县| 阳泉市| 常山县| 广州市| 小金县| 巴塘县| 墨竹工卡县| 顺义区| 台中县| 乌兰县| 凉城县| 西充县| 锦屏县| 临高县| 文昌市| 浮梁县|