Get remote server info

Hi, I’m trying to get information from remote servers, but I can’t see the information in the dashboard. The server name is entered through textbox.

# Start the dashboard
New-UDDashboard -Title "Remote Server Info Dashboard" -Pages @(
    New-UDPage -Name "Server Info" -Content {
        # Input box for server name
        New-UDTextbox -Id "ServerNameInput" -Placeholder "Enter Server Name"

        # Button to retrieve server information
        New-UDButton -Text "Fetch Server Info" -OnClick {
            $ServerName = (Get-UDElement -Id "ServerNameInput").Value

            # Notify the user that the process is starting
            Show-UDToast -Message "Fetching information for server: $ServerName" -Duration 3000

            # Retrieve server information using Invoke-Command
            try {
                $OS = Invoke-Command -ComputerName $ServerName -ScriptBlock {
                    Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty Caption
                }
                $CPU = Invoke-Command -ComputerName $ServerName -ScriptBlock {
                    Get-CimInstance -ClassName Win32_Processor | Select-Object -ExpandProperty Name
                }
                $RAM = Invoke-Command -ComputerName $ServerName -ScriptBlock {
                    Get-CimInstance -ClassName Win32_ComputerSystem | 
                    Select-Object @{Name="TotalMemoryGB";Expression={[math]::Round($_.TotalPhysicalMemory / 1GB, 2)}}
                }
                $Disks = Invoke-Command -ComputerName $ServerName -ScriptBlock {
                    Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 } |
                    Select-Object @{Name="DeviceID";Expression={$_.DeviceID}}, @{Name="SizeGB";Expression={[math]::Round($_.Size / 1GB, 2)}}
                }
                $Patches = Invoke-Command -ComputerName $ServerName -ScriptBlock {
                    Get-CimInstance -ClassName Win32_QuickFixEngineering | Select-Object -ExpandProperty HotFixID
                }

                # Combine results
                $ServerInfo = @(
                    [PSCustomObject]@{ Property = "Operating System"; Value = $OS }
                    [PSCustomObject]@{ Property = "CPU"; Value = $CPU }
                    [PSCustomObject]@{ Property = "RAM (GB)"; Value = $RAM.TotalMemoryGB }
                    [PSCustomObject]@{ Property = "Disk Details"; Value = ($Disks | ForEach-Object { "$($_.DeviceID): $($_.SizeGB) GB" }) -join ", " }
                    [PSCustomObject]@{ Property = "Patches"; Value = ($Patches -join ", ") }
                )

                # Update the table dynamically
                Sync-UDElement -Id "ServerInfoTable" -Content {
                    New-UDTable -Title "Server Information" -Data $ServerInfo -Columns @(
                        New-UDTableColumn -Property Property -Title "Property"
                        New-UDTableColumn -Property Value -Title "Value"
                    )
                }
            } catch {
                Show-UDToast -Message "Error retrieving server info: $_" -Type Error -Duration 5000
            }
        }

        # Placeholder for the table
        New-UDElement -Id "ServerInfoTable" -Tag "div"
    }
)

Product: PowerShell Universal
Version: 5.4.4

Have done similar a few times, but used a slightly different pattern using a Dynamic region (https://docs.powershelluniversal.com/apps/components/utilities/dynamic-regions)

so Id have this inside the OnClick

                # Combine results
                $Page:ServerInfo = @(
                    [PSCustomObject]@{ Property = "Operating System"; Value = $OS }
                    [PSCustomObject]@{ Property = "CPU"; Value = $CPU }
                    [PSCustomObject]@{ Property = "RAM (GB)"; Value = $RAM.TotalMemoryGB }
                    [PSCustomObject]@{ Property = "Disk Details"; Value = ($Disks | ForEach-Object { "$($_.DeviceID): $($_.SizeGB) GB" }) -join ", " }
                    [PSCustomObject]@{ Property = "Patches"; Value = ($Patches -join ", ") }
                )
                # Update the table dynamically
                Sync-UDElement -Id "ServerInfoTable" 

And this for the display

                New-UDDynamic -Id "ServerInfoTable" -Content {
                    if($Page.ServerInfo)
                    {
                        New-UDTable -Title "Server Information" -Data $Page:ServerInfo -Columns @(
                            New-UDTableColumn -Property Property -Title "Property"
                            New-UDTableColumn -Property Value -Title "Value"
                        )
                    }
               }

I finally solved it with this code, but I did it again from scratch.

New-UDApp -Content {
    New-UDTextbox -Id "serverName" -Label "Nombre del Servidor"
    New-UDButton -Text "Obtener Información" -OnClick {
        $server = Get-UDElement -Id "serverName" -Property Value

        $fecha = (Get-Date).ToString("dd-MM-yyyy-HH-mm-ss")

        if ([string]::IsNullOrWhiteSpace($server)) {
            Show-UDModal -Content {
                New-UDTypography -Text "Por favor, ingrese un nombre de servidor válido." -Variant h6
            }
            return
        }

        Show-UDModal -Content {
            New-UDProgress -Id "progressBar"
            New-UDTypography -Text "Cargando información Desde $server, por favor espere..." -Variant h6
        }

        try {
            Sync-UDElement -Id "progressBar" -ArgumentList 20
            Start-Sleep -Seconds 1

            $scriptBlock = {
                $osInfo = Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object Caption, TotalVisibleMemorySize
                $processorInfo = Get-CimInstance -ClassName Win32_Processor | Select-Object -First 1 Name, NumberOfCores
                $activeUsers = Get-LocalUser | Where-Object {$_.Enabled} | Select-Object Name, Enabled
                $installedPrograms = Get-CimInstance -ClassName Win32_Product | Select-Object Name, Version, Vendor
                $autoServices = Get-CimInstance -ClassName Win32_Service | Where-Object {$_.StartMode -eq 'Auto'} | Select-Object DisplayName, State, StartMode
                $diskInfo = Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object {$_.DeviceID -ne 'U:'} | Select-Object DeviceID, @{Name = "SizeGB"; Expression = { [math]::Round($_.Size / 1GB, 2) } }, @{Name = "FreeSpaceGB"; Expression = { [math]::Round($_.FreeSpace / 1GB, 2) } }
                $hotfixes = Get-HotFix | Select-Object Description, HotFixID, InstalledOn

                return @{
                    OSInfo            = $osInfo
                    ProcessorInfo     = $processorInfo
                    ActiveUsers       = $activeUsers
                    InstalledPrograms = $installedPrograms
                    AutoServices      = $autoServices
                    DiskInfo          = $diskInfo
                    HotFixes          = $hotfixes
                }
            }

            Sync-UDElement -Id "progressBar" -ArgumentList 50
            Start-Sleep -Seconds 2

 
            Sync-UDElement -Id "progressBar" -ArgumentList 100
            Start-Sleep -Seconds 1

            Hide-UDModal

            Show-UDModal -Content {
                # Nuevo título dinámico con el nombre del servidor
                New-UDTypography -Text "Información del Servidor $server" -Variant h4
                
                # Información General
                New-UDTypography -Text "Información General" -Variant h5
                New-UDTable -Id "generalTable" -Columns @(
                    New-UDTableColumn -Property "Propiedad" -Title "Propiedad"
                    New-UDTableColumn -Property "Valor" -Title "Valor"
                ) -Data @(
                    [PSCustomObject]@{ Propiedad = "Sistema Operativo"; Valor = $results.OSInfo.Caption }
                    [PSCustomObject]@{ Propiedad = "Procesador"; Valor = $results.ProcessorInfo.Name }
                    [PSCustomObject]@{ Propiedad = "Número de Cores"; Valor = $results.ProcessorInfo.NumberOfCores }
                    [PSCustomObject]@{ Propiedad = "RAM Total"; Valor = "$([math]::Round($results.OSInfo.TotalVisibleMemorySize / 1MB, 2)) GB" }
                )

                # Usuarios Locales Activos
                New-UDTypography -Text "Usuarios Locales Activos" -Variant h5
                New-UDTable -Id "usersTable" -Columns @(
                    New-UDTableColumn -Property "Name" -Title "Nombre de Usuario"
                    New-UDTableColumn -Property "Enabled" -Title "Estado"
                ) -Data $results.ActiveUsers

                # Parches Instalados con Filtro para KB
                New-UDTypography -Text "Parches Instalados" -Variant h5
                New-UDTable -Id "hotfixesTable" -Columns @(
                    New-UDTableColumn -Property "HotFixID" -Title "HotFixID" -ShowFilter
                    New-UDTableColumn -Property "Description" -Title "Descripción"
                    New-UDTableColumn -Property "InstalledOn" -Title "Fecha de Instalación"
                ) -Data $results.HotFixes

                # Programas Instalados
                New-UDTypography -Text "Programas Instalados" -Variant h5
                New-UDTable -Id "programsTable" -Columns @(
                    New-UDTableColumn -Property "Name" -Title "Nombre del Programa"
                    New-UDTableColumn -Property "Version" -Title "Versión"
                    New-UDTableColumn -Property "Vendor" -Title "Proveedor"
                ) -Data $results.InstalledPrograms

                # Servicios Automáticos Configurados
                New-UDTypography -Text "Servicios Automáticos Configurados" -Variant h5
                New-UDTable -Id "servicesTable" -Columns @(
                    New-UDTableColumn -Property "DisplayName" -Title "Nombre del Servicio"
                    New-UDTableColumn -Property "State" -Title "Estado"
                    New-UDTableColumn -Property "StartMode" -Title "Modo de Inicio"
                ) -Data $results.AutoServices

                # Botón para descargar en HTML
                New-UDButton -Text "Descargar en HTML" -OnClick {
                    $htmlContent = @"
                    <html>
                    <head><title>Información del Servidor $server</title></head>
                    <body>
                        <h3>Información del Servidor $server</h3>
                        <h3>Información General</h3>
                        <table border='1'>
                            <tr><th>Propiedad</th><th>Valor</th></tr>
                            <tr><td>Sistema Operativo</td><td>$($results.OSInfo.Caption)</td></tr>
                            <tr><td>Procesador</td><td>$($results.ProcessorInfo.Name)</td></tr>
                            <tr><td>Número de Cores</td><td>$($results.ProcessorInfo.NumberOfCores)</td></tr>
                            <tr><td>RAM Total</td><td>$([math]::Round($results.OSInfo.TotalVisibleMemorySize / 1MB, 2)) GB</td></tr>
                        </table>
                    </body>
                    </html>
"@

                    Start-UDDownload -StringData $htmlContent -FileName "ServerInfo-$fecha.html" -ContentType "text/html"
                }

                # Botón de impresión usando JavaScript
                New-UDButton -Text "Imprimir" -OnClick {
                    Invoke-UDJavaScript -JavaScript 'window.print();'
                }
            }
        } catch {
            Hide-UDModal
            Show-UDModal -Content {
                New-UDTypography -Text "Error al conectar con el servidor $server." -Variant h6
                New-UDTypography -Text "$($_.Exception.Message)" -Variant h6
            }
        }
    }
}

1 Like