Jump to content

Problem with default script 'Check System Drive Fragmentation'


Recommended Posts

Posted (edited)

I looked at this script, and it will always return "Operating system is not supported" because in the script $major and $minor are strings, I was able to confirm that with the commands:

$major.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

 

$minor.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

 

I recommend updating the script to this, and I highlighted the changes:

$version = (Get-WmiObject Win32_OperatingSystem).Version
[int]$major = $version.split(".")[0]
[int]$minor = $version.split(".")[1]
if ($major -gt 6 -or ($major -eq 6 -and $minor -gt 1)) {
    Optimize-Volume -driveletter $pwd.drive.name -Analyze -verbos
} Else {
    Write-Output "Operating system is not supported."
}

 

Here is the original script:

$version = (Get-WmiObject Win32_OperatingSystem).Version
$major = $version.split(".")[0]
$minor = $version.split(".")[1]
if ($major -gt 6 -or ($major -eq 6 -and $minor -gt 1)) {
    Optimize-Volume $pwd.drive.name -Analyze -verbos
} Else {
    Write-Output "Operating system is not supported."
}

 

REF: https://learn.microsoft.com/en-us/powershell/module/storage/optimize-volume?view=windowsserver2022-ps

REF: https://lazyadmin.nl/powershell/if-else-statements/

REF: https://theitbros.com/powershell-convert-string-to-int/

Edited by smcclos
Needed to clarify something

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...