powershell PowerShell:Get-MsiProducts /获取Windows Installer产品

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell PowerShell:Get-MsiProducts /获取Windows Installer产品相关的知识,希望对你有一定的参考价值。

#region Windows Installer
	function Get-MsiProducts {
		[CmdletBinding(SupportsShouldProcess=$True,DefaultParameterSetName="None")]
		PARAM()
		$Installer = New-Object -ComObject WindowsInstaller.Installer
		$Type = $Installer.GetType()
		$Products = $Type.InvokeMember('Products', [System.Reflection.BindingFlags]::GetProperty, $null, $Installer, $null)
		$MsiProducts = foreach ($Product In $Products) {
			try {
				$MsiProduct = New-Object -TypeName PSObject -Property @{
					ProductCode = $Product
				}
				$MsiProperties = @('Language', 'ProductName', 'PackageCode', 'Transforms', 'AssignmentType', 'PackageName', 'InstalledProductName', 'VersionString', 'RegCompany', 'RegOwner', 'ProductID', 'ProductIcon', 'InstallLocation', 'InstallSource', 'InstallDate', 'Publisher', 'LocalPackage', 'HelpLink', 'HelpTelephone', 'URLInfoAbout', 'URLUpdateInfo')		
				foreach ($MsiProperty In $MsiProperties) {
					$MsiProduct | Add-Member -MemberType NoteProperty -Name $MsiProperty -Value $Type.InvokeMember('ProductInfo', [System.Reflection.BindingFlags]::GetProperty, $null, $Installer, @($Product, $MsiProperty))
				}
				$MsiProduct | Add-Member -MemberType ScriptProperty -Name 'ProductVersion' -Value {$this.VersionString}
				$MsiProduct | Add-Member -MemberType ScriptProperty -Name 'Manufacturer' -Value {$this.Publisher}
				
				$MsiProduct
			} catch [System.Exception] {
				#$error[0]|format-list –force
			}
		}
		$MsiProducts | Sort ProductName
	}
	
	function Get-WindowsInstallerTableData {
		[CmdletBinding(SupportsShouldProcess=$True,DefaultParameterSetName="None")]
	    PARAM (
	        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,HelpMessage="MSI Database Filename",ValueFromPipeline=$true)]
	        [Alias("Database","Msi")]
			[ValidateScript({Test-Path $_ -PathType 'Leaf'})]
			[System.IO.FileInfo]
	        $MsiDbPath
			,
	        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,HelpMessage="MST Database Filename",ValueFromPipeline=$true)]
	        [Alias("Transform","Mst")]
			[System.IO.FileInfo[]]
	        $MstDbPath
			,
	        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,HelpMessage="SQL Query",ValueFromPipeline=$true)]
	        [Alias("Query")]
			[String]
	        $Table
	    )

		begin {
			# Add Required Type Libraries
			Add-Type -Path "$(Split-Path $Script:MyInvocation.MyCommand.Path)\Microsoft.Deployment.WindowsInstaller.dll";
		}
		
		process {
			# Open an MSI Database
			$Database = New-Object Microsoft.Deployment.WindowsInstaller.Database $MsiDbPath;
			# ApplyTransforms
			foreach ($MstDbPathEx In $MstDbPath) {
				if (Test-Path -Path $MstDbPathEx -PathType Leaf) {
					$Database.ApplyTransform($MstDbPathEx);
				}
			}
			
			#Create a View object
			# SELECT `Message` FROM `Error` WHERE `Error` = 1715 
			# [Microsoft.Deployment.WindowsInstaller.View]
			$_ColumnsView = $Database.OpenView("SELECT * FROM ``_Columns`` WHERE ``Table`` = '$($Table)'");
			if ($_ColumnsView) {
				# Execute the View object
				$_ColumnsView.Execute()
				# Place the objects in a PSObject
				$_Columns = @()
				$_ColumnsRow = $_ColumnsView.Fetch()
				while($_ColumnsRow -ne $null) {
					$hash = @{
						'Table' = $_ColumnsRow.GetString(1)
						'Number' = $_ColumnsRow.GetString(2)
						'Name' = $_ColumnsRow.GetString(3)
						'Type' = $_ColumnsRow.GetString(4)
					}
					$_Columns += New-Object -TypeName PSObject -Property $hash
					
					$_ColumnsRow = $_ColumnsView.Fetch()
				}

				$FieldNames = $_Columns | Select -ExpandProperty Name
			}
			
			if ($FieldNames) {
				# [Microsoft.Deployment.WindowsInstaller.View]
				$TableView = $Database.OpenView("SELECT * FROM ``$($Table)``");
				# Execute the View object
				$TableView.Execute()
				# Place the objects in a PSObject
				$Rows = @()
				# Fetch the first record
				$Row = $TableView.Fetch()
				while($Row -ne $null) {
					$hash = @{}
					foreach ($FieldName In $FieldNames) {
						$hash += @{
							$FieldName = $Row.Item($FieldName)
						}
					}
					$Rows += New-Object -TypeName PSObject -Property $hash
					
					# Fetch the next record
					$Row = $TableView.Fetch()
				}
				$Rows
			}
		}
		
		end {
			#Close the Database & View
			if ($_ColumnsView) {$_ColumnsView.Close();}
			if ($TableView) {$TableView.Close();}
			if ($Database) {$Database.Dispose();}
		}
	}
#endregion
function Get-MsiProducts {
	$Installer = New-Object -ComObject WindowsInstaller.Installer
	$Type = $Installer.GetType()
	$Products = $Type.InvokeMember('Products', [System.Reflection.BindingFlags]::GetProperty, $null, $Installer, $null)
	foreach ($Product In $Products) {
		$hash = @{}
		$hash.ProductCode = $Product
		$Attributes = @('Language', 'ProductName', 'PackageCode', 'Transforms', 'AssignmentType', 'PackageName', 'InstalledProductName', 'VersionString', 'RegCompany', 'RegOwner', 'ProductID', 'ProductIcon', 'InstallLocation', 'InstallSource', 'InstallDate', 'Publisher', 'LocalPackage', 'HelpLink', 'HelpTelephone', 'URLInfoAbout', 'URLUpdateInfo')		
		foreach ($Attribute In $Attributes) {
			$hash."$($Attribute)" = $null
		}
		foreach ($Attribute In $Attributes) {
			try {
				$hash."$($Attribute)" = $Type.InvokeMember('ProductInfo', [System.Reflection.BindingFlags]::GetProperty, $null, $Installer, @($Product, $Attribute))
			} catch [System.Exception] {
				#$error[0]|format-list –force
			}
		}
		New-Object -TypeName PSObject -Property $hash
	}
}

以上是关于powershell PowerShell:Get-MsiProducts /获取Windows Installer产品的主要内容,如果未能解决你的问题,请参考以下文章

powershell PowerShell:Get-Handles

powershell PowerShell:Get-MicrosoftUpdates

powershell PowerShell:Get-NetPrefixPolicy

powershell PowerShell:Get-FileHash

powershell PowerShell:Get-ComparableVersion

powershell PowerShell:Get-OSBitness