I have the below function:
Function Get-MsiProperty
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)]
[ValidateScript({$_ | Test-Path -PathType Leaf})]
[string]$Path,
[Parameter(Mandatory=$true)]
[string]$Property
)
Begin
{
$GetProperty = {
Param
(
$Object,
$PropertyName,
[object[]]$ArgumentList
)
Return $Object.GetType().InvokeMember($PropertyName, 'Public, Instance, GetProperty', $null, $Object, $ArgumentList)
}
$InvokeMethod = {
Param
(
$Object,
$MethodName,
$ArgumentList
)
Return $Object.GetType().InvokeMember($MethodName, 'Public, Instance, InvokeMethod', $null, $Object, $ArgumentList)
}
${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
$PSParameters = $PSBoundParameters | Format-Table -Auto | Out-String
}
Process
{
Try
{
Set-StrictMode -Version Latest
# http://msdn.microsoft.com/en-us/library/aa369432(v=vs.85).aspx
$msiOpenDatabaseModeReadOnly = 0
$Installer = New-Object -ComObject WindowsInstaller.Installer -ErrorAction 'Stop'
$Database = &$InvokeMethod -Object $Installer -MethodName OpenDatabase -ArgumentList @($Path, $msiOpenDatabaseModeReadOnly)
$View = &$InvokeMethod -Object $Database -MethodName OpenView -ArgumentList @("SELECT Value FROM Property WHERE Property='$Property'")
&$InvokeMethod -Object $View -MethodName Execute | Out-Null
$MSIProperty = $null
$Record = &$InvokeMethod -Object $View -MethodName Fetch
If ($Record)
{
$MSIProperty = &$GetProperty -Object $Record -PropertyName StringData -ArgumentList 1
}
Write-Output $MSIProperty
}
Catch
{
Write-Host -Message "Failed to get the MSI property [$Property]"
}
Finally
{
&$InvokeMethod -Object $View -MethodName Close -ArgumentList @() | Out-Null
}
}
}
If I call the function as below, I get the correct result:
$ProductCode = Get-MsiProperty -Path "ConfigMgrTools.msi" -Property 'ProductCode'
If I call the function as below, the result has a space before and after the result. Why does this happen? I have used Get-Member to analyze the variable it shows up as a 'system.string' both times.
[string]$ProductCode = Get-MsiProperty -Path "ConfigMgrTools.msi" -Property 'ProductCode'