当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 我的遥测PowerShell提示功能

作者:精品下载站 日期:2024-12-14 07:53:48 浏览:12 分类:玩电脑

我的遥测PowerShell提示功能


我很喜欢构建这些 PowerShell 提示函数。因为性能至关重要,所以它迫使我创建高效的代码。例如,我一直在使用 Get-CimInstance 从某些类中检索选定的属性。因为我事先知道我需要什么属性,所以我可以告诉 Get-Ciminstance 仅获取并返回这些属性。对于今天的版本来说,这一点非常重要。这种“遥测”提示将先前迭代中的许多功能组合成一些关键服务器状态的更全面的快照。

该提示功能还依赖于运行空间和同步哈希表从少量远程服务器获取系统信息。您可以即时修改列表。我建议将其保持在最低限度,除非您使控制台屏幕更宽或修改功能。顺便说一下,如果没有找到默认文件 C:\Scripts\watch.txt,则提示符将默认为 localhost。假设列表存在并且所有服务器都已启动并运行,您可能会收到如下提示:

[玩转系统] 我的遥测PowerShell提示功能

正如您所看到的,有一些颜色编码。我在一些值前面使用了一些图形元素来帮助您解读提示。当然,您可以随意修改,如您所愿。

该功能还将考虑服务器是否离线。

[玩转系统] 我的遥测PowerShell提示功能

当服务器恢复时,一切都恢复了。

[玩转系统] 我的遥测PowerShell提示功能

该函数以及其他函数的代码位于 Github 上。

TelemetryPrompt.ps1:

#requires -version 5.1
#this requires a windows platform

Function prompt {

    $charHash = @{
        Up   = 0x25b2 -as [char]
        Down = 0x25bc -as [char]
    }

    Try {
        #verify there is a global hashtable variable
        Get-Variable -Name rsHash -Scope global -ErrorAction Stop | Out-Null
    }
    Catch {
        #create the runspace and synchronized hashtable
        $global:rsHash = [hashtable]::Synchronized(@{Computername = $env:computername; results = ""; date = (Get-Date); computers = @()})
        $newRunspace = [runspacefactory]::CreateRunspace()
        #set apartment state if available
        if ($newRunspace.ApartmentState) {
            $newRunspace.ApartmentState = "STA"
        }
        $newRunspace.ThreadOptions = "ReuseThread"         
        $newRunspace.Open()
        $newRunspace.SessionStateProxy.SetVariable("rsHash", $rsHash)  

        $pscmd = [PowerShell]::Create().AddScript( {                      

            #define the path to the list of computers
            $script:listPath = "C:\scripts\watch.txt"
            #define scriptblock to run remotely
                $sb = {

                    Try {        
                        $mem = Get-CimInstance -ClassName win32_operatingsystem -Property TotalVisibleMemorySize, FreePhysicalMemory -ErrorAction Stop -OperationTimeoutSec 2
                        $memUsage = ($mem.FreePhysicalMemory / $mem.TotalVisibleMemorySize) * 100 -as [int]
                        $disk = Get-CimInstance -Classname win32_logicaldisk -filter "deviceID = 'c:'" -Property DeviceID, Size, FreeSpace -ErrorAction Stop -OperationTimeoutSec 2
                        $diskFree = ($disk.FreeSpace / $disk.Size) * 100 -as [int]
                        #get a count of all processes except idle and system and subtract 1 for the remote connection
                        $proc = (Get-Process).Where({$_.name -notmatch 'Idle|system'}).count -1
                        $os = Get-CimInstance -ClassName win32_operatingsystem -Property lastbootupTime
                        $isUp = $True
                    }
                    catch {
                        #this will probably never be used
                        $memUsage = 0
                        $diskFree = 0
                        $isUp = $False
                        Processes = 0
                        OS = "" 
                    }
                    [pscustomobject]@{
                        Computername = $env:computername
                        MemoryUsage  = $memUsage
                        DiskFree     = $diskFree
                        Processes    = $proc
                        IsUp         = $IsUp
                        Uptime       = ((Get-Date) - $os.LastBootUpTime).toString("d\.hh\:mm\:ss")
                    }
                } #end scriptblock
                    
                #define a pssession option to speed up connections
                $opt = New-PSSessionOption -OpenTimeout 1000 -MaxConnectionRetryCount 1

                do {
                    #define the list of computers to test
                    #filter out blank lines and trim any spaces to clean up names.
                    #This should be a short list unless you increase the width of your console sessions
                    if (Test-Path -path $script:listPath ) {
                           $computers = Get-Content -Path $script:listpath |
                            Where-Object {$_ -match '\w+' -AND $_ -notmatch "#"} | Foreach-Object {$_.trim()} | Sort-Object
                }
                else {
                    #if path not found default to local computer
                    $computers = $env:computername
                }


                    #make sure there are sessions for all computers in the list
                    $computers | Where-Object {(get-pssession).where( {$_.state -eq 'opened'}).computername -notcontains $_} |
                        ForEach-Object {
                        New-PSSession -ComputerName $_ -SessionOption $opt
                    }
                    #remove broken sessions
                    Get-PSSession | Where-Object {$_.state -eq 'broken'} | Remove-PSSession
                    $results = Invoke-Command -ScriptBlock $sb -HideComputerName  -Session (Get-PSSession) 

                    $global:rsHash.results = $results
                    $global:rsHash.date = Get-Date
                    $global:rshash.computers = $computers
                    #set a sleep interval between tests
                    Start-Sleep -Seconds 10
                } While ($True)
            }) # script
        $pscmd.runspace = $newrunspace
        [void]$psCmd.BeginInvoke()
    } #catch

    #guess at a line length
    $names = ($global:rshash.computers | Measure-Object -Property length -sum).sum
    #account for a : after each name + character + value
    #you may have to tweak this value to get everything to line up
    #and it might depend on whether you are using the console or ISE
    $len = $names + (($global:rshash.computers.count) * 27) + 1
    
    #display local computername and datetime
    $dt= "`n {2} {0} {1} " -f (get-date).toshortdatestring(),(get-date).TolongTimeString(),$env:computername
    Write-host $dt -ForegroundColor black -BackgroundColor gray

    Write-Host "`n$([char]0x250c)" -NoNewline
    Write-Host $(([char]0x2500).ToString() * $len ) -NoNewline
    Write-Host $([char]0x2510) 
    Write-Host " " -NoNewline

    if ($global:rsHash.results) {
    
        #create a hashtable from the results
        $h = $global:rshash.results | Group-Object -Property Computername -AsHashTable -AsString

        #sort the results by computername
        foreach ($item in $global:rshash.computers) {

            if ($h[$item]) {
                $obj = $h[$item]
            }
            else {
                #create a temporary object for computer that is not running but in the list
                $obj = [pscustomobject]@{
                    Computername = $item.ToUpper()
                    Uptime = (New-TimeSpan).toString()
                    DiskFree = "00"
                    MemoryUsage = "00"
                    Processes = "00"
                    IsUp = $false
                }
            }

            if ($obj.DiskFree -le 20) {
                $diskfg = "red"
            }
            elseif ($obj.DiskFree -le 75) {
                $diskfg = "yellow"
            }
            else {
                $diskfg = "green"
            }
            if ($obj.MemoryUsage -le 30) {
                $memfg = "red"
            }
            elseif ($obj.MemoryUsage -le 60) {
                $memfg = "yellow"
            }
            else {
                $memfg = "green"
            }
            
            if ($obj.IsUp) {
                Write-Host " $($charHash.Up)" -ForegroundColor green -NoNewline
                $upfg = "green"
            }
            else {
                Write-Host " $($charHash.down)" -ForegroundColor red -NoNewline
                $upfg = "red"
            }
            
            Write-Host $obj.Uptime -ForegroundColor $upfg -NoNewline
            Write-Host " $($obj.computername) " -NoNewline            
            Write-Host "$([char]0x058d)$($obj.diskfree)%" -NoNewline -ForegroundColor $diskfg
            Write-Host " $([char]0x3bc)$($obj.MemoryUsage)%"-NoNewline -ForegroundColor $memfg 
            Write-Host " p$($obj.Processes)" -NoNewline
            
        } #foreach
    }
    else {
        Write-Host "Working..." -ForegroundColor yellow -NoNewline
    }

    Write-Host " "
    Write-Host $([char]0x2514) -NoNewline
    Write-Host $(([char]0x2500).ToString() * $len ) -NoNewline
    Write-Host $([char]0x2518) 

    "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "

}

我仍在使用代码,因此您可能会看到略有不同的输出。否则,享受并享受一些乐趣。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯