From 403762d862f46e156a719adc9b143094c6c4bc14 Mon Sep 17 00:00:00 2001 From: silversword411 Date: Tue, 31 Aug 2021 22:45:53 -0400 Subject: [PATCH] wip script additions --- .../Win_Disk_Space_Usage_Reports_Wiztree.ps1 | 302 ++++++++++++++++++ ...n_Disk_Space_Usage_Reports_WiztreeAlt2.ps1 | 17 + scripts_wip/Win_Powershell_Upgrade.ps1 | 2 + scripts_wip/Win_Powershell_Version_Check.ps1 | 12 + scripts_wip/Win_Speedtest_Ookla.ps1 | 33 ++ scripts_wip/Win_Speedtest_Packetloss.ps1 | 65 ++++ 6 files changed, 431 insertions(+) create mode 100644 scripts_wip/Win_Disk_Space_Usage_Reports_Wiztree.ps1 create mode 100644 scripts_wip/Win_Disk_Space_Usage_Reports_WiztreeAlt2.ps1 create mode 100644 scripts_wip/Win_Powershell_Version_Check.ps1 create mode 100644 scripts_wip/Win_Speedtest_Ookla.ps1 create mode 100644 scripts_wip/Win_Speedtest_Packetloss.ps1 diff --git a/scripts_wip/Win_Disk_Space_Usage_Reports_Wiztree.ps1 b/scripts_wip/Win_Disk_Space_Usage_Reports_Wiztree.ps1 new file mode 100644 index 00000000..a6cd2e06 --- /dev/null +++ b/scripts_wip/Win_Disk_Space_Usage_Reports_Wiztree.ps1 @@ -0,0 +1,302 @@ +<# +From https://smsagent.blog/2018/08/15/create-disk-usage-reports-with-powershell-and-wiztree/ + +To use the script, simply download the WizTree Portable app, extract the WizTree64.exe and place it in the same location as the script (assuming 64-bit OS). Set the run location in the script (ie $PSScriptRoot if calling the script, or the directory location if running in the ISE), the temporary location where it can create files, and the server share where you want to copy the reports to. Then just run the script in admin context. + + #> + + +# Script to export html and csv reports of file and directory content on the system drive + +# Use to identify large files/directories for disk space cleanup +# Uses WizTree portable to quickly retrieve file and directory sizes from the Master File Table on disk +# Download and extract the WizTree64.exe and place in the same directory as this script + +# Set the running location +$RunLocation = $PSScriptRoot +#$RunLocation = "C:\temp" +$TempLocation = "C:\temp" + +# Set Target share to copy the reports to +$TargetRoot = "\\server-01\sharename\DirectorySizeInfo" + +# Free disk space thresholds (percentages) for summary report +$script:Thresholds = @{} +$Thresholds.Warning = 80 +$Thresholds.Critical = 90 + +# Custom function to exit with a specific code +function ExitWithCode { + param + ( + $exitcode + ) + $host.SetShouldExit($exitcode) + exit +} + +# Function to set the progress bar colour based on the the threshold value in the summary report +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 +} + +# Define Html CSS style +$Style = @" + +"@ + +# Set the filenames of WizTree csv's +$FilesCSV = "Files_$(Get-Date –Format 'yyyyMMdd_hhmmss').csv" +$FoldersCSV = "Folders_$(Get-Date –Format 'yyyyMMdd_hhmmss').csv" + +# Set the filenames of customised csv's +$ExportedFilesCSV = "Exported_Files_$(Get-Date –Format 'yyyyMMdd_hhmmss').csv" +$ExportedFoldersCSV = "Exported_Folders_$(Get-Date –Format 'yyyyMMdd_hhmmss').csv" + +# Set the filenames of html reports +$ExportedFilesHTML = "Largest_Files_$(Get-Date –Format 'yyyyMMdd_hhmmss').html" +$ExportedFoldersHTML = "Largest_Folders_$(Get-Date –Format 'yyyyMMdd_hhmmss').html" +$SummaryHTMLReport = "Disk_Usage_Summary_$(Get-Date –Format 'yyyyMMdd_hhmmss').html" + +# Run the WizTree portable app +Start-Process –FilePath "$RunLocation\WizTree64.exe" –ArgumentList """$Env:SystemDrive"" /export=""$TempLocation\$FilesCSV"" /admin 1 /sortby=2 /exportfolders=0" –Verb runas –Wait +Start-Process –FilePath "$RunLocation\WizTree64.exe" –ArgumentList """$Env:SystemDrive"" /export=""$TempLocation\$FoldersCSV"" /admin 1 /sortby=2 /exportfiles=0" –Verb runas –Wait + + + +#region Files + +# Remove the first 2 rows from the CSVs to leave just the relevant data +$CSVContent = Get-Content –Path $TempLocation\$FilesCSV –ReadCount 0 +$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("Name", [System.String])) +[void]$Table.Columns.Add([System.Data.DataColumn]::new("Size (Bytes)", [System.Int64])) +[void]$Table.Columns.Add([System.Data.DataColumn]::new("Size (KB)", [System.Decimal])) +[void]$Table.Columns.Add([System.Data.DataColumn]::new("Size (MB)", [System.Decimal])) +[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('"', '')), $Content[2], ([math]::Round(($Content[2] / 1KB), 2)), ([math]::Round(($Content[2] / 1MB), 2)), ([math]::Round(($Content[2] / 1GB), 2))) +} + +# Export the table to a new CSV +$Table | Sort 'Size (Bytes)' –Descending | Export-CSV –Path $TempLocation\$ExportedFilesCSV –NoTypeInformation –UseCulture + +# Export the largest 100 results into html format +$Table | +Sort 'Size (Bytes)' –Descending | +Select –First 100 | +ConvertTo-Html –Property 'Name', 'Size (Bytes)', 'Size (KB)', 'Size (MB)', 'Size (GB)' –Head $style –Body "

100 largest files on $env:COMPUTERNAME

" –CssUri "http://www.w3schools.com/lib/w3.css" | +Out-String | Out-File $TempLocation\$ExportedFilesHTML + +#endregion + + + +#region Folders + +# Remove the first 2 rows from the CSVs to leave just the relevant data +$CSVContent = Get-Content –Path $TempLocation\$FoldersCSV –ReadCount 0 +$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("Name", [System.String])) +[void]$Table.Columns.Add([System.Data.DataColumn]::new("Size (Bytes)", [System.Int64])) +[void]$Table.Columns.Add([System.Data.DataColumn]::new("Size (KB)", [System.Decimal])) +[void]$Table.Columns.Add([System.Data.DataColumn]::new("Size (MB)", [System.Decimal])) +[void]$Table.Columns.Add([System.Data.DataColumn]::new("Size (GB)", [System.Decimal])) +[void]$Table.Columns.Add([System.Data.DataColumn]::new("Files", [System.String])) +[void]$Table.Columns.Add([System.Data.DataColumn]::new("Folders", [System.String])) + +# Populate the table from the CSV data +Foreach ($csvrow in $CSVContent) { + $Content = $csvrow.split(',') + [void]$Table.rows.Add($($Content[0].Replace('"', '')), $Content[2], ([math]::Round(($Content[2] / 1KB), 2)), ([math]::Round(($Content[2] / 1MB), 2)), ([math]::Round(($Content[2] / 1GB), 2)), $Content[5], $Content[6]) +} + +# Export the table to a new CSV +$Table | Sort 'Size (Bytes)' –Descending | Export-CSV –Path $TempLocation\$ExportedFoldersCSV –NoTypeInformation –UseCulture + +# Export the largest 100 results into html format +$Table | +Sort 'Size (Bytes)' –Descending | +Select –First 100 | +ConvertTo-Html –Property 'Name', 'Size (Bytes)', 'Size (KB)', 'Size (MB)', 'Size (GB)', 'Files', 'Folders' –Head $style –Body "

100 largest directories on $env:COMPUTERNAME

" –CssUri "http://www.w3schools.com/lib/w3.css" | +Out-String | Out-File $TempLocation\$ExportedFoldersHTML + +#endregion + + + +#region Create HTML disk usage summary report + +# Get system drive data +$WMIDiskInfo = Get-CimInstance –ClassName Win32_Volume –Property Capacity, FreeSpace, DriveLetter | Where { $_.DriveLetter -eq $env:SystemDrive } | Select Capacity, FreeSpace, DriveLetter +$DiskInfo = [pscustomobject]@{ + DriveLetter = $WMIDiskInfo.DriveLetter + 'Capacity (GB)' = [math]::Round(($WMIDiskInfo.Capacity / 1GB), 2) + 'FreeSpace (GB)' = [math]::Round(($WMIDiskInfo.FreeSpace / 1GB), 2) + 'UsedSpace (GB)' = [math]::Round((($WMIDiskInfo.Capacity / 1GB) – ($WMIDiskInfo.FreeSpace / 1GB)), 2) + 'Percent Free' = [math]::Round(($WMIDiskInfo.FreeSpace * 100 / $WMIDiskInfo.Capacity), 2) + 'Percent Used' = [math]::Round((($WMIDiskInfo.Capacity – $WMIDiskInfo.FreeSpace) * 100 / $WMIDiskInfo.Capacity), 2) +} + +# Create html header +$html = @" + + + + +"@ + +# Set html +$html = $html + @" +

Disk Space Usage for Drive $($DiskInfo.DriveLetter) on $env:COMPUTERNAME

+ + + + + +
+ $($DiskInfo.'UsedSpace (GB)') GB ($($DiskInfo.'Percent Used') %) + +
+ + + + + + + + + + +
+ Capacity: $($DiskInfo.'Capacity (GB)') GB +
+ FreeSpace: $($DiskInfo.'FreeSpace (GB)') GB +
+ Percent Free: $($DiskInfo.'Percent Free') % +
+"@ + +If ($DiskInfo.'FreeSpace (GB)' -lt 20) { + + $html = $html + @" + + + + +
+ You need to free $(20 – $DiskInfo.'FreeSpace (GB)') GB on this disk to pass the W10 readiness check! +
+"@ +} + +# Close html document +$html = $html + @" + + +"@ + +# Export to file +$html | +Out-string | +Out-File $TempLocation\$SummaryHTMLReport + + +#endregion + + + + +#region Copy files to share + +# Create a subfolder with computername if doesn't exist +If (!(Test-Path $TargetRoot\$env:COMPUTERNAME)) { + $null = New-Item –Path $TargetRoot –Name $env:COMPUTERNAME –ItemType Directory +} + +# Create a subdirectory with current date-time +$DateString = ((Get-Date).ToUniversalTime() | get-date –Format "yyyy-MM-dd_HH-mm-ss").ToString() +If (!(Test-Path $TargetRoot\$env:COMPUTERNAME\$DateString)) { + $null = New-Item –Path $TargetRoot\$env:COMPUTERNAME –Name $DateString –ItemType Directory +} + +# Set final target location +$TargetLocation = "$TargetRoot\$env:COMPUTERNAME\$DateString" + +# Copy files +$Files = @( + $ExportedFilesCSV + $ExportedFoldersCSV + $ExportedFilesHTML + $ExportedFoldersHTML + $SummaryHTMLReport +) +Try { + Robocopy $TempLocation $TargetLocation $Files /R:10 /W:5 /NP +} +Catch {} + +#endregion + + +# Cleanup temp files +$Files = @( + $FilesCSV + $FoldersCSV + $ExportedFilesCSV + $ExportedFoldersCSV + $ExportedFilesHTML + $ExportedFoldersHTML + $SummaryHTMLReport +) + +Foreach ($file in $files) { + Remove-Item –Path $TempLocation\$file –Force +} + + +# Force a code 0 on exit, in case of some non-terminating error. +ExitWithCode 0 \ No newline at end of file diff --git a/scripts_wip/Win_Disk_Space_Usage_Reports_WiztreeAlt2.ps1 b/scripts_wip/Win_Disk_Space_Usage_Reports_WiztreeAlt2.ps1 new file mode 100644 index 00000000..a9aa364a --- /dev/null +++ b/scripts_wip/Win_Disk_Space_Usage_Reports_WiztreeAlt2.ps1 @@ -0,0 +1,17 @@ +# extract WizTree +Expand-Archive C:\temp\wiztree_3_26_portable.zip -DestinationPath C:\temp\wiztree + +# run wiztree.exe against provided drive/path +# generates diskusage.csv file and uploads to asset, deletes local file after upload + +# If 32-bit +if ([System.IntPtr]::Size -eq 4) { + C:\temp\wiztree\wiztree.exe "$scanpath" /export="c:\temp\wiztree\diskusage.csv" /admin=1 /exportfolders=1 /exportfiles=0 /sortby=2 | Out-Null +} +else { + C:\temp\wiztree\wiztree64.exe "$scanpath" /export="c:\temp\wiztree\diskusage.csv" /admin=1 /exportfolders=1 /exportfiles=0 /sortby=2 | Out-Null +} +# This will upload the file to Syncro and attach it to the Asset. +Upload-File -Subdomain "$subdomain" -FilePath "C:\temp\wiztree\diskusage.csv" +# Delete local file after upload +Remove-Item -Path "C:\temp\wiztree\diskusage.csv" -Force \ No newline at end of file diff --git a/scripts_wip/Win_Powershell_Upgrade.ps1 b/scripts_wip/Win_Powershell_Upgrade.ps1 index ac1519c2..5988f0e2 100644 --- a/scripts_wip/Win_Powershell_Upgrade.ps1 +++ b/scripts_wip/Win_Powershell_Upgrade.ps1 @@ -5,6 +5,8 @@ # Win 8.1 x64 and Svr 2012 R2 x64 https://download.microsoft.com/download/6/F/5/6F5FF66C-6775-42B0-86C4-47D41F2DA187/Win8.1AndW2K12R2-KB3191564-x64.msu # Win 81 x32 https://download.microsoft.com/download/6/F/5/6F5FF66C-6775-42B0-86C4-47D41F2DA187/Win8.1-KB3191564-x86.msu +# See https://github.com/wh1te909/tacticalrmm/blob/develop/scripts_wip/Win_Powershell_Version_Check.ps1 for alert script to warn when this is needed + if ($PSVersionTable.PSVersion.Major -lt 5) { Write-Output "Old Version - Need to Upgrade" # Download MSU file - EDIT THIS URL diff --git a/scripts_wip/Win_Powershell_Version_Check.ps1 b/scripts_wip/Win_Powershell_Version_Check.ps1 new file mode 100644 index 00000000..5f69d640 --- /dev/null +++ b/scripts_wip/Win_Powershell_Version_Check.ps1 @@ -0,0 +1,12 @@ +# Use as check script for old Powershell version 2.0 (aka Win7) and upgrade using https://github.com/wh1te909/tacticalrmm/blob/develop/scripts_wip/Win_Powershell_Upgrade.ps1 + +if ($PSVersionTable.PSVersion.Major -gt 2) { + $PSVersionTable.PSVersion.Major + Write-Output "PSVersion Greater than 2.0" + exit 0 +} +else { + $PSVersionTable.PSVersion.Major + Write-Output "PSVersion less than 2.0" + exit 1 +} \ No newline at end of file diff --git a/scripts_wip/Win_Speedtest_Ookla.ps1 b/scripts_wip/Win_Speedtest_Ookla.ps1 new file mode 100644 index 00000000..874721ac --- /dev/null +++ b/scripts_wip/Win_Speedtest_Ookla.ps1 @@ -0,0 +1,33 @@ +$runpath = "C:\TechTools\Speedtest\Speedtest.exe" +$zippath = "C:\TechTools\Zip\" +$toolpath = "C:\TechTools\Speedtest\" +$Url = "https://install.speedtest.net/app/cli/ookla-speedtest-1.0.0-win64.zip" +$DownloadZipFile = "C:\TechTools\Zip\" + $(Split-Path -Path $Url -Leaf) +$ExtractPath = "C:\TechTools\Speedtest\" + + +#Check for speedtest cli executable, if missing it will check for and create folders required, +#download speedtest cli zip file from $URL and extract into correct folder +IF(!(test-path $runpath)) +{ + #Check for SpeedTest folder, if missing, create + If(!(test-path $toolpath)) + { + New-Item -ItemType Directory -Force -Path $toolpath + } + + #Check for zip folder, if missing, create + If(!(test-path $zippath)) + { + New-Item -ItemType Directory -Force -Path $zippath + } + + #Download and extract zip from the URL in $URL + Invoke-WebRequest -Uri $Url -OutFile $DownloadZipFile + $ExtractShell = New-Object -ComObject Shell.Application + $ExtractFiles = $ExtractShell.Namespace($DownloadZipFile).Items() + $ExtractShell.NameSpace($ExtractPath).CopyHere($ExtractFiles) + +} + +& $runpath \ No newline at end of file diff --git a/scripts_wip/Win_Speedtest_Packetloss.ps1 b/scripts_wip/Win_Speedtest_Packetloss.ps1 new file mode 100644 index 00000000..65a1fd35 --- /dev/null +++ b/scripts_wip/Win_Speedtest_Packetloss.ps1 @@ -0,0 +1,65 @@ +Import-Module $env:SyncroModule + +$Random = get-random -min 1 -max 100 +start-sleep $random + +######### Absolute monitoring values ########## +$maxpacketloss = 2 #how much % packetloss until we alert. +$MinimumDownloadSpeed = 10 #What is the minimum expected download speed in Mbit/ps +$MinimumUploadSpeed = 1 #What is the minimum expected upload speed in Mbit/ps +######### End absolute monitoring values ###### + +#Replace the Download URL to where you've uploaded the ZIP file yourself. We will only download this file once. +#Latest version can be found at: https://www.speedtest.net/nl/apps/cli +$DownloadURL = "https://bintray.com/ookla/download/download_file?file_path=ookla-speedtest-1.0.0-win64.zip" +$DownloadLocation = "$($Env:ProgramData)\SpeedtestCLI" +try { + $TestDownloadLocation = Test-Path $DownloadLocation + if (!$TestDownloadLocation) { + new-item $DownloadLocation -ItemType Directory -force + Invoke-WebRequest -Uri $DownloadURL -OutFile "$($DownloadLocation)\speedtest.zip" + Expand-Archive "$($DownloadLocation)\speedtest.zip" -DestinationPath $DownloadLocation -Force + } +} +catch { + write-host "The download and extraction of SpeedtestCLI failed. Error: $($_.Exception.Message)" + exit 1 +} +$PreviousResults = if (test-path "$($DownloadLocation)\LastResults.txt") { get-content "$($DownloadLocation)\LastResults.txt" | ConvertFrom-Json } +$SpeedtestResults = & "$($DownloadLocation)\speedtest.exe" --format=json --accept-license --accept-gdpr +$SpeedtestResults | Out-File "$($DownloadLocation)\LastResults.txt" -Force +$SpeedtestResults = $SpeedtestResults | ConvertFrom-Json + +#creating object +[PSCustomObject]$SpeedtestObj = @{ + downloadspeed = [math]::Round($SpeedtestResults.download.bandwidth / 1000000 * 8, 2) + uploadspeed = [math]::Round($SpeedtestResults.upload.bandwidth / 1000000 * 8, 2) + packetloss = [math]::Round($SpeedtestResults.packetLoss) + isp = $SpeedtestResults.isp + ExternalIP = $SpeedtestResults.interface.externalIp + InternalIP = $SpeedtestResults.interface.internalIp + UsedServer = $SpeedtestResults.server.host + ResultsURL = $SpeedtestResults.result.url + Jitter = [math]::Round($SpeedtestResults.ping.jitter) + Latency = [math]::Round($SpeedtestResults.ping.latency) +} +$SpeedtestHealth = @() +#Comparing against previous result. Alerting is download or upload differs more than 20%. +if ($PreviousResults) { + if ($PreviousResults.download.bandwidth / $SpeedtestResults.download.bandwidth * 100 -le 80) { $SpeedtestHealth += "Download speed difference is more than 20%" } + if ($PreviousResults.upload.bandwidth / $SpeedtestResults.upload.bandwidth * 100 -le 80) { $SpeedtestHealth += "Upload speed difference is more than 20%" } +} + +#Comparing against preset variables. +if ($SpeedtestObj.downloadspeed -lt $MinimumDownloadSpeed) { $SpeedtestHealth += "Download speed is lower than $MinimumDownloadSpeed Mbit/ps" } +if ($SpeedtestObj.uploadspeed -lt $MinimumUploadSpeed) { $SpeedtestHealth += "Upload speed is lower than $MinimumUploadSpeed Mbit/ps" } +if ($SpeedtestObj.packetloss -gt $MaxPacketLoss) { $SpeedtestHealth += "Packetloss is higher than $maxpacketloss%" } + +if (!$SpeedtestHealth) { + $SpeedtestHealth = "Healthy" +} + +Set-Asset-Field -Subdomain "fresh-tech" -Name "Download Speed" -Value $SpeedtestObj.downloadspeed +Set-Asset-Field -Subdomain "fresh-tech" -Name "Upload Speed" -Value $SpeedtestObj.uploadspeed +Set-Asset-Field -Subdomain "fresh-tech" -Name "Packet Loss" -Value $SpeedtestObj.packetloss +Set-Asset-Field -Subdomain "fresh-tech" -Name "Speedtest Health" -Value $SpeedtestHealth