需要在 Powershell 中测量排序 CPU 速度并写入文件

Need to measure sorting CPU speed in Powershell and write out to a file

需要开发一个脚本来测量 PowerShell 中的排序CPU速度,并每隔 30 分钟到 1 小时将以下信息写入日志文件

机器名称日期时间 sort100kseconds

最重要的是添加一些东西来计算 java.exe 进程或登录统计信息的用户的数量。

您可以使用 Get-CimInstance 获取 CPU 规格。 class 是 Win32_Processor
运行 Get-CimInstance Win32_Processor | Select-Object *
然后你使用你想要的属性。

至于过程,您可以使用 Get-Process cmdlet。
Get-Process *java*
如果不止一个,您也可以在 ForEach 循环中单独使用它。

https://docs.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?view=powershell-7.1

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-process?view=powershell-7.1#:~:text=The%20Get%2DProcess%20cmdlet%20gets,the%20pipeline%20to%20this%20cmdlet.

下面是我开发的脚本,用于测量 CPU 速度并将输出写入日志文件。我在编写脚本方面需要一点帮助。我需要在启动应用程序时安排此脚本,并且需要在退出启动的应用程序后立即停止此脚本写入日志。我如何在这个脚本中处理它?请协助

# Initialising the variables
$Array = @()
$Server = (Get-ComputerInfo).CsName
$measurecmd = (measure-command { 1..1000 | % {random} | sort }).Seconds
$datetime = Get-Date
$sortsec = "1000"
$LogTime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$LogFile = 'C:\Avanthi\PS_script_'+$LogTime+".log"

 # Starting the timer to iterate the output till session is closed
 $timer = [Diagnostics.Stopwatch]::StartNew()
 while ($timer.Elapsed.TotalSeconds -lt 1800) {
    Try {
    
        # Processor utilization
        $Processor = (Get-WmiObject -ComputerName $Server -Class win32_processor -ErrorAction Stop | Measure-Object -Property LoadPercentage -Average | Select-Object Average).Average
 
        # Memory utilization
        $ComputerMemory = Get-WmiObject -ComputerName $Server -Class win32_operatingsystem -ErrorAction Stop
        $Memory = ((($ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory)*100)/ $ComputerMemory.TotalVisibleMemorySize)
        $RoundMemory = [math]::Round($Memory, 2)
         
        # Creating custom object
        # MachineName Date Time sort100kseconds
        $Object = New-Object PSCustomObject
        $Object | Add-Member -MemberType NoteProperty -Name "Server name" -Value $Server
        $datetime = Get-Date
        $Object | Add-Member -MemberType NoteProperty -Name "Date Time" -Value $datetime
        $Object | Add-Member -MemberType NoteProperty -Name "CPU %" -Value $Processor
        $Object | Add-Member -MemberType NoteProperty -Name "Memory %" -Value $RoundMemory
        $Object | Add-Member -MemberType NoteProperty -Name "Meaure CPU speed" -Value (measure-command { 1..1000 | % {random} | sort }).Seconds
        $Object | Add-Member -MemberType NoteProperty -Name "Sort Seconds" -Value $sortsec
 
        $Object
        $Array += $Object
    }
    
    Catch {
        Write-Host "Something went wrong ($Server): "$_.Exception.Message
        Continue
    }
    
#Final results - Writing to Log file
If ($Array) { 
    $Array | Out-File $LogFile -Force
            }

    
# Sleep for amount of seconds
Start-Sleep -Seconds 60
}