简体   繁体   English

如何使 -AutoRefresh 命令在 powershell 通用仪表板脚本中工作

[英]How to make the -AutoRefresh command work in a powershell universal dashboard script

I managed to create a script/dashboard that monitors a log in a file for a keyword which then creates a log of it's own.我设法创建了一个脚本/仪表板来监视文件中的关键字日志,然后创建它自己的日志。 I say managed because I have little to know coding skills.我说托管是因为我对编码技能知之甚少。 The problem I am having at the moment, is that while the script works, it doesn't actively monitor the file.我目前遇到的问题是,虽然脚本有效,但它并没有主动监视文件。 I would like it to continuously look for this keyword indefinitely without reloading the dashboard.我希望它在不重新加载仪表板的情况下无限期地持续查找此关键字。

The code:编码:

Get-UDDashboard | Stop-UDDashboard
Start-UDDashboard -Port 1000 -Dashboard (
    New-UDDashboard -Title “SERVER STATUS DASHBOARD” -Content {
        New-UDHeading -Text "ACTIVE SERVICES" -Size 5   
        New-UDLayout   -Columns 6 -Content {  
            New-UDCard -Title 'Server Status' -Content {
   
                $SEL = Select-String -Path 'C:\Users\Server_log.txt' -Pattern "Server went down"

                if ($SEL -ne $null)
                {
                    Write-Host 'Contains String' -ForegroundColor Green
                    New-UDParagraph -Text 'Server is DOWN'-Color red

                } 

                else
                {
                    Write-Host 'Not Contains String' -ForegroundColor Red
                    New-UDParagraph -Text 'Server is UP'-Color Green
                }  
            } -AutoRefresh -RefreshInterval 5
        } 
    } 
 )

After scouring the internet, I added a "-AutoRefresh RefreshInterval 5" to no avail.在互联网上搜索后,我添加了一个“-AutoRefresh RefreshInterval 5”无济于事。 The error I received was as follows:我收到的错误如下:

***New-UDDashboard : Exception calling "Invoke" with "0" argument(s): "A parameter cannot be found that matches parameter name 'AutoRefresh'."
At C:\Users\Server_Status2.ps1:3 char:5
+     New-UDDashboard -Title “SERVER STATUS DASHBOARD” -Content ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SyntaxError: (UniversalDashboard.Models.Dashboard:Dashboard) [New-UDDashboard], CmdletInvocationException
    + FullyQualifiedErrorId : UniversalDashboard.Cmdlets.NewDashboardCommand***

Originally I assumed that the error was an indication that the cmdlet named "AutoRefresh" was not in my powershell environment.最初我认为该错误表明名为“AutoRefresh”的 cmdlet 不在我的 powershell 环境中。 So I tried a script with that command in it that I knew worked, and it did actually work in my environment.所以我尝试了一个脚本,其中包含我知道有效的命令,它确实在我的环境中有效。 So I know my environment does support "AutoRefresh".所以我知道我的环境确实支持“自动刷新”。 I believe the syntax is correct so i think that just leaves improper code structure to be the issue.我相信语法是正确的,所以我认为这只是留下不正确的代码结构成为问题。

Any help would be appreciated.任何帮助,将不胜感激。 Thanks谢谢

Based on the code you've posted, the -Autorefresh argument is being applied to the New-UDCard cmdlet (if I'm counting braces correctly).根据您发布的代码, -Autorefresh参数正在应用于New-UDCard cmdlet(如果我正确计算大括号)。 Looking at the help page the I found , this cmdlet doesn't support an autorefresh argument.查看我发现的帮助页面,此 cmdlet 不支持autorefresh参数。

THe only cmdlet in that module that seems to support autorefresh is New-UDElement which you don't seem to be calling.该模块中似乎支持自动刷新的唯一 cmdlet 是您似乎没有调用的New-UDElement autorefresh

I'm afraid I don't know enough (or anything at all really) about this module you are using to help you further, but hopefully this helps in your debugging.恐怕我对您用来进一步帮助您的这个模块了解不够(或根本不知道),但希望这有助于您的调试。

So after a little helpful hint from zdan and some more scouring on the web, I was able to resolve my issue.因此,在 zdan 提供了一些有用的提示并在网上进行了一些更深入的搜索之后,我能够解决我的问题。 I was able to get the AutoRefresh RefreshInterval parameter working with the new-UDCard cmdlet by using New-UDRow and New-UDColumn as shown below:通过使用 New-UDRow 和 New-UDColumn,我能够使 AutoRefresh RefreshInterval 参数与 new-UDCard cmdlet 一起使用,如下所示:

Get-UDDashboard | Stop-UDDashboard
Start-UDDashboard -Port 1000 -Dashboard (
    New-UDDashboard -Title “SERVER STATUS DASHBOARD” -Content {
        New-UDHeading -Text "ACTIVE SERVICES" -Size 5   
        New-UDLayout   -Columns 6 -Content {  
            
            New-UDRow -Endpoint {

            New-UDColumn -Content {
            
            New-UDCard -Title 'Server Status' -Content {
   
                $SEL = Select-String -Path 'C:\Users\Server_log.txt' -Pattern "Server went down"

                if ($SEL -ne $null)
                {
                    Write-Host 'Contains String' -ForegroundColor Green
                    New-UDParagraph -Text 'Server is DOWN'-Color red

                } 

                else
                {
                    Write-Host 'Not Contains String' -ForegroundColor Red
                    New-UDParagraph -Text 'Server is UP'-Color Green
                }  
            } 
          } -AutoRefresh -RefreshInterval 5
        } 
       }
      }
 )

Hopefully, if anyone else wants to do the same, maybe this can help them as well.希望,如果其他人也想做同样的事情,也许这也可以帮助他们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM