Get-PC/Private/AppDiff.ps1

88 lines
2.6 KiB
PowerShell
Raw Normal View History

2024-06-11 18:27:55 +00:00
param (
$examplePC
)
function Get-AppDiff {
param(
[Parameter(Mandatory)]
$pc,
$TableView = $false
)
# Compares a pc's apps with a list of expected apps to see if the pc has any
# extra apps that we need to consider downloading
$apps = Get-Apps $pc
$apps = Remove-Version $apps
# Open example file
$lookupTablePath = Join-Path (get-item $PSScriptRoot).Parent.FullName 'Data\ExpectedApps.json'
$example = get-content $lookupTablePath | ConvertFrom-Json
$output = @()
# Finds each app that isn't in the example
foreach ( $app in $apps ) {
if (
# Apps that shouldn't be considered an "installed app"
(-not ($app.DisplayName -match "Citrix")) -and
(-not ($app.DisplayName -match "Microsoft Visual C\+\+")) -and
(-not ($app.DisplayName -match "RightFax")) -and
# This check is last for performance
(-not ( $example.DisplayName -contains $app.DisplayName ) )
)
{
$output += $app
}
}
if($TableView){
$output | Out-GridView -Title "Get-PC Apps - $ComputerName"
}
else{
Write-Output $output
}
}
# Function parses get-pc -apps output to extract just the name, not the
# version number or other unnecessary info
function Remove-Version {
param ( $apps )
$output = @()
foreach ($app in $apps ) {
if ($null -eq $app.DisplayName) { continue }
# Strip out the version details
# " - " might filter out too much, not sure
$separator = "`t"," ", " - "
$option = [System.StringSplitOptions]::RemoveEmptyEntries
$app.DisplayName = $app.DisplayName.Split($separator, $option)[0]
$output += $app
}
Write-Output $output
}
function Copy-FileNoDestruct ($srcpath, $dstpath) {
while ($true) {
if (Test-Path $dstpath) {
$dstpath += ".bak"
} else {
Copy-Item -Path $srcpath -Destination $dstpath
return
}
}
}
function Get-ExampleApps {
param( $examplePC )
$appsPath = (Join-Path (get-item $PSScriptRoot).Parent.FullName 'Private\Apps.ps1')
Import-Module -Force $appsPath
$apps = Get-Apps $examplePC
$apps = Remove-Version $apps
# Open example file
$lookupTablePath = Join-Path (get-item $PSScriptRoot).Parent.FullName 'Data\ExpectedApps.json'
Copy-FileNoDestruct $lookupTablePath $($lookupTablePath + ".bak")
Write-Host "Writing expected apps to" $lookupTablePath
$apps | ConvertTo-Json | Out-File $lookupTablePath
}
if ($examplePC) {
Get-ExampleApps $examplePC
}