Digital Fifteen

What is eating my CPU?

A very busy machine needed some relief...

A machine on site needed some attention as it was smashing its CPU around the place. User's that were trying to remote to it were unable to establish a working session via RDP to get on there and shutdown whatever was consuming all processing time.

PowerShell allowed me to get on the remote machine to see what was going on, find what was eating up all the CPU and dispose of it.

This can be done in a much neater way, but in this case the client was looking over my shoulder and wanted to step through what was being done to ensure that the service was appropriate to close down. Here is what I used...

# Set variables
$Creds = Get-Credential
$Machine = "hostname"

# Attempt to set up and connect using a PowerShell Session using the above details
$Session = New-PSSession -Credential $Creds -ComputerName $Machine
Enter-PSSession $Session

# Your promt changes to one that is prefixed with your remote machine name

# Get all processes from the machine that are using more than 5% of CPU Time and make the output readable
$HeavyProcesses = (Get-Counter '\Process(*)\% Processor Time').CounterSamples | Where-Object {$_.CookedValue -gt 5}

foreach ($HeavyProcess in $HeavyProcesses) {
    $HeavyProcessName = $HeavyProcess.InstanceName
    $HeavyProcessValue = [math]::Round($HeavyProcess.CookedValue,2)
    if ($HeavyProcessName -ne "idle" -and $HeavyProcessName -ne "_total") {
        Write-Host "$HeavyProcessName" -ForegroundColor Red -NoNewline
        Write-Host " is using " -NoNewline
        Write-Host "$HeavyProcessValue%" -ForegroundColor Red -NoNewline
        Write-Host " Processor Time"

foreach ($HeavyProcess in $HeavyProcesses) {
    $HeavyProcessName = $HeavyProcess.InstanceName
    $HeavyProcessValue = [math]::Round($HeavyProcess.CookedValue,2)
    if ($HeavyProcessName -eq "_total") {
        Write-Host ""
        Write-Host "Out of a TOTAL of $HeavyProcessValue% Processor Time" -ForegroundColor Cyan
        Write-Host ""
        Write-Host "(Each 100% time is a count of CPU)" -ForegroundColor DarkCyan
        Write-Host "(E.g. ~400% is 4 CPU)" -ForegroundColor DarkCyan

# NOTE: Each 100% time is a count of CPU - E.g. ~800% is 8 CPU cores

# Need to kill it?

# Find the name of a process that looks like it could be causing trouble and use this to get its ID
Get-Process -Name "naughtyprocess"

# Take the process ID you want to kill and put it in this to kill it... CAREFUL!
Stop-Process -Id "xxxxx" -Force -Confirm:$false

# Exit from and then drop the session
Remove-PSSession $Session

# Drink Coffee

Testing Syntax Highlighting

So many ways of doing this, so I'm avoiding writng my own again this time.

Just a quick post with some PowerShell to test the highlighting...

# This is a PowerShell comment.

function MyFunction([Parameter(Position = 0)][System.String]$path)
    :loopLabel foreach ($thisFile in (Get-ChildItem $path))
        Write-Host ; Write-Host -Fore Yellow `
            ('Length:' +
            [System.Math]::Floor($thisFile.Length / 1000))

Back to the PowerCLI...

I'll upload some scripts soon, I've just been busy working.

I know I keep saying about getting back into the scripting but I have just been so busy to get round to actually posting content.
I have thrown together a few helpful scripts of late and I'll get them on here soon. I'll also have to explain my methods, because I don't like trying to condense everything and make "one liners". I like to write scripts that step through logically and output useful details about where they are up to and what is going on. Using PowerShell's Write-Host and the many colours it allows, makes for a nice touch. I'd much rather that compares to something sitting silent and processing stuff in the background whilst the user doesn't know what is going on. Even if it is just cosmetic, I think it is just nice to look at. Cyan, Magenta, Yellow... Strong.

A good example of that came from today's requirement for some PowerCLI. Combining some different articles I have read, I wrote something that allows you to reduce disk sizes in VMware. Although this is not new, it was a good excuse to get colourful and detailed on the output. See my tweet about it and the image posted.

I know there is a need for Verbose, but sometimes it is just nice to present something different. I'm going to use it, so I might as well enjoy it. I also know that I am very overdue posting PowerShell content, but this is all the same. :)

Back to the PowerShell!

Time to get back to it and stop messing about with CSS...

Leaving the design of the page alone for a little while, I can get back to the PowerShell. I'm very much using this post as a little test to see if all the components are working OK... and they seem to be. So now I can get back to uploading some of the scripts and stuff that I have been using recently.

Planet PowerShell - Featured

I don't know how much use I'll be, but it is nice to be included in the Community.

So whilst I have been messing about with the design, I got a github notification to advise that my pull request had been approved and I have been added to the PlanetPowerShell repo. I had a look this morning to see that I am now "published" on the community authors page. That is pretty neat. I had to update the location, as I had moved since I originally submitted it... but I am here...

It is exactly typical that it happens when my blog is currently down, whilst I tinker with the design. However, it has given me the urge to get more content posted and maybe it will be of use to someone. You'll have to excuse the old content coming back when I merge it all together later down the line. For now though, I get to put a little image on the site to show that I am now featured.

↑ Top