internal void _RegScan(RegistryHive RegHive, RegistryView RegView, List <AddSoftware> lScanList) { try { RegistryKey oUBase = RegistryKey.OpenBaseKey(RegHive, RegView); RegistryKey oUKey = oUBase.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall", false); List <string> USubKeys = new List <string>(); USubKeys.AddRange(oUKey.GetSubKeyNames()); foreach (string sProdID in USubKeys) { try { RegistryKey oRegkey = oUKey.OpenSubKey(sProdID); bool bSystemComponent = Convert.ToBoolean(oRegkey.GetValue("SystemComponent", 0)); bool bWindowsInstaller = Convert.ToBoolean(oRegkey.GetValue("WindowsInstaller", 0)); string sParent = oRegkey.GetValue("ParentKeyName", "").ToString(); string sRelease = oRegkey.GetValue("ReleaseType", "").ToString(); //Check if its a SystemComponent or WindowsInstaller if (bSystemComponent) { continue; } //Check if NO PrentKeyName exists if (!string.IsNullOrEmpty(sParent)) { continue; } //Check if NO ReleaseType exists if (!string.IsNullOrEmpty(sRelease)) { continue; } AddSoftware oItem = GetSWPropertiesAsync(oUKey.OpenSubKey(sProdID)).Result; if (!string.IsNullOrEmpty(oItem.ProductName)) { try { lock (lScanList) { lScanList.Add(oItem); } } catch (Exception ex) { ex.Message.ToString(); } } } catch { } } } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
public static bool UploadSWEntry(AddSoftware lSoftware) { try { JavaScriptSerializer ser = new JavaScriptSerializer(); HttpContent oCont = new StringContent(ser.Serialize(lSoftware), Encoding.UTF8, "application/json"); var response = oClient.PostAsync(sURL + "/rest/v2/uploadswentry", oCont); response.Wait(30000); //30s max if (response.Result.StatusCode == HttpStatusCode.OK) { return(true); } else { return(false); } } catch { } return(false); }
internal async Task <AddSoftware> GetSWPropertiesAsync(RegistryKey oRegkey) { return(await Task.Run(() => { AddSoftware oResult = new AddSoftware(); Version oVer = null; bool bVersion = false; oResult.PSPreReq = "$true"; if (oRegkey.View == RegistryView.Registry32) { oResult.Architecture = "X86"; } else { oResult.Architecture = "X64"; oResult.PSPreReq = "[Environment]::Is64BitProcess"; } string sMSI = oRegkey.Name.Split('\\').Last(); string EncKey = ""; if (sMSI.StartsWith("{") && sMSI.EndsWith("}")) { bool bIsMSI = true; EncKey = descramble(sMSI.Substring(1, 36).Replace("-", "")); try { RegistryKey oBase = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, oRegkey.View); RegistryKey oKey = oBase.OpenSubKey(@"SOFTWARE\Classes\Installer\Products\" + EncKey, false); if (oKey == null) { bIsMSI = false; } } catch { bIsMSI = false; } if (bIsMSI) { oResult.MSIProductID = sMSI; oResult.PSUninstall = "$proc = (Start-Process -FilePath \"msiexec.exe\" -ArgumentList \"/x " + sMSI + " /qn REBOOT=REALLYSUPPRESS \" -Wait -PassThru);$proc.WaitForExit();$ExitCode = $proc.ExitCode"; oResult.PSDetection = @"Test-Path 'HKLM:\SOFTWARE\Classes\Installer\Products\" + EncKey + "'"; try { RegistryKey oSource = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\Installer\Products\" + EncKey + "\\SourceList"); if (oSource != null) { oResult.PSInstall = "$proc = (Start-Process -FilePath \"msiexec.exe\" -ArgumentList \"/i `\"" + oSource.GetValue("PackageName") + "`\" /qn ALLUSERS=2 REBOOT=REALLYSUPPRESS\" -Wait -PassThru);$proc.WaitForExit();$ExitCode = $proc.ExitCode"; } } catch { try { string sVer = oRegkey.GetValue("DisplayVersion", "").ToString(); if (Version.TryParse(sVer, out oVer)) { oResult.PSDetection = @"if(([version](Get-ItemPropertyValue -path '" + oRegkey.Name.Replace("HKEY_LOCAL_MACHINE", "HKLM:") + "' -Name DisplayVersion -ea SilentlyContinue)) -ge '" + sVer + "') { $true } else { $false }"; } else { oResult.PSDetection = @"if((Get-ItemPropertyValue -path '" + oRegkey.Name.Replace("HKEY_LOCAL_MACHINE", "HKLM:") + "' -Name DisplayVersion -ea SilentlyContinue) -eq '" + sVer + "') { $true } else { $false }"; } } catch { } oResult.PSInstall = "$proc = (Start-Process -FilePath \"msiexec.exe\" -ArgumentList \"/i `\"<PackageName.msi>`\" /qn ALLUSERS=2 REBOOT=REALLYSUPPRESS\" -Wait -PassThru);$proc.WaitForExit();$ExitCode = $proc.ExitCode"; } } else { oResult.PSInstall = "$proc = (Start-Process -FilePath \"setup.exe\" -ArgumentList \"/?\" -Wait -PassThru);$proc.WaitForExit();$ExitCode = $proc.ExitCode"; string sVer = oRegkey.GetValue("DisplayVersion", "").ToString(); if (Version.TryParse(sVer, out oVer)) //check if its a Version { bVersion = true; } if (Environment.Is64BitOperatingSystem && oRegkey.View == RegistryView.Registry32) { if (bVersion) { oResult.PSDetection = @"if(([version](Get-ItemPropertyValue -path '" + oRegkey.Name.ToUpper().Replace("HKEY_LOCAL_MACHINE", "HKLM:").Replace("SOFTWARE\\", "SOFTWARE\\WOW6432NODE\\") + "' -Name DisplayVersion -ea SilentlyContinue)) -ge '" + sVer + "') { $true } else { $false }"; } else { oResult.PSDetection = @"if((Get-ItemPropertyValue -path '" + oRegkey.Name.ToUpper().Replace("HKEY_LOCAL_MACHINE", "HKLM:").Replace("SOFTWARE\\", "SOFTWARE\\WOW6432NODE\\") + "' -Name DisplayVersion -ea SilentlyContinue) -eq '" + sVer + "') { $true } else { $false }"; } } else { if (bVersion) { oResult.PSDetection = @"if(([version](Get-ItemPropertyValue -path '" + oRegkey.Name.ToUpper().Replace("HKEY_LOCAL_MACHINE", "HKLM:") + "' -Name DisplayVersion -ea SilentlyContinue)) -ge '" + sVer + "') { $true } else { $false }"; } else { oResult.PSDetection = @"if((Get-ItemPropertyValue -path '" + oRegkey.Name.ToUpper().Replace("HKEY_LOCAL_MACHINE", "HKLM:") + "' -Name DisplayVersion -ea SilentlyContinue) -eq '" + sVer + "') { $true } else { $false }"; } } } } else { string sVer = oRegkey.GetValue("DisplayVersion", "").ToString(); if (Version.TryParse(sVer, out oVer)) //check if its a Version { bVersion = true; } oResult.PSInstall = "$proc = (Start-Process -FilePath \"setup.exe\" -ArgumentList \"/?\" -Wait -PassThru);$proc.WaitForExit();$ExitCode = $proc.ExitCode"; if (Environment.Is64BitOperatingSystem && oRegkey.View == RegistryView.Registry32) { if (bVersion) { oResult.PSDetection = @"if(([version](Get-ItemPropertyValue -path '" + oRegkey.Name.ToUpper().Replace("HKEY_LOCAL_MACHINE", "HKLM:").Replace("SOFTWARE\\", "SOFTWARE\\WOW6432NODE\\") + "' -Name DisplayVersion -ea SilentlyContinue)) -ge '" + sVer + "') { $true } else { $false }"; } else { oResult.PSDetection = @"if((Get-ItemPropertyValue -path '" + oRegkey.Name.ToUpper().Replace("HKEY_LOCAL_MACHINE", "HKLM:").Replace("SOFTWARE\\", "SOFTWARE\\WOW6432NODE\\") + "' -Name DisplayVersion -ea SilentlyContinue) -eq '" + sVer + "') { $true } else { $false }"; } } else { if (bVersion) { oResult.PSDetection = @"if(([version](Get-ItemPropertyValue -path '" + oRegkey.Name.ToUpper().Replace("HKEY_LOCAL_MACHINE", "HKLM:") + "' -Name DisplayVersion -ea SilentlyContinue)) -ge '" + sVer + "') { $true } else { $false }"; } else { oResult.PSDetection = @"if((Get-ItemPropertyValue -path '" + oRegkey.Name.ToUpper().Replace("HKEY_LOCAL_MACHINE", "HKLM:") + "' -Name DisplayVersion -ea SilentlyContinue) -eq '" + sVer + "') { $true } else { $false }"; } } } oResult.PSDetection = oResult.PSDetection.Replace("HKEY_LOCAL_MACHINE", "HKLM:"); oResult.PSDetection = oResult.PSDetection.Replace("HKEY_CURRENT_USER", "HKCU:"); oResult.ProductName = oRegkey.GetValue("DisplayName", "").ToString(); oResult.ProductVersion = oRegkey.GetValue("DisplayVersion", "").ToString(); oResult.Manufacturer = oRegkey.GetValue("Publisher", "").ToString().TrimEnd('.'); //If not an MSI try to get Uninstall command from Registry if (string.IsNullOrEmpty(oResult.MSIProductID)) { try { string sUninst = oRegkey.GetValue("QuietUninstallString", "").ToString().Replace("\"", ""); if (!string.IsNullOrEmpty(sUninst)) { try { if (sUninst.IndexOf('/') >= 0) { oResult.PSUninstall = "$proc = (Start-Process -FilePath \"" + sUninst.Split('/')[0] + "\" -ArgumentList \"" + sUninst.Substring(sUninst.IndexOf('/')) + "\" -Wait -PassThru);$proc.WaitForExit();$ExitCode = $proc.ExitCode"; } else { oResult.PSUninstall = "$proc = (Start-Process -FilePath \"" + sUninst + "\" -ArgumentList \"/?\" -Wait -PassThru);$proc.WaitForExit();$ExitCode = $proc.ExitCode"; } //$proc = (Start-Process -FilePath "zps17_en.exe" -ArgumentList "/Silent" -PassThru);$proc.WaitForExit();$ExitCode = $proc.ExitCode } catch { oResult.PSUninstall = "$proc = (Start-Process -FilePath \"" + sUninst + "\" -ArgumentList \"/?\" -Wait -PassThru);$proc.WaitForExit();$ExitCode = $proc.ExitCode"; } } } catch { } } //If no silent uninstall is provided, use the normal uninstall string if (string.IsNullOrEmpty(oResult.PSUninstall)) { if (string.IsNullOrEmpty(oResult.MSIProductID)) { string sUninst = oRegkey.GetValue("UninstallString", "").ToString().Replace("\"", ""); oResult.PSUninstall = "$proc = (Start-Process -FilePath \"" + sUninst + "\" -ArgumentList \"/?\" -Wait -PassThru);$proc.WaitForExit();$ExitCode = $proc.ExitCode"; } } //get Version String if (string.IsNullOrEmpty(oResult.ProductVersion)) { string sVersion = oRegkey.GetValue("Version", "").ToString(); if (!string.IsNullOrEmpty(sVersion)) { try { Int32 iVersion = Convert.ToInt32(sVersion); string sfullval = iVersion.ToString("X8"); sVersion = Convert.ToInt32(sfullval.Substring(0, 2), 16).ToString(); sVersion = sVersion + "." + Convert.ToInt32(sfullval.Substring(2, 2), 16).ToString(); sVersion = sVersion + "." + Convert.ToInt32(sfullval.Substring(4, 4), 16).ToString(); oResult.ProductVersion = sVersion; } catch { } } } //Get Image string sDisplayIcon = oRegkey.GetValue("DisplayIcon", "").ToString().Split(',')[0]; if (!string.IsNullOrEmpty(EncKey)) { try { RegistryKey oKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\Installer\Products\" + EncKey, false); if (oKey != null) { string sIcon = oKey.GetValue("ProductIcon", "") as string; if (!string.IsNullOrEmpty(sIcon)) { sDisplayIcon = sIcon; } } } catch { } } //Add default Icon if (string.IsNullOrEmpty(sDisplayIcon)) { sDisplayIcon = @"C:\windows\system32\msiexec.exe"; } if (!sDisplayIcon.Contains(":\\")) { sDisplayIcon = @"C:\windows\system32\" + sDisplayIcon; } sDisplayIcon = sDisplayIcon.Replace("\"", ""); sDisplayIcon = sDisplayIcon.Split(',')[0]; if (!File.Exists(sDisplayIcon)) { if (File.Exists(sDisplayIcon.Replace(" (x86)", ""))) { sDisplayIcon = sDisplayIcon.Replace(" (x86)", ""); } else { sDisplayIcon = ""; } } if (!string.IsNullOrEmpty(sDisplayIcon)) { oResult.Image = imageToByteArray(GetImageFromExe(sDisplayIcon.Replace("\"", ""))); } oResult.Architecture = "X64"; return oResult; })); }
internal void scan(Intune_API oAPI) { try { var lIDs = oAPI.getRZIDs(); //File.AppendAllLines(Environment.ExpandEnvironmentVariables("%TEMP%\\dbg.txt"), new string[] { "Repository-Items:" + SoftwareRepository.Count.ToString() }); #if DEBUG System.IO.File.AppendAllLines(Environment.ExpandEnvironmentVariables("%TEMP%\\RZDebug.txt"), new string[] { DateTime.Now.ToString() + ";S0;" + "RZItems detected: ", lIDs.Count.ToString() }); #endif #if DEBUG System.IO.File.AppendAllLines(Environment.ExpandEnvironmentVariables("%TEMP%\\RZDebug.txt"), new string[] { DateTime.Now.ToString() + ";S0;" + "Repository Items: ", SoftwareRepository.Count().ToString() }); #endif foreach (Intune_API.GraphRZ RZSW in lIDs) { try { if (SoftwareRepository.Count(t => t.SWId == RZSW.RZID) == 0) { //File.AppendAllLines(Environment.ExpandEnvironmentVariables("%TEMP%\\dbg.txt"), new string[] { "not match, IconId:" + SQLRZ.RZID.ToString() }); var oSW = SoftwareRepository.FirstOrDefault(t => t.ShortName == RZSW.Shortname); if (oSW != null) { AddSoftware oNew = new AddSoftware() { ProductName = oSW.ProductName, ProductVersion = oSW.ProductVersion, Manufacturer = oSW.Manufacturer, ShortName = oSW.ShortName, Description = oSW.Description, SWId = oSW.SWId, IconHash = oSW.IconHash, MSIProductID = RZSW.Version }; //if (RZSW.Bootstrap) // oNew.Author = "BootstrapTrue"; //else // oNew.Author = "BootstrapFalse"; #if DEBUG System.IO.File.AppendAllLines(Environment.ExpandEnvironmentVariables("%TEMP%\\RZDebug.txt"), new string[] { DateTime.Now.ToString() + ";S1;" + "New SWVersion: ", oNew.ProductName, oNew.ProductVersion, oNew.MSIProductID }); #endif NewSoftwareVersions.Add(oNew); } } else { try { var oSW = SoftwareRepository.FirstOrDefault(t => t.SWId == RZSW.RZID); if (oSW != null) { try { AddSoftware oExisting = new AddSoftware() { ProductName = oSW.ProductName, ProductVersion = oSW.ProductVersion, Manufacturer = oSW.Manufacturer, ShortName = oSW.ShortName, Description = oSW.Description, IconHash = oSW.IconHash, SWId = oSW.SWId }; //if (RZSW.Bootstrap) // oExisting.Author = "BootstrapTrue"; //else // oExisting.Author = "BootstrapFalse"; #if DEBUG System.IO.File.AppendAllLines(Environment.ExpandEnvironmentVariables("%TEMP%\\RZDebug.txt"), new string[] { DateTime.Now.ToString() + ";S2;" + "Installed SWVersion: ", oExisting.ProductName, oExisting.ProductVersion }); #endif InstalledSoftware.Add(oExisting); } catch { } } } catch { } } } catch (Exception ex) { System.IO.File.AppendAllLines(Environment.ExpandEnvironmentVariables("%TEMP%\\RZError.txt"), new string[] { DateTime.Now.ToString() + ";F1814E1" + ex.Message }); } } //Cleanup SW where new Version already exists foreach (var oSW in InstalledSoftware) { NewSoftwareVersions.RemoveAll(t => t.ShortName == oSW.ShortName); } } catch (Exception ex) { System.IO.File.AppendAllLines(Environment.ExpandEnvironmentVariables("%TEMP%\\RZError.txt"), new string[] { DateTime.Now.ToString() + ";F1845E1" + ex.Message }); } OnUpdScanCompleted(this, new EventArgs()); OnSWScanCompleted(this, new EventArgs()); }