windows_uninstall

in chef/cookbooks/cpe_osquery/resources/cpe_osquery.rb [453:498]


  def windows_uninstall
    
    download_package if node['packages'].key?(pkg_name)

    
    windows_package "uninstall #{pkg_name}" do
      source pkg_filepath
      checksum pkg_checksum
      action :remove
      options '/qn /norestart'
      only_if { node['packages'].key?(pkg_name) && ::File.exists?(pkg_filepath) }
    end
    
    powershell_script 'uninstall osquery - force cleanup' do
      code <<-PSCRIPT
      $serviceName = 'osqueryd'
      $serviceDescription = 'osquery daemon service'
      $progFiles = [System.Environment]::GetEnvironmentVariable('ProgramFiles')
      $targetFolder = Join-Path $progFiles 'osquery'
      
      
      $oldPath = [System.Environment]::GetEnvironmentVariable('Path', 'Machine')
      if ($oldPath -imatch [regex]::escape($targetFolder)) {
        $newPath = $oldPath -replace [regex]::escape($targetFolder), $NULL
        [System.Environment]::SetEnvironmentVariable('Path', $newPath, 'Machine')
      }
      if ((Get-Service $serviceName -ErrorAction SilentlyContinue)) {
        Stop-Service $serviceName
        
        $proc = Get-Process | Where-Object { $_.ProcessName -eq 'osqueryd' }
        if ($null -ne $proc) {
          Stop-Process -Force $proc -ErrorAction SilentlyContinue
        }
        Set-Service $serviceName -startuptype 'manual'
        Get-CimInstance -ClassName Win32_Service -Filter "Name='osqueryd'" | Invoke-CimMethod -methodName Delete
      }
      if (Test-Path $targetFolder) {
        Remove-Item -Force -Recurse $targetFolder
      } else {
        Write-Debug 'osquery was not found on the system. Nothing to do.'
      }
      PSCRIPT
      only_if { ::File.directory?(::File.join(ENV['ProgramFiles'], 'osquery')) }
    end
  end