I was writing a report on the server status. To create a graph I created a comma deliminated file using the following PowerShell script:

$timestamp = get-date -f “yyyy/MM/dd HH:mm:ss”
write-host $timestamp -nonewline
write-host “, ” -nonewline

#$strComputer = “.”
$strComputer = $env:ComputerName

$CPUCores = (Get-WmiObject -class “Win32_Processor” -namespace “root/CIMV2” -computername $strComputer)

$str = “”
foreach ($objItem in $CPUCores) {
    #echo $objItem.DeviceID
    #echo $objItem.NumberOfCores
    #echo $objItem.LoadPercentage
    #echo “”
    write-host $objItem.LoadPercentage -nonewline
    write-host “, ” -nonewline

# Memory is represented in kb, divide it in 1kb to get the value in mb
# Have then rounded to zero decimal places
$memory = Get-WmiObject -Class “Win32_OperatingSystem”
$FreeMem = ([math]::round($memory.FreePhysicalMemory / 1kb, 0))
$TotalMem = ([math]::round($memory.TotalVisibleMemorySize / 1kb, 0))

write-host $FreeMem

#((Get-WMIObject -class “Win32_OperatingSystem”).FreePhysicalMemory/1kb)
#((Get-WMIObject -class “Win32_OperatingSystem”).TotalVisibleMemorySize/1kb)
#((Get-WMIObject -class “win32_physicalmemory”).Capacity/1kb/1kb)

This was then wrapped in a .bat file so each time it was run the output was appended to a .txt file:

@echo off

c:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe ServerStats.ps1 >> ServerStatOut.txt

 The .bat file was then run every hour using task scheduler.

EDIT:  18/07

I found that the scrip wasn’t working when I closed the remote connection to the server.
To fix this I had to check the box so the script could run with the highese privelages: