/var/opt/nydus/ops/customer_local_ops/operating_system/powershell
Edit: /var/opt/nydus/ops/customer_local_ops/operating_system/powershell/collect_packages.ps1 (4475B)
param(
[string]$ProvisionDate = "",
[string]$EndDate = ""
)
$ErrorActionPreference = 'SilentlyContinue'
if ($ProvisionDate) {
try {
$provDt = [DateTime]::Parse($ProvisionDate).ToUniversalTime()
$cutoffDate = $provDt.Date
} catch {
$cutoffDate = [DateTime]::MinValue
}
} else {
$cutoffDate = [DateTime]::MinValue
}
$endCutoff = $null
if ($EndDate) {
try {
$endCutoff = [DateTime]::Parse($EndDate).ToUniversalTime()
if ($EndDate -match 'T00:00:00$') {
$endCutoff = $endCutoff.Date.AddHours(23).AddMinutes(59).AddSeconds(59)
}
} catch {
$endCutoff = $null
}
}
$results = @()
$seen = @{}
$regPaths = @(
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",
"HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
)
foreach ($path in $regPaths) {
Get-ItemProperty $path -ErrorAction SilentlyContinue | Where-Object {
$_.DisplayName -and $_.DisplayName.Trim() -ne ""
} | ForEach-Object {
$name = $_.DisplayName.Trim()
if ($seen.ContainsKey($name)) { return }
$seen[$name] = $true
$version = if ($_.DisplayVersion) { $_.DisplayVersion.Trim() } else { "unknown" }
$installDateStr = "unknown"
$include = ($cutoffDate -eq [DateTime]::MinValue)
if ($_.InstallDate) {
try {
$dt = [DateTime]::ParseExact($_.InstallDate, 'yyyyMMdd', $null)
$installDateStr = $dt.ToString('yyyy-MM-ddT00:00:00')
if ($cutoffDate -eq [DateTime]::MinValue -or $dt -ge $cutoffDate) { $include = $true }
} catch {
$installDateStr = "unknown"
$include = $true
}
} else {
$include = $true
}
if ($include -and $endCutoff -ne $null -and $installDateStr -ne "unknown") {
try {
$instDt = [DateTime]::Parse($installDateStr).ToUniversalTime()
if ($instDt -gt $endCutoff) { $include = $false }
} catch {}
}
if ($include) {
$results += @{
name = $name
version = $version
installed_date = $installDateStr
source = "registry"
}
}
}
}
$defaultFeatures = @(
'FileAndStorage-Services', 'Storage-Services', 'NET-Framework-45-Features',
'NET-Framework-45-Core', 'NET-WCF-Services45', 'NET-WCF-TCP-PortSharing45',
'PowerShellRoot', 'PowerShell', 'PowerShell-ISE', 'WoW64-Support',
'Server-Gui-Mgmt-Infra', 'Server-Gui-Shell', 'Windows-Defender',
'Windows-Defender-Features', 'WAS', 'WAS-Process-Model',
'WAS-Config-APIs', 'User-Interfaces-Infra', 'Server-Gui-Mgmt',
'Desktop-Experience', 'System-DataArchiver', 'Telnet-Client',
'SMB1Protocol', 'ServerCore-EA-Infra'
)
try {
Get-WindowsFeature -ErrorAction Stop | Where-Object {
$_.Installed -and $_.InstallState -eq 'Installed' -and
$defaultFeatures -notcontains $_.Name
} | ForEach-Object {
$fname = $_.Name
if ($seen.ContainsKey($fname)) { return }
$seen[$fname] = $true
$results += @{
name = $fname
version = "feature"
installed_date = "unknown"
source = "windows_feature"
}
}
} catch {}
if (Get-Command choco -ErrorAction SilentlyContinue) {
try {
choco list --limit-output 2>$null | ForEach-Object {
$parts = $_ -split '\|'
if ($parts.Count -ge 2) {
$cname = $parts[0].Trim()
if ($seen.ContainsKey($cname)) { return }
$seen[$cname] = $true
$results += @{
name = $cname
version = $parts[1].Trim()
installed_date = "unknown"
source = "chocolatey"
}
}
}
} catch {}
}
if ($endCutoff -ne $null) {
$filtered = @()
foreach ($r in $results) {
$id = $r.installed_date
if ($id -eq "unknown") {
$filtered += $r
} else {
try {
$instDt = [DateTime]::Parse($id).ToUniversalTime()
if ($instDt -le $endCutoff) { $filtered += $r }
} catch {
$filtered += $r
}
}
}
$results = $filtered
}
$results | ConvertTo-Json -Compress -Depth 3