powershell PowerShell:Test-WIProductCode - 测试Windows Installer(又名MSI)ProductCode存在

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell PowerShell:Test-WIProductCode - 测试Windows Installer(又名MSI)ProductCode存在相关的知识,希望对你有一定的参考价值。

function Test-WIProductCode {
    [CmdletBinding(SupportsShouldProcess=$True,DefaultParameterSetName="None")]
    PARAM(
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidateScript({$_ -match ("^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$")})] 
        [guid] 
        $ProductCode
    )

    BEGIN {
        Write-Verbose "[$($MyInvocation.MyCommand)] Entering..."
        Write-Verbose "[$($MyInvocation.MyCommand)] ProductCode: $($ProductCode.ToString('B').ToUpper())"
    }

    PROCESS {
        Write-Verbose "[$($MyInvocation.MyCommand)] Creating WindowsInstaller.Installer ComObject"
        $Installer = New-Object -ComObject WindowsInstaller.Installer
        try {
            Write-Verbose "[$($MyInvocation.MyCommand)] Successfully created WindowsInstaller.Installer ComObject."
            Write-Verbose "[$($MyInvocation.MyCommand)] Getting WindowsInstaller.Installer Type"
            $Type = $Installer.GetType()
            Write-Verbose "[$($MyInvocation.MyCommand)] Getting WindowsInstaller.Installer Products"
            $ProductCodes = @($Type.InvokeMember('Products', [System.Reflection.BindingFlags]::GetProperty, $null, $Installer, $null))
            Write-Verbose "[$($MyInvocation.MyCommand)] $($ProductCodes.Count) WindowsInstaller.Installer Products Discovered"
            Write-Verbose "[$($MyInvocation.MyCommand)] Searching for WindowsInstaller.Installer Product"
            if ($ProductCodes -contains "$($ProductCode.ToString('B').ToUpper())") {
                Write-Verbose "[$($MyInvocation.MyCommand)] WindowsInstaller.Installer Product found."
                $true
            } else {
                Write-Verbose "[$($MyInvocation.MyCommand)] WindowsInstaller.Installer Product not found."
                $false
            }
        } catch {
            Write-Verbose "[$($MyInvocation.MyCommand)] Failed to create WindowsInstaller.Installer ComObject."
            $false
        }
    }

    END {
        if ($Installer -is [System.__ComObject]) {
            try {
	            [System.Runtime.InteropServices.Marshal]::ReleaseComObject($Installer) | Out-Null
	            [System.GC]::Collect()
	            [System.GC]::WaitForPendingFinalizers()
                Write-Verbose "[$($MyInvocation.MyCommand)] Successfully released ComObject"
            } catch {
                Write-Verbose "[$($MyInvocation.MyCommand)] Failed to release ComObject"
            }
        }
    }

}
function Get-WIProductCodes {
    [CmdletBinding(SupportsShouldProcess=$True,DefaultParameterSetName="None")]
    PARAM(
    )

    BEGIN {
        Write-Verbose "[$($MyInvocation.MyCommand)] Entering..."
    }

    PROCESS {
        Write-Verbose "[$($MyInvocation.MyCommand)] Creating WindowsInstaller.Installer ComObject"
        $Installer = New-Object -ComObject WindowsInstaller.Installer
        try {
            Write-Verbose "[$($MyInvocation.MyCommand)] Successfully created WindowsInstaller.Installer ComObject."
            Write-Verbose "[$($MyInvocation.MyCommand)] Getting WindowsInstaller.Installer Type"
            $Type = $Installer.GetType()
            Write-Verbose "[$($MyInvocation.MyCommand)] Getting WindowsInstaller.Installer Products"
            $ProductCodes = @($Type.InvokeMember('Products', [System.Reflection.BindingFlags]::GetProperty, $null, $Installer, $null))
			$ProductCodes | Sort
		} catch {
            Write-Verbose "[$($MyInvocation.MyCommand)] Failed to create WindowsInstaller.Installer ComObject."
            $false
        }
    }

    END {
    }
}

以上是关于powershell PowerShell:Test-WIProductCode - 测试Windows Installer(又名MSI)ProductCode存在的主要内容,如果未能解决你的问题,请参考以下文章

powershell PowerShell:启动PowerShell作为其他用户提升

powershell [提示输入powershell] #powershell #input #prompt

powershell 测量PowerShell命令或PowerShell脚本的速度

powershell远程下载exe并执行

powershell 使用啥端口

powershell PowerShell使用PowerShell创建本地管理员帐户