Powershell for usage

 # Input and Output Configuration

$ServerList = Get-Content -Path "C:\ServerList.txt"   # Replace with your server list file path

$OutputFile = "C:\VM_Resource_Usage_Report.csv"      # Replace with desired output file path

$MaxThreads = 20                                     # Adjust number of concurrent threads based on system capacity

$Interval = 15                                       # Interval in minutes for pending replication size


# Define Runspace Pool

$RunspacePool = [runspacefactory]::CreateRunspacePool(1, $MaxThreads)

$RunspacePool.Open()

$Jobs = @()


# Function to Get Resource Usage for a VM

Function Get-VMResourceUsage {

    param($Server)


    try {

        $Results = @()

        Invoke-Command -ComputerName $Server -ScriptBlock {

            # Get All VMs in Primary Mode

            $VMs = Get-VM | Where-Object { $_.ReplicationMode -eq "Primary" }

            foreach ($VM in $VMs) {

                # Collect CPU Usage

                $CPUUsage = (Get-Counter "\Hyper-V Virtual Machine(*)\% Total Run Time" | Where-Object { $_.CounterSamples.Path -match $VM.VMId }).CounterSamples.CookedValue


                # Collect RAM Usage

                $RAMUsage = [math]::Round($VM.MemoryAssigned / 1GB, 2)


                # Collect Storage Usage

                $StorageUsed = [math]::Round((Get-VMHardDiskDrive -VMName $VM.VMName).Path | 

                    ForEach-Object { (Get-Item $_).Length / 1GB } | Measure-Object -Sum).Sum, 2


                # Collect Replication Pending Size

                $ReplicationPending = [math]::Round($VM.ReplicationHealth.ReplicationStatistics.PendingReplicationSize / 1MB, 2)


                # Add Results to PSCustomObject

                $Results += [PSCustomObject]@{

                    ServerName = $env:COMPUTERNAME

                    VMName     = $VM.VMName

                    CPU        = "$CPUUsage %"

                    RAM        = "$RAMUsage GB"

                    Storage    = "$StorageUsed GB"

                    ReplicationPending = "$ReplicationPending MB"

                    Time       = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

                }

            }

        } -ErrorAction Stop

        return $Results

    }

    catch {

        Write-Warning "Failed to fetch data from $Server. Error: $_"

        return $null

    }

}


# Process Servers in Threads

foreach ($Server in $ServerList) {

    $Job = [powershell]::Create().AddScript({

        param($ServerName)

        Get-VMResourceUsage -Server $ServerName

    }).AddArgument($Server)

    $Job.RunspacePool = $RunspacePool

    $Jobs += [PSCustomObject]@{ Server = $Server; Job = $Job; Handle = $Job.BeginInvoke() }

}


# Wait for Jobs and Gather Results

$AllResults = @()

foreach ($Job in $Jobs) {

    $Job.Job.EndInvoke($Job.Handle)

    $Output = $Job.Job.Invoke()

    if ($Output) { $AllResults += $Output }

    $Job.Job.Dispose()

}


# Export Results to CSV

if ($AllResults.Count -gt 0) {

    $AllResults | Export-Csv -Path $OutputFile -NoTypeInformation -Append

    Write-Output "Results exported to $OutputFile"

} else {

    Write-Warning "No results to export."

}


# Cleanup Runspace Pool

$RunspacePool.Close()

$RunspacePool.Dispose()

Comments

Popular Posts