I was trying to fetch the report for last 20 days for the System\Processor queue length, but is getting error.
At line:61 char:48
Write-Host “Error retrieving data from $server: $_” -Foregrou …
~~~~~~~~
Variable reference is not valid. ‘:’ was not followed by a valid variable name character. Consider using ${} to
delimit the name.
+ CategoryInfo : ParserError: (
, ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvalidVariableReferenceWithDrive
Need your expert advise on this. Below is the query :-
…
Define the list of servers
$servers = Get-Content -Path “D:\temp\Servers.txt” # Path to your server list file
Define output file
$outputFile = “D:\temp\QueueLengthResults.csv”
Initialize the results array
$results = @()
Loop through each server
foreach ($server in $servers) {
# Try-Catch for error handling
try {
# Create a hashtable to hold hourly data
$hourlyData = @{}
# Get queue length data for the last 20 days
for ($i = 0; $i -lt 30; $i++) {
$date = (Get-Date).AddDays(-$i)
# Retrieve the data every hour for the past 20 days
for ($hour = 0; $hour -lt 24; $hour++) {
$startTime = $date.Date.AddHours($hour)
$endTime = $startTime.AddHours(1)
# Get the counter value
$queueLength = Get-Counter -ComputerName $server -Counter “\System\Processor Queue Length” -StartTime $startTime -ErrorAction Stop | Select-Object -ExpandProperty CounterSamples
# Process and accumulate hourly data
foreach ($entry in $queueLength) {
$timestamp = [datetime]::Parse($entry.Timestamp).ToString(“yyyy-MM-dd HH:00”) # Format timestamp to hourly
$value = [math]::Round($entry.CookedValue, 2) # Round value to two decimal places
if (-not $hourlyData.ContainsKey($timestamp)) {
$hourlyData[$timestamp] = @()
}
$hourlyData[$timestamp] += $value
}
}
}
# Calculate Min, Max, and Average for each hour
foreach ($timestamp in $hourlyData.Keys) {
$minValue = ($hourlyData[$timestamp] | Measure-Object -Minimum).Minimum
$maxValue = ($hourlyData[$timestamp] | Measure-Object -Maximum).Maximum
$avgValue = ($hourlyData[$timestamp] | Measure-Object -Average).Average
$totalEntries = $hourlyData[$timestamp].Count
# Add results to final output
$results += [PSCustomObject]@{
ServerName = $server
Timestamp = $timestamp
MinQueueLength = $minValue
MaxQueueLength = $maxValue
AvgQueueLength = [math]::Round($avgValue, 2)
TotalEntries = $totalEntries
}
}
}
catch {
Write-Host “Error retrieving data from $server: $_” -ForegroundColor Red
}
}
Export results to CSV
$results | Export-Csv -Path $outputFile -NoTypeInformation
Write-Host “Data exported to $outputFile” -ForegroundColor Green
…