Hi,
I am writing a PowerShell script that will automate the installation of MSI files. I also want this script to be able to open up a browse files dialog box so that the user can specify which MSI files to install (the basic version of this will allow for manual editing of the script for pre-determined installs).
This is the script I have at the moment. It works when I press play within the PowerShell ISE, but from the desktop/command line it just freezes. I've included the errors from ISE as well.
Also, it would be ideal to make it so that if the MSI does not install it does not prompt with 'MSI installed successfully' and rather says 'MSI did not install' or something to that effect.
(please note I do not receive any error when I run the script in ISE and select a file, only when I do not select a file and cancel I get this error)
Script:
function Select-FileDialog
{
param([string]$Title,[string]$Directory,[string]$Filter="All Files (*.*)|*.*")
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
$objForm = New-Object System.Windows.Forms.OpenFileDialog
$objForm.InitialDirectory = $Directory
$objForm.Filter = $Filter
$objForm.Title = $Title
$Show = $objForm.ShowDialog()
If ($Show -eq "OK")
{
Return $objForm.FileName
}
Else
{
Write-Error "Operation cancelled by user."
}
}
$file = Select-FileDialog -Title "Select a file" -Directory "c:\msi" -Filter "MSI|*.msi"
$product= [WMICLASS]"\\localhost\ROOT\CIMV2:win32_Product"
$product.Install("$file")
$a = new-object -comobject wscript.shell
$b = $a.popup("OK! MSI Install has successfully installed",0,"Sporting Index",1)