locked
Scripting Issue - New to powershell RRS feed

  • Question

  • Hello All,

    I have wrote the below script which is giving me errors and I am unable to figure out how to debug. I am new to Powershell and still learning, any help will be appreciated.

    ===========

    param(
         [string]$path,
         [int]$days
    )

    $date = ((Get-Date).AddDays(-$days)).ToShortDateString()
    $files = Get-ChildItem -Path $path | Where-Object LastWriteTime -LT $date

    $filecount = 0

    if ($files.Length -lt 1){
        $filecount = 0
    } elseif ($files-isnot [array]){
        $filecount = 1
    } else {
        $filecount = $files.Length
    }

    "`n The following " + $filecount + " file(s) have not been modified since $date"

    $files

    =======

    Error: 

    PS C:\Users\testuser1\Desktop\Powershell Scripting> & '.\4.21 - Writing another script.ps1' -path C:\Windows -days 9
    Where-Object : The 'Ilt' operator failed: Could not compare "09/15/2018 19:33:54" to "17/05/2019". Error: "Cannot convert value 
    "17/05/2019" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."".
    At C:\Users\testuser1\Desktop\Powershell Scripting\4.21 - Writing another script.ps1:7 char:38
    + ... es = Get-ChildItem -Path $path | Where-Object LastWriteTime -LT $date
    +                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (addins:PSObject) [Where-Object], PSInvalidOperationException
        + FullyQualifiedErrorId : OperatorFailed,Microsoft.PowerShell.Commands.WhereObjectCommand

    Sunday, May 26, 2019 4:43 AM

All replies

  • Just read the error message.  You can't compare a date to a string.  You must use both as dates.

    Note that we do not do homework problems.  If you are having issues with learning then ask your instructor for help.


    \_(ツ)_/

    Sunday, May 26, 2019 5:14 AM
  • Hello There,

    Thank you so much for responding, the problem here is I am self teaching myself, I can not afford to have an instructor. Anyways I was struggling to read and understand the error message, mostly struggling with how to fix it after reading it. Do you know of any other portal where I can ask questions?

    Many thanks

    Vinay

    Sunday, May 26, 2019 7:04 AM
  • The error message is plain English. Do you read English?

    You cannot compare a date object to a string.

    You need to start by taking a turorial or with a book.  You cannot learn by guessing.

    Start here its free:

    Microsoft Virtual     Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/

    Sunday, May 26, 2019 7:08 AM
  • Well you can read what I am writing so the answer to your question is obvious. Thank you so much for telling me what the problem is :) I will visit the link you gave me and start from there :)
    Sunday, May 26, 2019 7:20 AM
  • You error tells you that this is wrong:

    Where-Object : The 'Ilt' operator failed: Could not compare "09/15/2018 19:33:54" to "17/05/2019". Error: "Cannot convert value 
    "17/05/2019" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."".

    Your date format when the date is converted is not usable as a string.  This is one reason to not compare strings with dates.  Do not use a string.  Use the date objects only.

    If you do the tutorial from the beginning to the end and learn every step until you understand this kind of issue will become obvious.

    Before trying to learn PowerShell you will need to have a basic knowledge of programming.

    Here is a good place to learn: https://studio.code.org/courses


    \_(ツ)_/

    Sunday, May 26, 2019 7:21 AM
  • Well you can read what I am writing so the answer to your question is obvious. Thank you so much for telling me what the problem is :) I will visit the link you gave me and start from there :)

    Many come here and use a translator.  The translations do not always work with technical material.  Translations of errors is very bad or impossible. You didn't seem to understand the error which made me ask if English was your language.

    \_(ツ)_/

    Sunday, May 26, 2019 7:24 AM
  • Hi Vinay,

    As jrv said the problem is one side you have string even other side you have not.

    So i suggest do not use .ToShortDateString() in your script hence the new script should look like below: -

    ---------------------------------

    param(
         [string]$path,
         [int]$days
    )

    $date = ((Get-Date).AddDays(-$days))
    $files = Get-ChildItem -Path $path | Where-Object LastWriteTime -LT $date

    $filecount = 0

    if ($files.Length -lt 1){
        $filecount = 0
    } elseif ($files-isnot [array]){
        $filecount = 1
    } else {
        $filecount = $files.Length
    }

    "`n The following " + $filecount + " file(s) have not been modified since $date"

    $files

    -------------------------------------

    I have tried this script and i could see its working fine. Please find below screenshot: -

    Hope this helps you :)


    Cheers, Gourav Please remember to mark the replies as answers if it helped.

    • Proposed as answer by GouravIN Sunday, May 26, 2019 8:40 AM
    Sunday, May 26, 2019 7:38 AM