From eb93ecb0752cce6b0d074d7db94c0d64915887c2 Mon Sep 17 00:00:00 2001 From: dyoder Date: Mon, 26 Jul 2021 13:13:08 -0400 Subject: [PATCH] moved specific functions from Tools.ps1 to Get-InstalledSoftware.ps1 added additional processing features --- Get-InstalledSoftware.ps1 | 122 ++++++++++++++++++++++++++------------ Tools.ps1 | 22 ------- 2 files changed, 83 insertions(+), 61 deletions(-) diff --git a/Get-InstalledSoftware.ps1 b/Get-InstalledSoftware.ps1 index cf7a326..f264577 100644 --- a/Get-InstalledSoftware.ps1 +++ b/Get-InstalledSoftware.ps1 @@ -4,50 +4,94 @@ $FILE = "${Env:UserProfile}\Desktop\InstalledSoftware.csv" ## Clear content of output file If ( Test-Path $FILE ) { Clear-Content -Path $FILE -Force } +## Function to append a line to a file +Function Write-LineToFile { + param([Parameter(Mandatory=$true)][string]$Path,[Parameter(Mandatory=$true)][string]$Line) + If ( -not (Test-Path $Path) ) { + LogMsg "Creating file ""${Path}""" + Try { New-Item -Path $Path -ItemType File -Force | Out-Null } + Catch { LogErr $_.Exception.Message } + } + Try { Add-Content -Path $Path -Value $Line } + Catch { LogErr $_.Exception.Message } +} + +## Function to test whether or not an item has a specified property +Function Test-ItemProperty { + param([Parameter(Mandatory=$true)][string]$Path,[Parameter(Mandatory=$true)][string]$Name) + Try { + $test = Get-ItemProperty -Path $Path -Name $Name -ErrorAction SilentlyContinue + If (($test -eq $null) -or ($test.Length -eq 0)) { Return $false } Else { Return $true } + } + Catch { Return $false } +} + +## Function to retrieve an item property +Function Get-ItemPropertyInfo { + param([Parameter(Mandatory=$true)][string]$Path,[Parameter(Mandatory=$true)][string]$Name) + If ( Test-ItemProperty -Path $Path -Name $Name ) { + $retval = Get-ItemPropertyValue -Path $Path -Name $Name + $retval = $retval -replace ",", " " + + ## Additional processing for specific properties + Switch ( $Name ) { + + ## Convert the estimated size into MB + "EstimatedSize" { + $size = [int]$retval + $size = [Math]::Ceiling($size / 1024) + $retval = [string]$size + } + + ## Convert the date format into YYYY-MM-DD + "InstallDate" { + + $origRetval = $retval + + ## Try to find each type of date string so we can convert it + Switch -Regex ( $retval ) { + '^\d{8}$' { $formatstring = 'yyyyMMdd' } + '^\d{1,2}\\\d{1,2}\\\d{4}$' { $formatstring = 'MM\dd\yyyy' } + '^\d{4}\\\d{1,2}\\\d{1,2}$' { $formatstring = 'yyyy\MM\dd' } + Default { $formatstring = $null } + } + + ## Make sure $formatstring has a value, if not just try to cast directly to a datetime + Try { + If ( $formatstring -eq $null ) { + $date = [datetime]$retval + } Else { + $date = [datetime]::ParseExact($retval,$formatstring,$null) + } + + ## Set $retval to a standard datetime format + $retval = $date.ToString('yyyy-MM-dd') + } + Catch { LogErr $_.Exception.Message; $retval = $origRetval } + } + Default {} + } + } + Else { $retval = "" } + + Return $retval +} + ## Write the headers to the output file Write-LineToFile -Path $FILE -Line "Name,Publisher,Version,InstallDate,EstimatedSize(MB),UninstallString,ModifyPath" +## Iterate through all Registry Uninstall keys and build the CSV file of all installed software ForEach ( $item in $(Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall') ) { - If ( Test-ItemProperty -Path $item.PSPath -Name DisplayName ) { - $DisplayName = Get-ItemPropertyValue -Path $item.PSPath -Name DisplayName - $DisplayName = $DisplayName -replace ",", " " - } Else { $DisplayName = "" } + $DisplayName = Get-ItemPropertyInfo -Path $item.PSPath -Name DisplayName + $DisplayVersion = Get-ItemPropertyInfo -Path $item.PSPath -Name DisplayVersion + $EstimatedSize = Get-ItemPropertyInfo -Path $item.PSPath -Name EstimatedSize + $InstallDate = Get-ItemPropertyInfo -Path $item.PSPath -Name InstallDate + $Publisher = Get-ItemPropertyInfo -Path $item.PSPath -Name Publisher + $UninstallString = Get-ItemPropertyInfo -Path $item.PSPath -Name UninstallString + $ModifyPath = Get-ItemPropertyInfo -Path $item.PSPath -Name ModifyPath - If ( Test-ItemProperty -Path $item.PSPath -Name DisplayVersion ) { - $DisplayVersion = Get-ItemPropertyValue -Path $item.PSPath -Name DisplayVersion - $DisplayVersion = $DisplayVersion -replace ",", " " - } Else { $DisplayVersion = "" } - - If ( Test-ItemProperty -Path $item.PSPath -Name EstimatedSize ) { - $EstimatedSize = Get-ItemPropertyValue -Path $item.PSPath -Name EstimatedSize - $EstimatedSize = $EstimatedSize -replace ",", " " - $size = [int]$EstimatedSize - $size = [Math]::Ceiling($size / 1024) - $EstimatedSize = [string]$size - } Else { $EstimatedSize = "" } - - If ( Test-ItemProperty -Path $item.PSPath -Name InstallDate ) { - $InstallDate = Get-ItemPropertyValue -Path $item.PSPath -Name InstallDate - $InstallDate = $InstallDate -replace ",", " " - } Else { $InstallDate = "" } - - If ( Test-ItemProperty -Path $item.PSPath -Name Publisher ) { - $Publisher = Get-ItemPropertyValue -Path $item.PSPath -Name Publisher - $Publisher = $Publisher -replace ",", " " - } Else { $Publisher = "" } - - If ( Test-ItemProperty -Path $item.PSPath -Name UninstallString ) { - $UninstallString = Get-ItemPropertyValue -Path $item.PSPath -Name UninstallString - $UninstallString = $UninstallString -replace ",", " " - } Else { $UninstallString = "" } - - If ( Test-ItemProperty -Path $item.PSPath -Name ModifyPath ) { - $ModifyPath = Get-ItemPropertyValue -Path $item.PSPath -Name ModifyPath - $ModifyPath = $ModifyPath -replace ",", " " - } Else { $ModifyPath = "" } - - ## Write installed software to output file only if it has a name, otherwise discard - If ( $DisplayName -ne "" ) { + ## Write installed software to output file only if it has a name and uninstall/modify path, otherwise discard + If ( -not (($DisplayName -eq "") -and ($UninstallString -eq "") -and ($ModifyPath -eq "")) ) { $content = "${DisplayName},${Publisher},${DisplayVersion},${InstallDate},${EstimatedSize},${UninstallString},${ModifyPath}" Write-LineToFile -Path $FILE -Line $content } diff --git a/Tools.ps1 b/Tools.ps1 index 4f406e5..2079dd2 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -41,28 +41,6 @@ Function LogErr { Add-Content -Path $LogFile -Value $LogEntry } -## Function to append a line to a file -Function Write-LineToFile { - param([Parameter(Mandatory=$true)][string]$Path,[Parameter(Mandatory=$true)][string]$Line) - If ( -not (Test-Path $Path) ) { - LogMsg "Creating file ""${Path}""" - Try { New-Item -Path $Path -ItemType File -Force | Out-Null } - Catch { LogErr $_.Exception.Message } - } - Try { Add-Content -Path $Path -Value $Line } - Catch { LogErr $_.Exception.Message } -} - -## Function to test whether or not an item has a specified property -Function Test-ItemProperty { - param([Parameter(Mandatory=$true)][string]$Path,[Parameter(Mandatory=$true)][string]$Name) - Try { - $test = Get-ItemProperty -Path $Path -Name $Name -ErrorAction SilentlyContinue - If (($test -eq $null) -or ($test.Length -eq 0)) { Return $false } Else { Return $true } - } - Catch { Return $false } -} - ## Function to determine if the current user context is an Administrator Function IsAdmin { If ( ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) )