I have the below script that outputs Product_Name, Product_Version and Package_Code. I need to export to csv Product_Name, Product_Version and Package_Code. I've attempted this from within the functions in the script and also by calling it outside the script. I'm having no luck. This is a pretty large script but I do not see a file attachment option so I have pasted it below. Problem is with the last function in the script Add-MsiPackageCodeToCsv.
On a side note this is my first post ever on a technical forum of any kind. I'm a noob for the most part to scripting and posting on forums such as this. To get the script working you must define a path to a msi file which is the last line in the script.
function Get-MsiInformation {
param (
[IO.FileInfo] $FilePath = $(throw "Parameter -FilePath [IO.FileInfo] is required.")
)
$packageCode = Get-MsiPackageCode $FilePath
$productCode = Get-MsiProductCode $FilePath
$version = Get-MsiProductVersion $FilePath
$productName = Get-MsiProductName $FilePath
$upgradeCode = Get-MsiUpgradeCode $FilePath
#$PackageCodeToCsv = Add-MsiPackageCodeToCsv $FilePath
return @{
PackageCode = $packageCode;
ProductCode = $productCode[1];
ProductVersion = $version[1];
ProductName = $productName[1];
UpgradeCode = $upgradeCode[1];
}
}
function Get-MsiUpgradeCode {
param (
[IO.FileInfo] $FilePath = $(throw "Parameter -FilePath [IO.FileInfo] is required.")
)
if (!(Test-Path $FilePath)){
Write-Error "FilePath $FilePath does not exist"
exit 1
}
$windowsInstaller = New-Object -com WindowsInstaller.Installer
$database = $windowsInstaller.GetType().InvokeMember("OpenDatabase", "InvokeMethod", $Null, $windowsInstaller, @($FilePath.FullName, 0))
$q = "SELECT Value FROM Property WHERE Property = 'UpgradeCode'"
$View = $database.GetType().InvokeMember(
"OpenView", "InvokeMethod", $Null, $database, ($q)
)
$View.GetType().InvokeMember("Execute", "InvokeMethod", $Null, $View, $Null)
$record = $View.GetType().InvokeMember(
"Fetch", "InvokeMethod", $Null, $View, $Null
)
$upgradeCode = $record.GetType().InvokeMember(
"StringData", "GetProperty", $Null, $record, 1
)
return $upgradeCode
}
function Get-MsiProductName {
param (
[IO.FileInfo] $FilePath = $(throw "Parameter -FilePath [IO.FileInfo] is required.")
)
if (!(Test-Path $FilePath)){
Write-Error "FilePath $FilePath does not exist"
exit 1
}
$windowsInstaller = New-Object -com WindowsInstaller.Installer
$database = $windowsInstaller.GetType().InvokeMember("OpenDatabase", "InvokeMethod", $Null, $windowsInstaller, @($FilePath.FullName, 0))
$q = "SELECT Value FROM Property WHERE Property = 'ProductName'"
$View = $database.GetType().InvokeMember(
"OpenView", "InvokeMethod", $Null, $database, ($q)
)
$View.GetType().InvokeMember("Execute", "InvokeMethod", $Null, $View, $Null)
$record = $View.GetType().InvokeMember(
"Fetch", "InvokeMethod", $Null, $View, $Null
)
$productName = $record.GetType().InvokeMember(
"StringData", "GetProperty", $Null, $record, 1
)
return $productName
}
function Get-MsiProductCode {
param (
[IO.FileInfo] $FilePath = $(throw "Parameter -FilePath [IO.FileInfo] is required.")
)
if (!(Test-Path $FilePath)){
Write-Error "FilePath $FilePath does not exist"
exit 1
}
$windowsInstaller = New-Object -com WindowsInstaller.Installer
$database = $windowsInstaller.GetType().InvokeMember("OpenDatabase", "InvokeMethod", $Null, $windowsInstaller, @($FilePath.FullName, 0))
$q = "SELECT Value FROM Property WHERE Property = 'ProductCode'"
$View = $database.GetType().InvokeMember(
"OpenView", "InvokeMethod", $Null, $database, ($q)
)
$View.GetType().InvokeMember("Execute", "InvokeMethod", $Null, $View, $Null)
$record = $View.GetType().InvokeMember(
"Fetch", "InvokeMethod", $Null, $View, $Null
)
$productCode = $record.GetType().InvokeMember(
"StringData", "GetProperty", $Null, $record, 1
)
return [System.Guid] $productCode
}
function Get-MsiProductVersion {
param (
[IO.FileInfo] $FilePath = $(throw "Parameter -FilePath [IO.FileInfo] is required.")
)
if (!(Test-Path $FilePath)){
Write-Error "FilePath $FilePath does not exist"
exit 1
}
$windowsInstaller = New-Object -com WindowsInstaller.Installer
$database = $windowsInstaller.GetType().InvokeMember("OpenDatabase", "InvokeMethod", $Null, $windowsInstaller, @($FilePath.FullName, 0))
$q = "SELECT Value FROM Property WHERE Property = 'ProductVersion'"
$View = $database.GetType().InvokeMember(
"OpenView", "InvokeMethod", $Null, $database, ($q)
)
$View.GetType().InvokeMember("Execute", "InvokeMethod", $Null, $View, $Null)
$record = $View.GetType().InvokeMember(
"Fetch", "InvokeMethod", $Null, $View, $Null
)
$productVersion = $record.GetType().InvokeMember(
"StringData", "GetProperty", $Null, $record, 1
)
return $productVersion
}
function Get-MsiPackageCode {
param (
[IO.FileInfo] $FilePath = $(throw "Parameter -FilePath [IO.FileInfo] is required.")
)
if (!(Test-Path $FilePath)){
Write-Error "FilePath $FilePath does not exist"
exit 1
}
try {
$windowsInstaller = New-Object -com WindowsInstaller.Installer
$database = $windowsInstaller.GetType().InvokeMember("OpenDatabase", "InvokeMethod", $Null, $windowsInstaller, @($FilePath.FullName, 0))
$summaryInfo = $database.GetType().InvokeMember("SummaryInformation", "GetProperty", $Null, $database, $Null)
$packageCode = [System.Guid]($summaryInfo.GetType().InvokeMember("Property", "GetProperty", $Null, $summaryInfo, 9))
return $packageCode
} catch {
throw "Failed to get MSI package code the error was {0}." -f $_
}
}
function Add-MsiPackageCodeToCsv {
param (
[System.String] $IsGa = $(throw "Parameter -IsGa [System.String] is required."),
[IO.FileInfo] $MsiFilePath = $(throw "Parameter -MsiFilePath [IO.FileInfo] is required.")
)
$packageInfo = Get-MsiInformation $MsiFilePath
$productName = $packageInfo.Get_Item("ProductName")
$productVersion = $packageInfo.Get_Item("ProductVersion")
$packageCode = $packageInfo.Get_Item("PackageCode")
$filepath = "C:\test.csv"
$content = [System.String](Get-Content $filepath)
if ($content.Contains($PackageCode)) {
Write-Error "$PackageCode already exists in list of current package codes"
exit 1
}
Add-MsiPackageCodeToCsv | Select-object ProductName,ProductVersion,PackageCode | export-csv -path C:\Temp.csv
#Add-Content $filepath "$productName,$productVersion,$IsGa,$packageCode"
}
Add-MsiPackageCodeToCsv | Select-object ProductName,ProductVersion,PackageCode | export-csv -path "C:\Temp.csv"
Get-MsiInformation "C:\path\to\msi\file\app_name.msi"