none
CPU and MEM usage % RRS feed

  • Question

  • Hi

    I have the following script:

    ### HEADER SECTION ###

    $Header = "<style type='text/css'>`n" + `
      "td { width: 200px }`n" + `
      "th { text-align: left }`n" + `
      "</style>"

    ### Report name ###
    $reportName = "DCDiskSpaceRpt_$(get-date -format dd-MM-yyyy).html";

    ### COMPILE A LIST OF SERVERS ONLINE SECTION ###

    $OnlineServers = @()
    Get-ADComputer -Filter 'Name -like "*DC*"' | %{
      $PingResult = Get-WmiObject -Query "SELECT * FROM win32_PingStatus WHERE address='$($_.Name)'"
      If ($PingResult.StatusCode -eq 0) {
     
    ### Add the current name to the array  ###
        $OnlineServers += "$($_.Name)"
      }
    }

    ### BODY OF REPORT SECTION ###

    $Body = Get-WmiObject -Query "select * from win32_logicaldisk" -ComputerName $OnlineServers | `
    Where-Object {$_.drivetype -eq "3"} | `
    Select-Object `
    @{n="Server";e={$_.SystemName}},`
    @{n="Drive Letter";e={$_.DeviceID}},`
    @{n="Volume Name";e={$_.VolumeName}},`
    @{n="Total Size (GB)";e={"{0:n2}" -f($_.size/1GB)}},`
    @{n="Free Space (GB)";e={"{0:n2}" -f($_.FreeSpace/1GB)}},`
    @{n="Used Space (GB)";e={"{0:n2}" -f(([Int64]$_.size/1gb) - ([Int64]$_.FreeSpace/1gb))}},`
    @{n="Percentage Free";e={"{0:P2}" -f(($_.FreeSpace/$_.Size))}} | `
    ConvertTo-HTML -Title "Server Disk Report" -Head $Header | Out-File D:\Scripts\DC_Storage_Checks\$reportName

    The above currently outputs to a nice HTML page, however, I need to display a couple more columns and am hoping someone can help.

    I need to add the following columns:

    CPU % usage

    Memory % Usage

    Some of the servers have dual CPU's so I assume that 2 figures would be returned, I would prefer just 1 figure similar to what can be seen in Task Manager.

    I would also like to add some shading to indicate normal operation, Warning/High operation and Critical/heavy operation. Green, Orange and Red respectively.  The option to amend the values for the normal/high/critical thresholds.

    I hope someone can help

    Cheers

    Nelix

    • Moved by Bill_Stewart Friday, July 7, 2017 4:37 PM This is not "fix/debug/rewrite my script for me" forum
    Sunday, May 7, 2017 4:28 PM

All replies

  • This is how to write that in PowerShell:

    $Header = @'
    	<style type='text/css'>
    		td { width: 200px }
    		th { text-align: left }
    	</style>
    '@
    $properties = @(
    	'SystemName',
    	'DeviceID',
    	'VolumeName',
    	@{ n = "Total Size (GB)"; e = { "{0:n2}" -f ($_.size/1GB) } },
    	@{ n = "Free Space (GB)"; e = { "{0:n2}" -f ($_.FreeSpace/1GB) } },
    	@{ n = "Used Space (GB)"; e = { "{0:n2}" -f (([Int64]$_.size/1gb) - ([Int64]$_.FreeSpace/1gb)) } },
    	@{ n = "Percentage Free"; e = { "{0:P2}" -f (($_.FreeSpace/$_.Size)) } }
    )
    $reportName = "DCDiskSpaceRpt_$(get-date -format dd-MM-yyyy).html";
    
    Get-ADComputer -Filter 'Name -like "*DC*"' | 
    	Where-Object{Test-Connection $_.Name -Quiet} |
    	ForEach-Object{Get-WmiObject win32_logicaldisk -ComputerName $_.Name -Filter 'DriveType=3'} |
    	Select-Object $properties |
    	ConvertTo-HTML -Title 'Server Disk Report' -PreContent '<h1>Disk Space Report</h1>' -Head $Header |
    	Out-File D:\Scripts\DC_Storage_Checks\$reportName
    

    If you need extra information just add it to the property list.

    The memory and CPU can be extrapolated from Win32_OperatingSystem.


    \_(ツ)_/



    • Edited by jrv Sunday, May 7, 2017 5:40 PM
    Sunday, May 7, 2017 5:18 PM
  • Cheers.

    I've given that a go and I am getting the following error:

    ==========================================

    Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
    At line:20 char:48
    +     Get-WmiObject win32_logicaldisk -ComputerName $_.Name -Filter 'DriveType=3' |
    +                                                   ~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    ==========================================

    Sunday, May 7, 2017 5:33 PM
  • Sorry - I posted the wrong copy.

    Get-ADComputer -Filter 'Name -like "*DC*"' | 
    	Where-Object{Test-Connection $_.Name -Quiet} |
    	ForEach-Object{Get-WmiObject win32_logicaldisk -ComputerName $_.Name -Filter 'DriveType=3'} |
    	Select-Object $properties |
    	ConvertTo-HTML -Title 'Server Disk Report' -PreContent '<h1>Disk Space Report</h1>' -Head $Header |
    	Out-File D:\Scripts\DC_Storage_Checks\$reportName
    


    \_(ツ)_/

    Sunday, May 7, 2017 5:39 PM
  • That's working better, thanks

    I've added the following and it is creating a column but it is showing no figures, any idea's:

    @{n="Memory Usage %";e={ “{0:P2}” -f((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}|`

    Sunday, May 7, 2017 6:14 PM
  • My days of teaching arithmetic are over.  

    #1 - don't use line extenders.
    #2 - Use the properties list
    #3 - Take some time to learn PowerShell.

    Start with this.  I fixed some of the math.  I leave the rest up to you.

    $properties = @(
    	'SystemName',
    	'DeviceID',
    	'VolumeName',
    	@{ n = 'Total Size (GB)'; e = { '{0:n2}' -f ($_.size/1GB) } },
    	@{ n = 'Free Space (GB)'; e = { '{0:n2}' -f ($_.FreeSpace/1GB) } },
    	@{ n = 'Used Space (GB)'; e = { '{0:n2}' -f (($_.size - $_.FreeSpace)1Gb } },
    	@{ n = 'Percentage Free'; e = { '{0:P2}' -f ($_.FreeSpace/$_.Size) } },
    	@{ n = 'Memory Usage %';  e = { '{0:P2}' -f (($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)/ $_.TotalVisibleMemorySize * 100 } }
    )

    Of course you will still get zero because you have not queried WMI for the OS class instance and the logical disk object does not have memory sizes.

    As noted above. You need to start by learning PowerShell instead of copying code you don't understand and asking others to re-write it for you.  I have given you direction.  It is up to you to do something about it.


    \_(ツ)_/




    • Edited by jrv Sunday, May 7, 2017 7:44 PM
    Sunday, May 7, 2017 7:40 PM