Esri / arcgis-powershell-dsc

This repository contains scripts, code and samples for automating the install and configuration of ArcGIS (Enterprise and Desktop) using Microsoft Windows PowerShell DSC (Desired State Configuration).
Apache License 2.0
114 stars 62 forks source link

Avoid using Win32_Product for application queries #287

Closed HakonD closed 4 years ago

HakonD commented 4 years ago

Community Note

Module Version

Affected Resource(s)

Windows

Configuration Files

Expected Behavior

Actual Behavior

We noticed that the Application event log on one of our customers sites filled up with Event ID 1035. Checking for the issue, I have found it on all inspected Windows servers with a DSC configuration and an installed arcgis module.

The culprit are line 288-289 in ArcGIS_Install.psm1, but there are also 18 other occurrences of Win32_product.

$ver = Get-CimInstance Win32_Product| Where-Object {$_.Name -match $trueName -and $_.Vendor -eq 'Environmental Systems Research Institute, Inc.'}
Write-Verbose "Installed Version $($ver.Version)"

As far as I can see the two lines are not even necessary, as the Write-Verbose is the only use of the $ver variable. A quick fix is to comment out these two lines in the installed module, or remove the arcgis module after installation.

Microsoft has strongly advised against using the Win32_Product class for application queries.

https://docs.microsoft.com/en-us/troubleshoot/windows-server/admin-development/windows-installer-reconfigured-all-applications

Win32_product class is not query optimized. Queries such as select * from Win32_Product where (name like 'Sniffer%') require WMI to use the MSI provider to enumerate all of the installed products and then parse the full list sequentially to handle the where clause. This process also initiates a consistency check of packages installed, verifying and repairing the install. With an account with only user privileges, as the user account may not have access to quite a few locations, may cause delay in application launch and an event 11708 stating an installation failure.

The recommended approach to get this information seems to be use of registry as mentioned in this devblog article. https://devblogs.microsoft.com/scripting/use-powershell-to-quickly-find-installed-software/

Code example using registry:

$array = @()

#Define the variable to hold the location of Currently Installed Programs
$UninstallKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"

#Create an instance of the Registry Object and open the HKLM base key
$reg = [microsoft.win32.registrykey]::OpenBaseKey("LocalMachine", [Microsoft.Win32.RegistryView]::Registry32)

#Drill down into the Uninstall key using the OpenSubKey Method
$regkey = $reg.OpenSubKey($UninstallKey)

#Retrieve an array of string that contain all the subkey names
$subkeys = $regkey.GetSubKeyNames()

#Open each Subkey and use GetValue Method to return the required values for each
foreach ($key in $subkeys) {
    $thisKey = $UninstallKey + "\\" + $key
    $thisSubKey = $reg.OpenSubKey($thisKey)
    $obj = New-Object PSObject
    # $obj | Add-Member -MemberType NoteProperty -Name “ComputerName” -Value $computername
    $obj | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($thisSubKey.GetValue("DisplayName"))
    $obj | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $($thisSubKey.GetValue("DisplayVersion"))
    $obj | Add-Member -MemberType NoteProperty -Name "Publisher" -Value $($thisSubKey.GetValue("Publisher"))
    $obj | Add-Member -MemberType NoteProperty -Name "Version" -Value $($thisSubKey.GetValue("Version"))
    $array += $obj
}

$array | Where-Object { $_.Publisher -eq "Environmental Systems Research Institute, Inc." } | select-object DisplayName, DisplayVersion, Publisher, Version | ft -auto

Steps to Reproduce

Important Factoids

References

shailesh91 commented 4 years ago

@HakonD thanks for this great suggestion. We will make appropriate fixes on the lines you have suggested in the upcoming releases.

cameronkroeker commented 4 years ago

@HakonD we have included changes for this in release v.3.1.1:

https://github.com/Esri/arcgis-powershell-dsc/releases/tag/v3.1.1

Closing this issue, but if this is still being encountered please feel free to reopen.