Home > Software design >  How to make the -AutoRefresh command work in a powershell universal dashboard script
How to make the -AutoRefresh command work in a powershell universal dashboard script

Time:07-16

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. 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. 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

CodePudding user response:

Based on the code you've posted, the -Autorefresh argument is being applied to the New-UDCard cmdlet (if I'm counting braces correctly). Looking at the help page the I found, this cmdlet doesn't support an autorefresh argument.

THe only cmdlet in that module that seems to support autorefresh is New-UDElement which you don't seem to be calling.

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.

CodePudding user response:

So after a little helpful hint from zdan and some more scouring on the web, I was able to resolve my issue. 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:

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.

  • Related