PowerShell-文件创建警报

时间:2020-03-02 23:23:19

标签: powershell

我创建了此PowerShell脚本,该脚本在Web服务器上运行,以在文件夹(C:\ folder \ dumps)中创建文件时发出警报。目的是我们的Web服务之一从第三方创建文件时出错。我创建了一个警报,该警报使用iFrame每10秒刷新一次,以显示在Cyfe仪表板上。如果有文件,则html页面上的背景颜色为红色,否则为绿色。我注意到的一件事是,似乎html文件已删除,然后每次都重新创建。我的代码错了吗?我知道这太可怕了,但是我对PowerShell感到厌倦,但是这很容易,但是确实有效。

$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
}
}

1 个答案:

答案 0 :(得分:0)

谢谢培根。我从另一个来源获得了此代码。这段代码效果更好。

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