Powershell run on aws machine

RITA DE CASSIA VIEIRA DA CRUZ 21 Reputation points
2020-10-01T01:42:01.26+00:00

Hi,
I'm a beginner!
I would like to know if it is possible to run a PowerShell script on my machine since the data is on an AWS server. The script is simple but it doesn't run.
Get-ChildItem -Path "\awsdisk\PDF\ ExecutionLogs\pdf.log" -Recurse | where-Object {$ _. LastWriteTime -gt '2020-09-06 00:00:00' -and $ _. LastWriteTime -lt '2020-09-21 00:00:00'} | Select-String -Pattern 'Error happened during downloading test components: due to:' -CaseSensitive -SimpleMatch | Out-File c:\ ritaaws\result01.txt

To test the script I used my machine's c:\ disk, and the result was instantaneous.

I ask for your help, is the command line wrong? To run a Powershell script on an AWS server do I need to add any parameters to the script? I am waiting for your answer. Thank you, Rita

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,560 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,896 Reputation points
    2020-10-04T23:27:22.127+00:00

    I think you've misunderstood the example I posted. The code in the 1st example:

    $x=gci C:\junk\evt.xml
    ($x.lastwritetime).gettype()

    Was intended to show you that the LastWriteTime isn't a string object, but a DateTime object and that trying to use a string when attempting to compare the value of a DateTime object is incorrect.

    Your script should look something like this:

    Get-ChildItem -Path \\awsdisk\PDF\ExecutionLogs -Recurse -File | 
        Where-Object {($_.LastWriteTime -gt [DateTime]'2020-09-06 00:00:00') -and ($_.LastWriteTime -lt [DateTime]'2020-09-21 00:00:00')} | 
            Select-String -Pattern 'Error happened during downloading test components: due to:' -CaseSensitive -SimpleMatch | 
                Out-File c:\ritaaws\result01.txt
    
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Ian Xue 37,711 Reputation points Microsoft Vendor
    2020-10-01T03:52:19.77+00:00

    Hi,

    Please use $_ to represent the current object. The property should be $_.LastWriteTime, not $.LastWriteTime.

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. RITA DE CASSIA VIEIRA DA CRUZ 21 Reputation points
    2020-10-01T11:34:32.683+00:00

    Hi Ian,
    Thank you for your answer. Sorry, I didn't notice that when copying and pasting that the property had become incorrect. I am executing the script with the property written correctly, it is displayed in green.
    Get-ChildItem -Path "\awsdisk\PDF\ ExecutionLogs\pdf.log" -Recurse | where-Object {$. LastWriteTime -gt '2020-09-06 00:00:00' -and $. LastWriteTime -lt '2020-09-21 00:00:00'} | Select-String -Pattern 'Error happened during downloading test components: due to:' -CaseSensitive -SimpleMatch | Out-File c:\ ritaaws\result01.txt
    I tried to make the copy-paste again and it doesn't appear as a property. When editing the text it appears, but when saved it is not displayed as a property. Can you tell me what I need to do? Please see the image29642-image.png
    Best Regards, Rita

    0 comments No comments

  3. Rich Matheisen 46,896 Reputation points
    2020-10-01T14:22:59.55+00:00

    The property "LastWriteTime" isn't a string and you shouldn't be using string comparisons with such objects.

    It you were to examine that property on one of those files you'd see:

    $x=gci C:\junk\evt.xml
    ($x.lastwritetime).gettype()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     DateTime                                 System.ValueType
    

    Continuing with that file above, casting your string to a DateTime object produces the desired results:

    $x.lastwritetime -lt [DateTime]"10/1/2020 00:00:00"
    False
    
    $x.lastwritetime -lt [DateTime]"10/1/2020 11:00:00"
    True
    

    In the future, if you're going to post code, please use the icon in the formatting toolbar. It's the one that has the "101" and "010" image. That'll make your code much more readable.

    0 comments No comments

  4. RITA DE CASSIA VIEIRA DA CRUZ 21 Reputation points
    2020-10-04T22:20:42.327+00:00

    Hi RichMatheisen,
    I appreciate your answer and your attention to my problem. Thank you.
    This is my first script after taking the course. But based on your answer, I'm rewriting the script and testing, step by step.
    I was able to verify that I can read the folders and subfolders and list the pdf.log file, without the filters until this moment.
    I was unable to apply this suggested part of the code to my script $x=gci "\\awsdisk\PDF\ExecutionLogs\" ($x.lastwritetime).gettype(), I tried it in many ways but without success.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.