rasta-mouse/Sherlock

Add PowerShell v1/v2 compatibility

vipzen opened this issue · 1 comments

Hello @rasta-mouse. I've been trying to adapt Sherlock to work with PowerShell v2 and managed to make it work even with v1 without any problem.
I have an ugly but full functional code here, but I will comment on some suggestions to be adapted accordingly by you in a more beautiful way.

Get-FileVersionInfo():

Instead using Win32_Product class (unstable on v2 and nonexistent on v1), we can use CIM_DataFile.

# Double slash for CIM_DataFile
$FilePath = $FilePath.Replace("\", "\\")
    
# PsH v1/v2 support via CIM_DataFile
$VersionInfo = (Get-WmiObject -Class CIM_DataFile -Filter "Name='$FilePath'" | Select-Object Version).Version

Also, on WinXP/Win2k3 with v2/v1, for some reason the architecture check fail in some functions and $Path will return null, triggering an error in Get-FileVersionInfo()

I "fixed" it by simply adding an extra Elseif conditional:

 } ElseIf ( $Architecture[1] -eq "x86" ) {

        $Path = $env:windir + "\system32\win32k.sys"

    }

Get-InstalledSoftware()

To avoid error, we can check the PowerShell version and return false in old versions.

    # Grab the PowerSherll version
    $PshVersion = $host.version.Major

    # If less or equal to 2, can not support Win32_Product Class then return false
    if($PshVersion -le '2') {

    return $false 
    
    } Else { 
    
    $SoftwareVersion = Get-WmiObject -Class Win32_Product | Where { $_.Name -eq $SoftwareName } | Select-Object Version
    
        if ( $SoftwareVersion) {
        
        $SoftwareVersion = $SoftwareVersion.Version  # I have no idea what I'm doing
        
        return $SoftwareVersion
        
        } else { 
        
        return $false
        
        }
    }