This is an old revision of the document!
Tested on: Win7 64-bit
Requieres: Powershell 2.0, MSI Module for Powershell
Download MSI Module for Powershell: http://psmsi.codeplex.com/
Dieses Powershellscript extrahiert informationen aus MSI Paketen und erstellt automatisiert anhand eines Templates fertige Opsiskripte inkl. control-Datei und der passenden Verzeichnisstruktur um die fertigen Daten auf den Opsiserver kopieren zu können.
[msi_to_opsi.ps1]
# Version: 0.1 # Date: 30.7.2013 # Author: Chris # Description: Retrieve informations from multiple MSI files and create OPSI package scripts from that. # How to use: MSI Files in poolfoldername will be processed by msi powershell module. Place MSI files in folder pool, run script, retrieve ready to use Opsi scripts from folder OUTPUT $testmodulemsi = Get-Module -ListAvailable | Where-Object { $_.name -eq "MSI" } if (($testmodulemsi)) { Import-Module -Name MSI -ErrorAction SilentlyContinue Write-Host "Module MSI loaded." } else { Write-Host `n"FATAL ERROR: Module MSI not available on this system. Please install Module MSI for PowerShell first. You can get it from here http://psmsi.codeplex.com/" `n Write-Host "THIS IS A FATAL ERROR. EXITING NOW."`n`n exit } $DateTime = (Get-Date -Format s) -Replace "T", "_" -Replace ":", "." ## Specify some global settings $BaseFolder = "C:\msi_to_opsi" # Poolfoldername is the location with the basefolder where the installers and theire procducte images are located. $PoolFolderName = "Pool" $InputFolder = $BaseFolder + "\" + $PoolFolderName + "\" # OPSI Package Parameter $DefaultValueMinimumspace = "1000 MB" $priority = "0" $packageversion = "1_autoconvert" # Set Log Folder $LogFolder = "$BaseFolder\Logs" If(!(Test-Path "$BaseFolder\Logs")){New-Item -ItemType Directory -Path "$BaseFolder\Logs"} $log = $LogFolder + "\create_opsi_packages_" + $DateTime +".log" # MSI Parameter $installparameter = "/gb-! REBOOT=REALLYSUPPRES" $uninstallparameter = "/gb-! REBOOT=REALLYSUPPRES" # Move finishe packages to Opsi server. Not implementet (yet) $CopyToOpsiServer = $false $OpsiServerName = "opsi2" $OpsiUserName = "pcpatch" $OpsiPasswordName = "1234" $CreatedLinuxShellScript = $false foreach ($msifile in Get-ChildItem .\Pool -Filter *.msi -name) { $fullmsifile = $InputFolder + $msifile $productName = get-msitable $fullmsifile -table Property | where-object { $_.Property -eq "ProductName" } | select-object -expand Value $productNameFlat = ($productName) -Replace " ", "_" #-Replace "'('", "_" -Replace "')'", "_" -Replace "[", "_" -Replace "]", "_" $description = "Automatically translated MSI-Package: " + $productName $progamversion = get-msitable $fullmsifile -table Property | where-object { $_.Property -eq "ProductVersion" } | select-object -expand Value $msiid = get-msitable $fullmsifile -table Property | where-object { $_.Property -eq "ProductCode" } | select-object -expand Value $uninstallprogramm = $msifile $uninstallprogramm = $msifile $installexec = $msifile write-host "Paket: " $productName $packagename = $productName.ToLower() $OutputPackageFolder = $BaseFolder + "\OUTPUT\$productNameFlat" $InstallerExecutable = $fullmsifile $OutputPackageFolderClientData = $OutputPackageFolder + '\CLIENT_DATA\' # Create packagefolder structur "Building Opsipackage: " + $productName + "`n" + "`n" | Out-File $log -append If(!(Test-Path "$OutputPackageFolder")) { New-Item -ItemType Directory -Path "$OutputPackageFolder" "Created folder " + $OutputPackageFolder | Out-File $log -append } Else { "SKIPPED: Folder " + $OutputPackageFolder + " already exists." | Out-File $log -append } If(!(Test-Path "$OutputPackageFolder\OPSI")) { New-Item -ItemType Directory -Path "$OutputPackageFolder\OPSI" "Created folder " + $OutputPackageFolder + "\OPSI" | Out-File $log -append } Else { "SKIPPED: Folder " + $OutputPackageFolder + "\OPSI already exists."| Out-File $log -append } If(!(Test-Path "$OutputPackageFolder\CLIENT_DATA")) { New-Item -ItemType Directory -Path "$OutputPackageFolder\CLIENT_DATA" "Created folder " + $OutputPackageFolder + "\CLIENT_DATA" | Out-File $log -append } Else { "SKIPPED: Folder " + $OutputPackageFolder + "\CLIENT_DATA already exists."| Out-File $log -append } $minimumspace = $DefaultValueMinimumspace # Create setup.ins $original_file_setup_ins = $BaseFolder + '\Templates\CLIENT_DATA\setup.ins' $destination_file_setup_ins = $OutputPackageFolder + '\CLIENT_DATA\setup.ins' (Get-Content $original_file_setup_ins) | Foreach-Object { $_ -replace 'PSVAR_PACKAGENAME', $packagename ` -replace 'PSVAR_MINIMUMSPACE', $minimumspace ` -replace 'PSVAR_INSTALLDIR', $installdir ` -replace 'PSVAR_INSTALLEXEC', $installexec ` -replace 'PSVAR_INSTALLPARAMETER', $installparameter ` -replace 'PSVAR_UNINSTALLPARAMETER', $uninstallparameter ` -replace 'PSVAR_UNINSTALLPROGRAMM', $uninstallprogramm ` -replace 'PSVAR_INSTALLSTRING', $installstring ` -replace 'PSVAR_EXITCODECHECKSTRING', $ExitCodeCheckstring ` -replace 'PSVAR_MSIID', $msiid ` -replace 'PSVAR_PRODUCTNAME', $productName } | Set-Content $destination_file_setup_ins # Create uninstall.ins $original_file_uninstall_ins = $BaseFolder + '\Templates\CLIENT_DATA\uninstall.ins' $destination_file_uninstall_ins = $OutputPackageFolder + '\CLIENT_DATA\uninstall.ins' (Get-Content $original_file_uninstall_ins) | Foreach-Object { $_ -replace 'PSVAR_PACKAGENAME', $packagename ` -replace 'PSVAR_MINIMUMSPACE', $minimumspace ` -replace 'PSVAR_INSTALLDIR', $installdir ` -replace 'PSVAR_INSTALLEXEC', $installexec ` -replace 'PSVAR_INSTALLPARAMETER', $installparameter ` -replace 'PSVAR_UNINSTALLPARAMETER', $uninstallparameter ` -replace 'PSVAR_UNINSTALLPROGRAMM', $uninstallprogramm ` -replace 'PSVAR_INSTALLSTRING', $installstring ` -replace 'PSVAR_MSIID', $msiid ` -replace 'PSVAR_PRODUCTNAME', $productName } | Set-Content $destination_file_uninstall_ins # Create delsub.ins $original_file_delsub_ins = $BaseFolder + '\Templates\CLIENT_DATA\delsub.ins' $destination_file_delsub_ins = $OutputPackageFolder + '\CLIENT_DATA\delsub.ins' (Get-Content $original_file_delsub_ins) | Foreach-Object { $_ -replace 'PSVAR_PACKAGENAME', $packagename ` -replace 'PSVAR_MINIMUMSPACE', $minimumspace ` -replace 'PSVAR_INSTALLDIR', $installdir ` -replace 'PSVAR_INSTALLEXEC', $installexec ` -replace 'PSVAR_INSTALLPARAMETER', $installparameter ` -replace 'PSVAR_UNINSTALLPARAMETER', $uninstallparameter ` -replace 'PSVAR_UNINSTALLPROGRAMM', $uninstallprogramm ` -replace 'PSVAR_INSTALLSTRING', $installstring ` -replace 'PSVAR_EXITCODECHECKSTRING', $ExitCodeCheckstring ` -replace 'PSVAR_MSIID', $msiid } | Set-Content $destination_file_delsub_ins # Create OPSI Controlfile $original_file_opsi_control = $BaseFolder + "\Templates\OPSI\control" $destination_file_opsi_control = $OutputPackageFolder + "\OPSI\control" (Get-Content $original_file_opsi_control) | Foreach-Object { $_ -replace "PSVAR_PACKAGENAME", $packagename ` -replace 'PSVAR_PACKAGEVERSION', $packageversion ` -replace 'PSVAR_DESCRIPTION', $description ` -replace 'PSVAR_NAME', $productname ` -replace 'PSVAR_PROGRAMMVERSION', $progamversion ` -replace 'PSVAR_PRIORITY', $priority } | Set-Content $destination_file_opsi_control # Copy executable files und producticons If(!(Test-Path $InstallerExecutable)) { Write-Host "Error: Installerfile ($InstallerExecutable) not found, package will not be complete" "Error: Installerfile ($InstallerExecutable) not found, package will not be complete" | Out-File $log -append } Else { Copy-Item -Path $InstallerExecutable -Destination $OutputPackageFolderClientData } }