Custom Function - Export MSI Package Codes to CSV File

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Chicken Pecker Chicken Pecker 1 year, 11 months ago.

  • Author
    Posts
  • #21341
    Profile photo of Chicken Pecker
    Chicken Pecker
    Participant

    I am a noob who has been handed the attached power shell script and instructed to append the output to a csv file. It sounds simple enough but I am making little to no progress on it. In order to run the script you will need to define a path to a msi package – this is done on the very last line of the script. When you execute the script it will output Product_Name, Product_Version and Package_Code. I need to export and append those 3 items to a csv file. When I execute the export function I receive a parameter error that bombs the script out.

    Within the script I have commented out the lines of code I have attempted adding/manipulating to try and make this work. Any help or direction on this would be much appreciated. Thank You in Advance.

    ***NOTE*** The script will run on any msi file but you must specify the path at the end of the script:
    Get-MsiInformation "C:\path\to\msi\file\app_name.msi"

  • #21343
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Please replace the Add-MsiPackageCodeToCsv function in your script with below:

    function Add-MsiPackageCodeToCsv {
    
        Param (
            [String] $IsGa = $(throw "Parameter -IsGa [System.String] is required."),
            [IO.FileInfo] $MsiFilePath = $(throw "Parameter -MsiFilePath [IO.FileInfo] is required."),
            [String] $CsvFilePath = "C:\test.csv"
        )
        
        $PackageInfo = Get-MsiInformation $MsiFilePath
        $PackageInfo['IsGa'] = $IsGa
       
        try {
    
            if (Test-Path -Path $CsvFilePath) {
    
                $PackageList = @(Import-Csv -Path $CsvFilePath)
        
                if ($PackageList.PackageCode -contains $PackageInfo.PackageCode) {
    
                  throw "$($PackageInfo.PackageCode) already exists in list of current package codes"
                }
            }
    
            $PackageList += [pscustomobject]$PackageInfo | Select-Object -Property ProductName, ProductVersion, PackageCode, IsGa
            
            $PackageList | Export-Csv -Path $CsvFilePath -NoTypeInformation
        }
        catch
        {
            Write-Error -Message $_
        }
    }
    
    Add-MsiPackageCodeToCsv -IsGa 'Yes' -MsiFilePath "C:\path\to\msi\file\app_name1.msi"
    Add-MsiPackageCodeToCsv -IsGa 'Yes' -MsiFilePath "C:\path\to\msi\file\app_name2.msi"
    
  • #21344
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    I think you are over complicating it. When I ran the code, it returned a hash table. Typically, a function like Get-MSInformation would return a PSObject:

       $props = @{
            PackageCode = $packageCode;
            ProductCode = $productCode[1];
            ProductVersion = $version[1];
            ProductName = $productName[1];
            UpgradeCode = $upgradeCode[1];
        }
    
        New-Object -TypeName PSObject -Property $props

    Once you update the function, you would get:

    UpgradeCode    : {50CA4F23-337B-450C-8576-7984A0FDE029}
    ProductVersion : 7.0.0
    ProductName    : VMware Remote Console
    ProductCode    : 0522e1ff-6290-467f-af84-d9b4eb3eeb09
    PackageCode    : 66a96973-bb1b-4dc5-9ecd-4bc2521149bc

    versus:

    Name                           Value                                                                                                                         
    ----                           -----                                                                                                                         
    UpgradeCode                    {50CA4F23-337B-450C-8576-7984A0FDE029}                                                                                        
    ProductVersion                 7.0.0                                                                                                                         
    ProductName                    VMware Remote Console                                                                                                         
    ProductCode                    0522e1ff-6290-467f-af84-d9b4eb3eeb09                                                                                          
    PackageCode                    66a96973-bb1b-4dc5-9ecd-4bc2521149bc   

    To export to a CSV, it would be as simple as:

    Get-MsiInformation "C:\users\rsimmers\Downloads\VMware-VMRC-7.0.0-2217084.msi" | Export-CSV C:\Users\rsimmers\Desktop\test.csv -NoTypeInformation

    If you only wanted a single property, it would be:

    Get-MsiInformation "C:\users\rsimmers\Downloads\VMware-VMRC-7.0.0-2217084.msi" | Select PackageCode | Export-CSV C:\Users\rsimmers\Desktop\test.csv -NoTypeInformation

    Also, you don't need the return moniker, just the variable name will return the data from the function. Hope this helps.

  • #21376
    Profile photo of Chicken Pecker
    Chicken Pecker
    Participant

    Thank you all for the helpful information. I now have a working script that is exporting the needed information to a csv file.

You must be logged in to reply to this topic.