Privacy Info

Sunday, October 16, 2022

PowerShell Script - CPU and Storage Details


Manually checking Storage consumption and CPU usage multiple time is very repetitive and boring task. Also if you want to check same details on remote servers then it will again consume lot of time. PowerShell help us to automate this process. We need to specify credentials, name of the server in the script and it will provide storage consumption and CPU utilization details.

Script 

PowerShell Script

$MyDir = Split-Path -Parent $MyInvocation.MyCommand.Path
[xml]$ConfigFile = Get-Content "$MyDir\Settings.xml"


function GetCPUStorageDetails($serverName)
{
    $Username = $ConfigFile.Settings.CredentialsSettings.Username;
    $Password = ConvertTo-SecureString -String $ConfigFile.Settings.CredentialsSettings.Password -AsPlainText -Force   		
    $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Password

     $cpuUsage = Get-WmiObject Win32_Processor -ComputerName $serverName -Credential $Cred
     Write-Host ({0}% -f $cpuUsage.LoadPercentage )

     $storageDetails = Get-WmiObject -Class win32_logicaldisk -ComputerName $serverName -Credential $Cred

      foreach ($storageDetail in $storageDetails) {
        #Device ID | FreeSpace
        Write-Host -NoNewline ({0} {1}GB   -f  $storageDetail.DeviceId,([math]::Round($storageDetail.FreeSpace/1GB,2)) )   
     }

     Write-Host ""
}

GetCPUStorageDetails("ServerName")


Settings.xml

<?xml version="1.0"?>
<Settings>
	<CredentialsSettings>
		<Username>Username</Username>
		<Password>Password</Password>
	</CredentialsSettings>
</Settings>


Hope you find this information useful.




No comments:

Post a Comment

Sitecore PowerShell Script - Remove item from Workflow

We came across scenario where we need to remove multiple items from workflow. If items would be less then there would not be any issue but i...