EdTrud
EdTrud

Reputation: 15

Powershell: no output with script

I am looking into a way that I can get the CPU and Memory usage from my servers but i'm not getting a output from this script, Is there something wrong with the way Write-Host is setup here? Any Help is appreciated

    Function Get-ADtop {
[CmdletBinding()]
param(
    [String]$ComputerName,
    [String]$Sort = "none",  
    [String]$BaseDN = "OU=systems,DC=domain,DC=com",  # Edit Default Base DN
    [String]$SampleTime = 2
)
If ($ComputerName) {
    $Computers = $ComputerName
} else {
    $Computers = Get-ADComputer -Filter * -Properties * -SearchBase $BaseDN -EA SilentlyContinue | % {$_.Name}
}
$DataSet = @()
$Targets = @()
ForEach ($Comp in $Computers) {
    If (Test-Connection -ComputerName $Comp -Count 1 -Quiet -TimeToLive 1 -EA SilentlyContinue) {
        If (!(Get-WmiObject -ComputerName $Comp win32_OperatingSystem -EA SilentlyContinue)) { break }
        $Targets += $Comp
    }
}
$CompCount = $Computers | Measure-Object | % {$_.Count}
$DeadCount = $CompCount - ($Targets | Measure-Object | % {$_.Count})
If (!($DeadCount -eq 0)) {
    Write-Host "`n$DeadCount unavailable computers removed"
}
Write-Host "`nGathering realtime CPU/MEM/DISK Usage data from $CompCount computers..."
ForEach ($Comp in $Targets) {
    $proc = (Get-WmiObject -ComputerName $Comp -class win32_processor -EA SilentlyContinue | Measure-Object -property LoadPercentage -Average | Select Average | % {$_.Average / 100}).ToString("P")
    $mem = Get-WmiObject -ComputerName $Comp win32_OperatingSystem -EA SilentlyContinue 
    $mem = (($mem.TotalVisibleMemorySize - $mem.FreePhysicalMemory) / $mem.TotalVisibleMemorySize).ToString("P")
    $disk = Get-WmiObject -ComputerName $Comp -class Win32_LogicalDisk -filter "DriveType=3" -EA SilentlyContinue 
    $disk = (($disk.Size - $disk.FreeSpace) / $disk.Size).ToString("P")
    $Info = [pscustomobject]@{
        'Computer' = $Comp
        'CPU Usage' = $proc
        'MEM Usage' = $mem
        'Disk Usage' = $disk
    }
    $DataSet += Add-Member -InputObject $Info -TypeName Computers.CPU.Usage -PassThru    
}
Switch ($Sort) {
    "none" { $DataSet }
    "CPU" { $DataSet | Sort-Object -Property "CPU Usage" -Descending }
    "MEM" { $DataSet | Sort-Object -Property "MEM Usage" -Descending }
    "DISK" { $DataSet | Sort-Object -Property "DISK Usage" -Descending }
}
}

Upvotes: 1

Views: 143

Answers (1)

FoxDeploy
FoxDeploy

Reputation: 13537

Your script fails as is on a system with more than one fixed disk. To fix this, I modified your line 33 to iterate through each disk. This error was a terminating error, which is why PowerShell wouldn't give you an output before.

It seems to work with no issues now.

$disk = $disk | % {($($_.Size) - $($_.FreeSpace)) / $($d_.Size).ToString("P")}

>Gathering realtime CPU/MEM/DISK Usage data from 1 computers...

 Computer  CPU Usage MEM Usage Disk Usage        
 --------  --------- --------- ----------        
 localhost 14.00 %   27.64 %   {62.91 %, 34.90 %}

Upvotes: 1

Related Questions