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

Writing a basic script

Now that we have covered the basics of Windows PowerShell, let's write our first script. PowerShell uses the .ps1 format to save scripts. Before we get into executing scripts, let's first understand the following PowerShell Script Execution Policies that are in place to prevent unwanted scripts from being executed on your computer.

The cmdlet Set-Execution Policy helps you to change the Policy of which scripts you can run on your computer. There are four different execution policies:

  • Restricted: In this mode, you cannot run any script and can use Windows PowerShell to run cmdlets in the interactive mode only.
  • AllSigned: Locally created and those signed by a trusted publisher can be run
  • RemoteSigned: Scripts downloaded from another computer can run only if these are signed by a trusted publisher
  • Unrestricted: All the scripts will run regardless of whether they are signed or not

Now, let's set the execution Policy to Remote Signed and verify using Get-Execution Policy cmdlet:

PS C:\> Set-ExecutionPolicy RemoteSigned
PS C:\> Get-ExecutionPolicy

I am going to introduce the PowerShell Integrated Scripting Engine (ISE) that makes it easier to run scripts with features, such as syntax-coloring, tab completion, IntelliSense, visual debugging, and context sensitive Help, as compared to the PowerShell console.

The following script will query the win32_logical disk WMI class. This is a representation of a local storage device on a Windows Computer. There are six possible Drive Types, and we will use a Switch statement to get the output in text describing the type of storage:

主站蜘蛛池模板: 潼南县| 大姚县| 日照市| 乳山市| 横峰县| 明星| 巨野县| 鞍山市| 开江县| 襄垣县| 彭州市| 湄潭县| 东港市| 大方县| 凤城市| 丹棱县| 拜泉县| 调兵山市| 新龙县| 武安市| 泰兴市| 饶河县| 安溪县| 仁怀市| 崇信县| 清新县| 彭州市| 高要市| 靖远县| 湘西| 根河市| 山阴县| 沙田区| 麻江县| 珲春市| 通榆县| 璧山县| 玛纳斯县| 丰镇市| 乐业县| 疏勒县|