locked
Help needed using a PowerScript Tutorial please RRS feed

  • Question

  • Hi

    I'm just following this tutorial PowerShell check folder for modified files in the last X minutes which crashes when I get to line 5 :-

    $xminutes="60"
    
    $xPath="c:\temp\*.*"
    
     $xfile = Get-ChildItem -Path $xPath -Filter *.txt, *.xml | ? {
      $_.LastWriteTime -gt (Get-Date).AddMinutes(-$xminutes) } 
    
    Write-Host $xfile

    The error is :-

    Get-ChildItem : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Filter'.

    Specified method is not supported.

    At line:5 char:46

    +  $xfile = Get-ChildItem -Path $xPath -Filter *.txt, *.xml | ? {

    +                                              ~~~~~~~~~~~~

        + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException

        + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.GetChildItemCommand

    I'm very new to Powerscript(v4 installed) and I get confused where the pipe and question mark appear. Can anyone help to correct this line for me please.

    Kind Regards

    Matthew

    Wednesday, April 30, 2014 10:55 AM

Answers

  • Try it with "-Include" instead of "-Filter", maybe that works.
    • Marked as answer by jmatty2000 Wednesday, April 30, 2014 11:48 AM
    Wednesday, April 30, 2014 11:12 AM

All replies

  • Try it with "-Include" instead of "-Filter", maybe that works.
    • Marked as answer by jmatty2000 Wednesday, April 30, 2014 11:48 AM
    Wednesday, April 30, 2014 11:12 AM
  • Yes, that fixes it :)

    Thanks very much - I'll let the article owner know also.

    Wednesday, April 30, 2014 11:22 AM
  • Issue is already fixed, thats fine.

    Additional info:

    (1) With  $xminutes="60" you define a string, not number.  AddMinutes() requires a number (a double value).
    Powershell automatically converts your string into a number before handing it over to AddMinutes.
    It is better to write

    $xminutes=60

    (2) The  ?  is a short alias for  Where-Object . I avoid "?" and use the "longer" alias  Where  instead: It still is shorter than "where-object" but it is clear and easier to read.
    The same with "%":

    %=foreach=ForEach-Object

    I'm used to avoid  % , I prefer foreach.

    Walter

    Wednesday, April 30, 2014 3:17 PM