Get-PC/Private/AppDiff.ps1
2024-07-26 16:04:17 -07:00

112 lines
3.3 KiB
PowerShell

param (
$examplePC
)
$GetPCNotInstalledApps = @(
"Citrix",
"Microsoft Visual C\+\+",
"RightFax",
"Windows Driver Package"
)
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
# Open file with manually added apps to ignore
$lookupTablePath = Join-Path (get-item $PSScriptRoot).Parent.FullName 'Data\AppsToIgnore.json'
$ignore = get-content $lookupTablePath | ConvertFrom-Json
$exclude = $example + $ignore
$output = @()
# Finds each app that isn't in the example
foreach ( $app in $apps ) {
$skip = $false
foreach ($notapp in $GetPCNotInstalledApps) {
if ($app.DisplayName -match $notapp) {
$skip = $true
break
}
}
if ($skip -or $exclude.DisplayName -contains $app.DisplayName) { continue }
$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')
$sccmPath = (Join-Path (get-item $PSScriptRoot).Parent.FullName 'Private\SCCMQuery.ps1')
Import-Module -Force $appsPath
Import-Module -Force $sccmPath
$apps = Get-Apps $examplePC | Select-Object -Property "DisplayName"
$trimmedApps = @()
foreach ($app in $apps) {
$skip = $false
foreach ($notapp in $GetPCNotInstalledApps) {
if (!$app.DisplayName -or $app.DisplayName -match $notapp) {
$skip = $true
break
}
}
if ($skip) { continue }
$trimmedApps += $app
}
$apps = Remove-Version $trimmedApps
# 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
}