header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Problem with PS script
Last Post 08 Sep 2010 09:37 AM by Marco Shaw (MVP). 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
David SymonsUser is Offline
New Member
New Member
Posts:6
Avatar

--
08 Sep 2010 04:21 AM

    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)

    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1641
    Avatar

    --
    08 Sep 2010 04:41 AM
    I'm sorry, I'm reading quick... So if your only problem when you run this from the command-line?

    This would have to do with the default threading used by powershell.exe. Look at the properties of your PowerShell shortcut icon, and add "-sta" to the command-line used to launch powershell.exe then retry.
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    David SymonsUser is Offline
    New Member
    New Member
    Posts:6
    Avatar

    --
    08 Sep 2010 04:50 AM
    Hi,

    Yes when I run it from the command line nothing happens. Adding -sta does work, however, is there anyway to avoid this or automate it in the script? The idea will be to roll this PS script out to many servers, and it would cause a delay if each server would have to have the properties of each icon changed! :)

    Any ideas as to the error messaging as well so it knows whether or not, and prompt accordingly, if it has been installed?
    David SymonsUser is Offline
    New Member
    New Member
    Posts:6
    Avatar

    --
    08 Sep 2010 07:33 AM
    Also, is it possible to install EVERY MSI in a folder, as opposed to just one specified or selected?

    Many thanks :)
    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1641
    Avatar

    --
    08 Sep 2010 09:37 AM
    You will not be able to avoid using STA with your current approach. You may need to have your script simply start another powershell.exe process with the -sta set that runs your script.

    I don't know about your second question... You could parse the MSI chosen, and simply use it's base dir for a simple search so that everything is loaded?
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer