From 56d8d9e4143e92611fb15e6336e4dd179ad2de8b Mon Sep 17 00:00:00 2001 From: dyoder Date: Fri, 23 Jul 2021 17:04:25 -0400 Subject: [PATCH] added new functions to Tools.ps1 and improved Get-InstalledSoftware.ps1 --- Get-InstalledSoftware.ps1 | 38 ++++++++++++++++++++++++++++++++++++-- Tools.ps1 | 24 +++++++++++++++++++++++- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/Get-InstalledSoftware.ps1 b/Get-InstalledSoftware.ps1 index 5d4dfee..6fa3f29 100644 --- a/Get-InstalledSoftware.ps1 +++ b/Get-InstalledSoftware.ps1 @@ -1,2 +1,36 @@ -$FILE = "${SystemDrive}\InstalledSoftware.csv" -Get-WmiObject -Class Win32_Product | Sort-Object Name | Select Name,Vendor,Version | Where Name -NotLike "" | Export-Csv -Path $FILE -NoTypeInformation -Force +#$FILE = "${SystemDrive}\InstalledSoftware.csv" +$FILE = "${UserProfile}\Desktop\InstalledSoftware.csv" +#Get-WmiObject -Class Win32_Product | Sort-Object Name | Select Name,Vendor,Version | Where Name -NotLike "" | Export-Csv -Path $FILE -NoTypeInformation -Force + +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 + } Else { $DisplayName = "" } + + If ( Test-ItemProperty -Path $item.PSPath -Name DisplayVersion ) { + $DisplayVersion = Get-ItemPropertyValue -Path $item.PSPath -Name DisplayVersion + } Else { $DisplayVersion = "" } + + If ( Test-ItemProperty -Path $item.PSPath -Name EstimatedSize ) { + $EstimatedSize = Get-ItemPropertyValue -Path $item.PSPath -Name EstimatedSize + } Else { $EstimatedSize = "" } + + If ( Test-ItemProperty -Path $item.PSPath -Name InstallDate ) { + $InstallDate = Get-ItemPropertyValue -Path $item.PSPath -Name InstallDate + } Else { $InstallDate = "" } + + If ( Test-ItemProperty -Path $item.PSPath -Name Publisher ) { + $Publisher = Get-ItemPropertyValue -Path $item.PSPath -Name Publisher + } Else { $Publisher = "" } + + If ( Test-ItemProperty -Path $item.PSPath -Name UninstallString ) { + $UninstallString = Get-ItemPropertyValue -Path $item.PSPath -Name UninstallString + } Else { $UninstallString = "" } + + If ( Test-ItemProperty -Path $item.PSPath -Name ModifyPath ) { + $ModifyPath = Get-ItemPropertyValue -Path $item.PSPath -Name ModifyPath + } Else { $ModifyPath = "" } + + $content = "${DisplayName},${Publisher},${DisplayVersion},${InstallDate},${EstimatedSize},${UninstallString},${ModifyPath}" + Write-LineToFile -Path $FILE -Line $content +} \ No newline at end of file diff --git a/Tools.ps1 b/Tools.ps1 index 601181c..4f406e5 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -12,7 +12,7 @@ $RootDirectory, $ScriptsDirectory, $ToolsDirectory, $LogsDirectory | ForEach-Obj If ( !(Test-Path $_) ) { Try { New-Item -Path $_ -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } Catch { Write-Output $_.Exception.Message } - } + } } ## Setup the log file for messages generated when this script is run @@ -41,6 +41,28 @@ 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) )