• No results found

Running PowerShell Scripts

In document IderaWP Powershell eBook Part 1.PDF (Page 45-48)

Notepad $env:temp\test.ps1

You can now enter any PowerShell code you want, and save the file. Once saved, you can also open your script with more sophisticated and specialized script editors. PowerShell comes with an editor called PowerShell ISE, and here is how you’d open the file you created with Notepad:

Try to run your script after you’ve created it:

Ise $env:temp\test.ps1

You’ll probably receive an error message similar to the one in the above example. All PowerShell scripts are initially disabled. You need to allow PowerShell to execute scripts first. This only needs to be done once:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser .\test.ps1

File “C:\Users\UserA\test.ps1” cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details.

At line:1 char:10 + .\test.ps1 <<<<

This grants permission to run locally stored PowerShell scripts. Scripts from untrusted sources, such as the Internet, will need to carry a valid digital signature or else they won’t run. This is to protect you from malicious scripts, but if you want to, you can turn this security feature off. Replace RemoteSigned with Bypass. The implications of signatures and other security settings will be discussed in Chapter 10. For now, the line above is enough for you to experiment with your own PowerShell scripts. To restore the original setting, set the setting to Undefined:

To get a complete picture, also try using the -List parameter with Get-ExecutionPolicy:

You now see all execution policies. The first two are defined by Group Policy so a corporation can centrally control execution policy. The scope “Process” refers to your current session only. So, you can use this scope if you want to only temporarily change the execution policy. No other PowerShell session will be affected by your change. “CurrentUser” will affect only you, but no other users. That’s how you can change this scope without special privileges. “LocalMachine,” which is the only scope available in PowerShell v.1, will affect any user on your machine. This is the perfect place for companies to set initial defaults that can be overridden. The default setting for this scope is “Restricted.”

Set-ExecutionPolicy Undefined -Scope CurrentUser

Get-ExecutionPolicy -List

Scope ExecutionPolicy --- ---MachinePolicy Undefined

UserPolicy Undefined Process Undefined CurrentUser RemoteSigned LocalMachine Restricted

The effective execution policy is the first policy from top to bottom in this list that is not set to “Undefined.” If all policies are set to

“Undefined,” then scripts are prohibited.

Note: To turn off signature checking altogether, you can set the execution policy to “Bypass.” This can be useful if you must run scripts regularly that are stored on file servers outside your domain. Otherwise, you may get security warnings and confirmation dialogs. Always remember: execution policy exists to help and protect you from potentially malicious scripts. If you are confident you can safely identify malicious scripts, then nothing is wrong by turning off signature checking. However, we recommend not using the “Bypass” setting if you are new to PowerShell.

The PowerShell console can run all kinds of commands interactively. You simply enter a command and the console will return the results.

Cmdlets are PowerShell’s own internal commands. A cmdlet name is always composed of a verb (what it does) and a noun (where it acts upon).

To find a particular command, you can either guess or use Get-Command. For example, this will get you a list if you wanted to find all cmdlets dealing with event logs:

Invoking Files and Scripts

Get-Command -Noun EventLog

Search for the verb “Stop” to find all cmdlets that stop something:

You can also use wildcards. This will list all cmdlets with the keyword “computer”:

Once you know the name of a particular cmdlet, you can use Get-Help to get more information. This function will help you view help information page by page:

Cmdlets are just one of six command types you can use to get work done:

· Alias: Shortcuts to other commands, such as dir or ls

· Function: “Macros” that run code and resemble “self-made” new commands Get-Command -Verb Stop

Get-Command *computer* -commandType cmdlet

Get-Help Stop-Computer Help Stop-Computer -examples Help Stop-Computer -parameter *

· Cmdlet: Built-in PowerShell commands

· Application: External executables, such as ipconfig, ping or notepad

· PowerShell scripts: Files with extension *.ps1 which can contain any valid PowerShell code · Other files: Batch files, VBScript script files, or any other file associated with an executable

If commands are ambiguous, PowerShell will stick to the order of that list. So, since the command type “Alias” is at the top of that list, if you define an alias like “ping”, it will be used instead of ping.exe and thus can override any other command type.

It is time to combine commands whenever a sin-gle PowerShell command can’t solve your prob-lem. One way of doing this is by using variables.

PowerShell can store results of one command in

In document IderaWP Powershell eBook Part 1.PDF (Page 45-48)

Related documents