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

Building parameterized script

When you start building script for automation that can be used by yourself and others repeatedly for doing some task, building parameterized script is the key. In PowerShell, it is very easy to build the parameterized script using just a simple param keyword in the script file. You may have solved the problem using individual cmdlets, and you now want that one to be used repeatedly for building an automation. Let's review the following script for a better understanding of using parameters:

I created a file called DiskInfo.ps1 in the D:\scripts directory. This script provides you with free GB available on the drive that you pass to this script. I am using Get-CimInstance to get me the computer name and free GB. The param keyword at the top of the script can be seen. You might just need to get a feeling of writing that syntax and nothing more. To make the drive letter mandatory, I used a keyword called Mandatory=$true. Once you saved the file, the script can be run as follows:

PS :\D>.\DiskInfo.ps1 -Drive C:

The beauty of the script is that when you start hitting the tab after the script name on the command prompt, it starts displaying you the parameters that can be passed to the script, which is amazing. This is the way you start building a new set of cmdlets for you. There is nothing else that needs to be done to build your own cmdlets.

主站蜘蛛池模板: 永春县| 彭山县| 萨迦县| 始兴县| 兴和县| 富顺县| 兰州市| 双流县| 湖州市| 临桂县| 三穗县| 高雄县| 仲巴县| 同仁县| 巩留县| 渝北区| 芜湖市| 达拉特旗| 赣榆县| 东港市| 博白县| 襄汾县| 嘉禾县| 嵩明县| 襄垣县| 万安县| 中宁县| 长宁区| 荔波县| 咸阳市| 任丘市| 建宁县| 南澳县| 周至县| 东台市| 玉山县| 彰武县| 屏东市| 巫山县| 许昌市| 泰宁县|