P. 1
Adl 10 Powershell Concepts

Adl 10 Powershell Concepts

|Views: 0|Likes:
Published by Krishna Gopi
top anti vitus softwares for free
top anti vitus softwares for free

More info:

Published by: Krishna Gopi on Aug 24, 2013
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





10 fundamental concepts for PowerShell scripting

January 21, 2011 By Brien Posey PowerShell scripts offer a handy way to automate various chores. Here are some key concepts that will help beginners as they start developing PowerShell scripts.

1: PS1 files
A PowerShell script is really nothing more than a simple text file. The file contains a series of PowerShell commands, with each command appearing on a separate line. For the text file to be treated as a PowerShell script, its filename needs to use the .PS1 extension.

2: Execution permissions
To prevent the execution of malicious scripts, PowerShell enforces an execution policy. By default, the execution policy is set to Restricted, which means that PowerShell scripts will not run. You can determine the current execution policy by using the following cmdlet: Get-ExecutionPolicy The execution policies you can use are: Restricted – Scripts won't run. RemoteSigned – Scripts created locally will run, but those downloaded from the Internet will not (unless they are digitally signed by a trusted publisher). AllSigned – Scripts will run only if they have been signed by a trusted publisher. Unrestricted – Scripts will run regardless of where they have come from and whether they are signed. You can set PowerShell's execution policy by using the following cmdlet: Set-ExecutionPolicy <policy name>

3: Running a script
For years now, if you wanted to run an executable file from the command line the practice was to navigate to the file's path and then type the name of the executable file. However, this age-old method doesn't work for PowerShell scripts. If you want to execute a PowerShell script, you will usually have to type the full path along with the filename. For example, to run a script named SCRIPT.PS1, you might type: C:\Scripts\Script.ps1 The big exception is that you can execute a script by simply typing its name if the folder containing the script is in your system's path. There is also a shortcut you can use if you are already in the folder containing the script. Instead of typing the script's full path in such a situation, you can enter .\ and the script's name. For example, you might type: .\Script.ps1
Page 1 Copyright © 2011 CNET Networks, Inc., a CBS Company. All rights reserved. TechRepublic is a registered trademark of CNET Networks, Inc For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html

For example. to sort the running processes by process ID and then assign the output to a variable. Notice how the @ symbol is being used in front of the variable name rather than the dollar sign that we usually see used: Get-Process @procs 7: Split The split operator splits a text string based on a character you designate. This allows the second command to act on the input it has received.. suppose you want to store the list of processes running on a server as a variable. It's easy to think of a variable as a repository for storing a value. For example. suppose that you want to break a sentence into an array consisting of each individual word in the sentence. You can get a list of processes by using the GetProcess cmdlet. take the following line of code. You could do so by using a command like this one: "This is a test" –split " " Page 2 Copyright © 2011 CNET Networks. to ensure that it is treated as an array rather than a single value. Occasionally. For example. In doing so. the variable is named $a. For example. However. you could use this command: $a = (Get-Process | Sort-Object ID) 6: The @ symbol By using the @ symbol. TechRepublic is a registered trademark of CNET Networks. please visit http://techrepublic. a variable can store a command's full output. To help you understand how pipelining works. typing $a prints the variable's contents on the screen. that output is used immediately.html . but the list will not be sorted. To do so."svchost"} You can also use the @ symbol when the variable is used.10 fundamental concepts for PowerShell scripting 4: Pipelining Pipelining is the term for feeding one command's output into another command. simply separate them with the pipe symbol (|). The string of commands used looks like this: Get-Process | Sort-Object ID 5: Variables Although you can use pipelining to feed one command's output into another command. For instance. you can turn the contents of a list into an array. but in PowerShell. For example. the list will be sorted.com. This is where variables come into play. if you pipeline the cmdlet's output into the Sort-Object ID command. If you want to use the variable. you may need to store the output for a while so that you can use (or reuse) it later. Windows will display all the processes used by Windows Explorer and Svchost. you could use this line of code: $a = Get-Process Here. imagine that you want to create a list of processes that are running on a server and sort that list by process ID number. When you pipeline a command's output into another command.com/2001-6240-0. All rights reserved. a CBS Company. sometimes pipelining alone won't get the job done. which creates a variable named $Procs that contains multiple lines of text (an array): $procs = @{name="explorer". To pipeline two commands (or cmdlets). You can assign a variable to the final output of multiple commands that have been pipelined together. the line of code below will run the Get-Process cmdlet against the variable I defined a moment ago. Inc. Just surround the commands with parentheses. Inc For more downloads and a free TechRepublic membership. simply call it by name.

The entire function will run without stopping. 10: Step When debugging a script.com/2001-6240-0. So if you wanted your script to break any time the contents of a$ changed. Incidentally."Posey" –join " " The space between the quotation marks at the end of the command tells Windows to insert a space between the two text strings. TechRepublic is a registered trademark of CNET Networks. if your script uses functions. however.10 fundamental concepts for PowerShell scripting The result would look like this: This is a test 8: Join Just as split can split a text string into multiple pieces. Get. you can make sure that the script is working as intended before you process the entire thing. you can use the Step-Into cmdlet.. For example. 9: Breakpoints Running a newly created PowerShell script can have unintended consequences if the script contains bugs. All rights reserved. and Remove. This will cause the script to pause after each line regardless of whether a breakpoint exists. There are a number of verbs you can use with PSBreakpoint including New. Inc. The easiest way to insert a breakpoint is by line number. you can use the Step-Out cmdlet to stop Windows from stepping through the script.com. a CBS Company. Disable. Page 3 Copyright © 2011 CNET Networks. that breakpoints are still processed even after the Step-Out cmdlet has been used.ps1 –Line 10 You can also bind a breakpoint to a variable. Step-Over works just like Step-Into. to insert a break point on the 10th line of a script. you could use a command like this one: New-PSBreakpoint –Script C:\scripts\Script. For instance.ps1 –variables a Notice that I didn't include the dollar sign after the variable name. you could use a command like this: New-PSBreakpoint –Script C:\Scripts\Script. Windows won't step through it. Enable. you may be interested in the Step-Over cmdlet. except that if a function is called. To do so. It is worth noting. When you are done. this line will create a text string consisting of my first name and last name: "Brien". please visit http://techrepublic. Inc For more downloads and a free TechRepublic membership. That way.html . the join operator can combine multiple blocks of text into one. it may sometimes be necessary to run the script line by line. One way to protect yourself is to insert breakpoints at strategic locations within your script.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->