Tuesday, 4 March 2014

Powershell Pointer: Foreach and Foreach-Object


Foreach
Foreach-Object

Loop

Cmdlet

Used when elements of the array are known previously and are saved in a variable.

Used for parallel processing when elements of the array are not known in advance.


Input cannot be obtained from pipelines.

Usage :  Foreach ($var1 in $var2) { Do something}


Input is received from pipeline only.

Usage :  $var | Foreach-Object {Do something }

Cannot output to a pipeline.

Can write to pipeline

Usage : $var | Foreach-Object {Do something } | Do-SomethingElse


Thursday, 13 June 2013

Pointers from Scripting Games 2013


The scripting games have just concluded with all 6 challenges drawing to a close. The games presented a wonderful learning opportunity along with helpful insights from expert judges and peers. The knowledge gained from reviewing other contributors' scripts and comments was immense. You can check the scripts and comments by following this link.

A few pointers that i picked up through the games are listed below: 


  • Full command and parameter names

Every challenge in the scripting games explicitly stated the preference for full command and parameter names instead of alias or abbreviations. It makes it easier for everyone to understand the script without much effort.


  • Formatting

Formatting the code with white spaces and tabs to create indents makes for easy reading.
This was important particularly in the beginner’s events where it was encouraged to write a one-liner. By introducing carriage returns and indents wherever possible, the integrity of a one liner is not broken and it is presented in a much better way.


  • Comment based help
I admit I had never considered including comment based help in my scripts before the scripting games. But after realizing its significance and due to encouragement from the community, it is a staple in my scripts now.

Don Jones also explains the concept here


  •  Requires
The #Requires tag can be used in scripts to annotate the powershell version necessary to execute the script. It is generally placed on top of the script.


  • Write-Host
Write-Host cmdlet is only capable of outputting data on screen. It does NOT write to pipeline. Almost every need to include the cmdlet can be replaced by either Write-Verbose or Write-Debug or Write-Error or Write-Warning or Write-Output.

That being said, Write-Host cmdlet’s ability to display colorful data on-screen using the –Foreground and –Background switches is unique.


  • Format* commands
Similar to Write-Host, format commands are also the last one in the pipeline. It can display data on screen and there can be no further data manipulation.


  • Filtering on server side
When querying data specially from a remote computer, it is always better to filter data on the server and then retrieve them. The script’s execution time reduces considerably.


  • Error handling
Error handling was another important feature that was emphasized in the games, notably in the advanced challenges. Try/Catch/Finally blocks can be an effective method to include the same within scripts.

Refer the following links for further information:



  • Use native Powershell commands and naming conventions.
Using native powershell commands, as much as possible, is also generally considered as good practice.


Tuesday, 28 May 2013

Eliminate Ellipsis In Powershell


When trying to display the result of some queries on screen, we may find that some of the output is truncated by ellipsis i.e. “ . . .” , often the case with multi-valued attributes. An example of the ellipsis can be found when retrieving the thread details of any process. The image below shows the output when querying the thread details for system process:

Get-Process System | select Name,Threads




The reason that the thread column gets truncated at 4 values is because of $FormatEnumerationLimit. Help about the variable can be found under About_Preference_Variables. The value is set at 4 by default in the powershell console and the value is 16 in the exchange shell.











The entire thread details can be listed by modifying the above command to:

Get-Process System | select Name,Threads | ft –Wrap –Autosize



An alternate method to display the threads would be to use the ‘ExpandProperty’ switch in Select-Object cmdlet:

get-process system | Select-Object Name -ExpandProperty Threads | ft Name, ID –AutoSize




The easiest method would be to change the default value of the $FormatEnumerationLimit.

The default value has been altered to 80 which displays the whole list of system threads on screen: