header image

Archive for Scripting Games

Filter vs Include

Posted by: | May 1, 2013 | 1 Comment |

 

I’ve gone through most of the Beginner event submissions over the last couple of days.  One thing that has jumped out is the potential misunderstanding around using the –include or –filter  parameters on get-childitem.

If we look at the two parameters

-Include <String[]>
    Gets only the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or  pattern, such as "*.txt". Wildcards are permitted.
    The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a directory, such as C:\Windows\*, where the wildcard character specifies the contents of the C:\Windows directory.

-Filter <String>
    Specifies a filter in the provider’s format or language. The value of this parameter qualifies the Path parameter.
    The syntax of the filter, including the use of wildcards, depends on the provider. Filters are more efficient than other parameters, because the provider applies them when retrieving the objects, rather than having Windows PowerShell filter the objects after they are retrieved.

These are best explained by some examples. Lets start with the current directory

PS> Get-ChildItem -Filter *.csv | select -First 1

    Directory: C:\MyData\SkyDrive\Data\scripts

Mode                LastWriteTime     Length Name
—-                ————-     —— —-
-a—        12/09/2012     19:07       2665 log.csv

 

but

Get-ChildItem -include *.csv | select -First 1

returns nothing

To get –Include to work you need to do this

PS> Get-ChildItem  -path .\*  -include *.csv | select -First 1

    Directory: C:\MyData\SkyDrive\Data\scripts

Mode                LastWriteTime     Length Name
—-                ————-     —— —-
-a—        12/09/2012     19:07       2665 log.csv

The path has to be given for –Include to return anything

Looking at another folder

These work

Get-ChildItem -Path C:\MyData\SkyDrive\Data\scripts -Filter *.csv

Get-ChildItem -Path C:\MyData\SkyDrive\Data\scripts\* -Filter *.csv

Get-ChildItem -Path C:\MyData\SkyDrive\Data\scripts\* -Include *.csv

Get-ChildItem -Path C:\MyData\SkyDrive\Data\scripts -Include *.csv -Recurse

But these don’t

Get-ChildItem -Path C:\MyData\SkyDrive\Data\scripts -Include *.csv

Get-ChildItem -Path C:\MyData\SkyDrive\Data\scripts\ -Include *.csv

If you want to use –Include remember the path and wildcard or use recurse

under: PowerShell Basics, PowerShell original, Scripting Games

Getting the folder name

Posted by: | April 30, 2013 | No Comment |

Consider the file information from get-childitem

PS> $file = ls servicesv0.6.txt

 

Fullname gives the full path to the file

PS> $file.Fullname
C:\MyData\SkyDrive\Data\scripts\servicesv0.6.txt

 

if you use DirectoryName you get the full path to the parent directory

 

PS> $file.DirectoryName
C:\MyData\SkyDrive\Data\scripts

 

The easiest way to get the folder holding the path is like this

PS> Split-Path -Path $file.DirectoryName -Leaf
scripts

 

However on a Windows 8/PS 3 setup you can also do this

PS> $file.Directory | select -ExpandProperty Name
scripts

under: PowerShell original, Scripting Games

Looking at my first group of entries for Beginners Event 1 I’ve noticed what seems like a fanatical attempt to squash everything onto one line.

command; command; command; command 

is NOT a one liner.  The ; marks the end of a line

command | command | command | command

IS a one liner, even if it goes onto multiple lines!

Don’t think one liner – think one PIPELINE

under: PowerShell original, Scripting Games

Read the Question

Posted by: | April 30, 2013 | No Comment |

When my children were at school and going off for a test or exam the last thing I told them was read the question.

It seems this needs to be repeated for the Scripting Games.  The Beginners Event 1 involved MOVING files from one server to another. I have seen a significant number of answers that are COPYING files.

Close doesn’t count in the Scripting Games.

Please read the question before diving into an answer

under: PowerShell original, Scripting Games

The voting/judging process has started for event 1 in the 2013 Scripting Games.  What I’m going to be doing over the next few weeks is point out some of the things that I’ve noticed when judging. Some will be good things that I think you should adopt; others will be bad things that you should avoid. There even may be some interesting stuff to get you thinking.

First off is the use of the backtick as a line continuation:

Get-Process | `
sort Handles –Descending

You don’t need a backtick at this point.  The pipe symbol acts as a line continuation marker so the backtick is redundant. All you need to do is this:

Get-Process |
sort Handles -Descending

Its the same with commas. You don’t need to do this:

Get-Process |
select Name, Id, `
Handles, CPU

because it works without the backtick

Get-Process |
select Name, Id,
Handles, CPU

You may see backticks used a lot in books but that is an attempt to reduce the width of the code to make it fit the page. We want you to be able to copy the code from the ebook and run it. The alternative is a line continuation marker which you would have to remove.

Bottom line – 99.9% recurring of the time you don’t need backticks as line continuation markers

under: PowerShell original, Scripting Games

The 2013 Scripting Games kicked off during the PowerShell summit.  Event 1 is open and you can submit entries up until 23:59:59 GMT on 29 April 2013.  Voting on the entries starts at at midnight on 30 April.

You can enter and you can vote on the entries.  This is a community games run by powershell.org – all are welcome.

If you haven’t entered yet there is still plenty of time to get you entry in for event 1.  Start by reviewing the information at http://powershell.org/wp/the-scripting-games/

Enjoy and good luck

under: PowerShell original, Scripting Games

Busy, busy, busy

Posted by: | April 13, 2013 | No Comment |

A very busy time coming up in PowerShell land with the first PowerShell Summit kicking off in just over a week’s time.  The 2013 Scripting Games will also be starting very soon.

I’ll try and post about both of them as time allows

under: PowerShell original, PowerShell Summit, Scripting Games

Scripting Games warm up

Posted by: | February 5, 2013 | No Comment |

As a warm up for this years Scripting Games a two event Winter Scripting Camp has been organised.  Details from  http://powershell.org/games/

under: PowerShell original, Scripting Games

Scripting Games 2013

Posted by: | September 21, 2012 | No Comment |

Advanced notice that there will be a Games next year – its changing a bit though.  See http://powershell.org/games/

under: PowerShell original, Scripting Games

An if statement is used to test a condition and if is true do one thing and do another if it is false. It can be written generically as

if (<condition>){ do stuff}
else {do other stuff}

Sometimes we need to test numerous alternatives. We could use multiple if statements, sometimes we have to nest them which can lead to code like this

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |            
foreach {            
 if ($_.FreeSpace -gt 1tb){Write-Host "Over 1TB of disk space free"}            
 else {             
   if ($_.FreeSpace -ge 500gb){Write-Host "Over 500GB of disk space free"}            
   else {            
     if ($_.FreeSpace -ge 300gb){Write-Host "Over 300GB of disk space free"}            
     else {            
      if ($_.FreeSpace -ge 200gb){Write-Host "Over 300GB of disk space free"}            
      else {            
        if ($_.FreeSpace -ge 100gb){Write-Host "Over 100GB of disk space free"}            
        else {            
          Write-Host "Insufficient disk space"            
        }            
     }            
     }            
   }            
 }             
 }

That’s not fun to write and its even less fun to debug or change

The code can be simplified by using if – elseif – else

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |            
foreach {            
 if ($_.FreeSpace -gt 1tb){Write-Host "Over 1TB of disk space free"}            
 elseif ($_.FreeSpace -ge 500gb){Write-Host "Over 500GB of disk space free"}            
 elseif ($_.FreeSpace -ge 300gb){Write-Host "Over 300GB of disk space free"}            
 elseif ($_.FreeSpace -ge 200gb){Write-Host "Over 300GB of disk space free"}            
 elseif ($_.FreeSpace -ge 100gb){Write-Host "Over 100GB of disk space free"}            
 else{Write-Host "Insufficient disk space"}            
 }

This much more compact and understandable. We can do a better job if we use a switch statement which is similar in concept to if-elseif-else

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |            
foreach {            
 switch ($_.FreeSpace) {            
 {$_ -gt 1tb}   {Write-Host "Over 1TB of disk space free"; break}            
 {$_ -ge 500gb} {Write-Host "Over 500GB of disk space free"; break}            
 {$_ -ge 300gb} {Write-Host "Over 300GB of disk space free"; break}            
 {$_ -ge 200gb} {Write-Host "Over 300GB of disk space free"; break}            
 {$_ -ge 100gb} {Write-Host "Over 100GB of disk space free"; break}            
 default {Write-Host "Insufficient disk space"}            
 }            
 }

We select the object to test and use $_ as a placeholder. Each line in the switch statement is a single test. The default statement is for anything that doesn’t pass any of the preceding tests

One difference between the if statements and switch is that with the if statements as soon as a condition is true testing stops. With switch testing would continue through all tests – break is used to stop that

When you need to perform multiple tests consider using a switch statement – it is less typing and easier to debug

under: PowerShell original, Scripting Games

« Newer Posts - Older Posts »

Categories