简体   繁体   中英

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

Need to develop a script to measure sorting CPU speed in PowerShell and write out the following information to the log file every 30 minutes to 1 hour

MachineName Date Time sort100kseconds

A cherry on top could be to add something to count number of java.exe processes or users logged in to the stats.

You can use Get-CimInstance to get CPU specs. The class is Win32_Processor
Run Get-CimInstance Win32_Processor | Select-Object * Get-CimInstance Win32_Processor | Select-Object *
Then you work with the properties you want.

As for the process, you can work with Get-Process cmdlet.
Something like Get-Process *java*
If there's more than one you also can work with it separately in a ForEach loop.

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 .

Below is the script I developed to measure CPU speed and write output to log file. I need a small help in scripting. I need to schedule this script when an application is launched and need to stop this script from writing to log immediately after quitting the launched application. How do I handle that in this script? Pls assist

# 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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