none
PowerShell - Alert on File Creation RRS feed

  • Question

  • I created this PowerShell script that runs on our web server to alert when a file is created in a folder (C:\webfolder\dumps). The purpose is one of our web services from a third party errors out when a file is created. I created an alert to show on a Cyfe dashboard using iFrame that refreshes every 10 seconds. When there is a file, the background color on the html page is red, otherwise it's green. One thing I noticed is that it seems the html file is deleted, then recreated every time. Is my code wrong? I know it's horrible but I suck at PowerShell and this was hodgepodged, but it works.

    $StyleBad = "
    <style>
        body {color: white;
        background-color: red;}
    </style>
    "
    
    $StyleGood = "
    <style>
        body {color: white;
        background-color: green;}
    </style>
    "
    
    
    while ($GetCount -le 99) {
    $GetCount = Get-ChildItem  C:\webfolder\dumps -Recurse -File | Measure-Object | %{$_.Count}
    
    If ($GetCount -le 0) {
    ConvertTo-HTML -head $StyleGood -PostContent $GetCount | Out-File C:\website\dumps\index.html
    }
    
    else {
    ConvertTo-HTML -head $StyleBad -PostContent $GetCount | Out-File C:\website\dumps\index.html
    }
    }

    Monday, March 2, 2020 10:03 PM

Answers

  • This is how to code this in PowerShell.  Your method has many holes in it.

    $css = @'
    <style "text/css">
        body{{
            color: white;
            background-color: {0};
        }}
    </style>
    '@
    
    while($true){
        $color = if($count = (Get-ChildItem  C:\webfolder\dumps -Recurse -File).Count){
            'red'
        }else{
            'green'
        }
        $style = $css -f $color
        ConvertTo-HTML -head $style -PostContent $count | Out-File C:\website\dumps\index.html
        if($count -gt 99){break}
    sleep 5 }


    \_(ツ)_/



    Monday, March 2, 2020 11:24 PM

All replies

  • This is how to code this in PowerShell.  Your method has many holes in it.

    $css = @'
    <style "text/css">
        body{{
            color: white;
            background-color: {0};
        }}
    </style>
    '@
    
    while($true){
        $color = if($count = (Get-ChildItem  C:\webfolder\dumps -Recurse -File).Count){
            'red'
        }else{
            'green'
        }
        $style = $css -f $color
        ConvertTo-HTML -head $style -PostContent $count | Out-File C:\website\dumps\index.html
        if($count -gt 99){break}
    sleep 5 }


    \_(ツ)_/



    Monday, March 2, 2020 11:24 PM
  • Also writing to the index.html file on a web server is an issue as that is a special file.  You should use aa different name.  While the file is being parsed by the server it can be locked causing a crash of the code and no file.

    Running in a loop with no throttling can cause issues on a web server. the delay should be used.


    \_(ツ)_/

    Monday, March 2, 2020 11:27 PM
  • Thank you for the suggestion. Because I'm new to PowerShell any advice helps. I tried running the code and it seems something is wrong with this line:  $style = $css -f $color
    Tuesday, March 3, 2020 5:10 PM
  • Sorry - I forgot that the brackets need to be escaperd.

    The following will work:

    $css = @'
    <style "text/css">
        body{{
            color: white;
            background-color: {0};
        }}
    </style>
    '@

    All brackets that are not part of a token "{0}" must be doubled "{{" and "}}".


    \_(ツ)_/


    • Edited by jrv Tuesday, March 3, 2020 5:45 PM
    Tuesday, March 3, 2020 5:44 PM
  • That worked, thank you. If you don't mind but I would like to understand the coding in these specific areas:

    1. $css = @' What does the @ symbol do at the start of the variable and at the end?

    2. $style = $css -f What does the -f do? Based on what I looked up, it looks like its a formatting switch.

    3. Where is the throttle in the script? I did notice in my code that the CPU was running between 20-38% where as this runs at 1-8%. Nice difference!

    4. In the while loop, you list 'red' and then 'green'. How does it know those are css colors?

    Monday, March 9, 2020 1:26 PM
  • It is called a "Here String".  Search for articles on how to use it.

    "-f" is the string format operator. It is the equivalent of "sprintf" in C/C++.

    [string]::Format(....)

    These questions are why you need to learn basic PowerShell 101.  They are the first things you will learn along with all of the required basics needed to use PowerShell.


    \_(ツ)_/

    Monday, March 9, 2020 7:13 PM