Diskspace Checker via Powershell Universal and HTML

This online tool will show the diskspace of a computer you specified. It is using PowerShell Universal as web server and HTML code to show the graphic bar. It has the disk capacity, free space and used space info.

WizTree is the magic tool here to find the largest 10 folders. It will first copy this tool to local computer you want to check, then run the tool, and copy the result csv file to our server, then our server will process data.

# Online Diskspace Checker via PowerShell Universal and HTML
# Rui Qiu
# 9/3/2021

# Set up global variables
$Session:pc = "xxx"
$Session:dir = "c:\temp\demo\"

# Asking for user input
New-UDTextbox -Id 'pc' -Placeholder 'Machine Name'
New-UDTextbox -Id 'drive' -Placeholder 'Drive Letter(C:) or Folder Path'
New-UDElement -tag 'div' -attributes @{
    style = @{ 
        height = '10px'
    }
}
New-UDButton -OnClick {
    $session:pc = (Get-UDElement -Id 'pc').value
    $session:dir = (Get-UDElement -Id 'drive').value  
    Show-UDToast -Message "You have selected $session:dir on $session:pc, it will take a few minutes to get the result." -Duration 5000
    Sync-UDElement -Id 'table'
} -Text "Confirm" 

New-UDElement -tag 'div' -attributes @{
    style = @{ 
        height = '10px'
    }
}

# Running Diskspace Checker
New-UDDynamic -Id 'table' -Content {

# User provides parameters
$pc = "$session:domain.name"
$dir = $session:dir
$drive = $dir.Substring(0,2)
New-UDTypography -Text "Host: $pc, Path: $dir"

New-UDElement -tag 'div' -attributes @{
    style = @{ 
        height = '10px'
    }
}

$script:Thresholds = @{}
$Thresholds.Warning = 80
$Thresholds.Critical =  90

Function Set-PercentageColour {
    param(
    [int]$Value
    )

    If ($Value -lt $Thresholds.Warning)
    {$Hex = "#00ff00" # Green
    }

    If ($Value -ge $Thresholds.Warning -and $Value -lt $Thresholds.Critical)
    {$Hex = "#ff9900" # Amber
    }

    If ($Value -ge $Thresholds.Critical)
    {$Hex = "#FF0000" # Red
    }

    Return $Hex
}

$WMIDiskInfo = gwmi win32_logicaldisk -ComputerName $pc –Property Size,FreeSpace,DeviceId | Where {$_.DeviceID -eq $drive} | Select Size,FreeSpace,DriveID
$DiskInfo = [pscustomobject]@{
    DriveLetter = $WMIDiskInfo.DriveId
    'Capacity (GB)' = [math]::Round(($WMIDiskInfo.Size / 1GB),2)
    'FreeSpace (GB)' = [math]::Round(($WMIDiskInfo.FreeSpace / 1GB),2)
    'UsedSpace (GB)' = [math]::Round((($WMIDiskInfo.Size / 1GB) – ($WMIDiskInfo.FreeSpace / 1GB)),2)
    'Percent Free' = [math]::Round(($WMIDiskInfo.FreeSpace * 100 / $WMIDiskInfo.Size),2)
    'Percent Used' = [math]::Round((($WMIDiskInfo.Size – $WMIDiskInfo.FreeSpace) * 100 / $WMIDiskInfo.Size),2)
}

$total = $DiskInfo.'Capacity (GB)'
$used = $DiskInfo.'UsedSpace (GB)'
$usedpercent = $DiskInfo.'Percent Used'
$free = $DiskInfo.'FreeSpace (GB)'

$sum = @"
<table cellpadding="0" cellspacing="0" width="700">
<tr>
  <td style="background-color:$(Set-PercentageColour –Value $usedpercent);padding:10px;color:#ffffff;" width="$usedpercent%">
    $used GB ($usedpercent%)
  </td>
  <td style="background-color:#eeeeee;padding-top:10px;padding-bottom:10px;color:#333333;" width="$($DiskInfo.'Percent Used')%">
  </td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" width="700">
</table>
"@

New-UDTypography -Text "Total Space: $total, Used Space: $used, Free Space: $free"
New-UDElement -tag 'div' -attributes @{
    style = @{ 
        height = '10px'
    }
}
New-UDHtml -Markup $sum
New-UDElement -tag 'div' -attributes @{
    style = @{ 
        height = '10px'
    }
}

# Running the WizTree remotely and getting the result
$session = New-PSSession –ComputerName $pc -UseSSL
$exe = "C:\Temp\WizTree64.exe"
$destination = "C:\Temp\Wiztree\"
If(!(Test-Path "\\$pc\c$\temp\WizTree64.exe")){Copy-Item –Path "E:\Web\WizTree64.exe" –Destination $exe –ToSession $session -Force}
Invoke-Command -Session $session -ScriptBlock {Start-Process –FilePath $using:exe –ArgumentList """$using:dir"" /export=""C:\Temp\$using:pc.csv"" /admin 1 /sortby=2 /exportfiles=0" –Verb runas –Wait}
Copy-Item –Path "C:\Temp\$pc.csv" –Destination "C:\Temp\Wiztree\" –FromSession $session -Force
$session | Remove-PSSession

# Remove first 3 row data
$CSVContent = Get-Content –Path "C:\Temp\WizTree\$pc.csv" –Totalcount 13 
$CSVContent = $CSVContent | Select –Skip 1
$CSVContent = $CSVContent | Select –Skip 1
$CSVContent = $CSVContent | Select –Skip 1

# Create a table to store the results
$Table = [System.Data.DataTable]::new("Directory Structure")
[void]$Table.Columns.Add([System.Data.DataColumn]::new("Folder",[System.String]))
[void]$Table.Columns.Add([System.Data.DataColumn]::new("Size (GB)",[System.Decimal]))

# Populate the table from the CSV data
Foreach ($csvrow in $CSVContent)
{
    $Content = $csvrow.split(',')
    [void]$Table.rows.Add(($Content[0].Replace('"','')),([math]::Round(($Content[2] / 1GB),2)))
}

 New-UDTable -Data $Table -ShowSort -Dense

}



Leave a Comment