Install Cab file on online system

Discussion in 'Scripting' started by rpo, Aug 8, 2020.

Tags:
  1. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,447
    1,421
    60
    An (other) way to install a cab file on a running Windows using an hybrid cmd/powershell script :
    Code:
    <# : standard way of doing hybrid batch + powershell scripts
    @PowerShell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden "iex((Get-Content(\""%~f0\"") -Raw))"&exit /b
    #>                                                                                       
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    #
    # Filebrowser dialog object
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
        Multiselect = $false # One file can be chosen
        Filter = 'Cab files (*.cab)|*.cab' # Select only cab files
    }
    #
    $Form=New-Object System.Windows.Forms.Form # Create the screen form (window)
    $Form.TopMost = $True
    $Form.Text="Install cab file"
    $Form.Width=420 ; $Form.Height=190
    $Form.StartPosition = "CenterScreen"
    $Form.ControlBox=$False ; $Form.SizeGripStyle = "Hide"
    [System.Windows.Forms.Application]::EnableVisualStyles();
    
    $CabFileName=New-Object System.Windows.Forms.Label
    $CabFileName.Location=New-Object System.Drawing.Point(20,30)
    $CabFileName.Font='Default Font,9'
    $CabFileName.Size=New-Object System.Drawing.Size(350,20)
    $CabFileName.text="Selected cab file :"
    $Form.Controls.Add($CabFileName)
    
    $CabFile=New-Object System.Windows.Forms.Label # Put the Cab file name on the form
    $CabFile.Location=New-Object System.Drawing.Point(20,50)
    $CabFile.Text=" "
    $CabFile.Font='Consolas,10'
    $CabFile.Size=New-Object System.Drawing.Size(365,23)
    $CabFile.Backcolor = [System.Drawing.Color]::White
    $CabFile.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
    $Form.Controls.Add($CabFile)
    
    $SelectCabButton=New-Object System.Windows.Forms.Button # Put the Select Cab button on the form
    $SelectCabButton.Location=New-Object System.Drawing.Point(155,110)
    $SelectCabButton.Text="Select Cab"
    $SelectCabButton.Font='Default Font,9'
    $SelectCabButton.Size=New-Object System.Drawing.Size(100,26)
    $Form.Controls.Add($SelectCabButton)
    $SelectCabButton.Add_Click({
        If ($FileBrowser.ShowDialog() -ne "Cancel"){ # if Cancel, just ignore
            $Global:ImagePath = $FileBrowser.filename    # return the file name
            $CabFile.Text= Split-Path -Path $ImagePath -leaf # extract filename and extension (Cab)
            if (($CabFile.Text).length -gt 44) {$CabFile.Text = $ImagePath.PadRight(100," ").substring(0,43)+"..."}
            $OKButton.Enabled = $true
            $OKButton.Focus()}})
    
    $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    $CancelButton.Location=New-Object System.Drawing.Point(285,110)
    $CancelButton.Text="Cancel"
    $CancelButton.Font='Default Font,9'
    $CancelButton.Size=New-Object System.Drawing.Size(100,26)
    $Form.Controls.Add($CancelButton)
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    
    $OKButton=New-Object System.Windows.Forms.Button # Put the Create Disk button on the form
    $OKButton.Location=New-Object System.Drawing.Point(20,110)
    $OKButton.Text="Install"
    $OKButton.Font='Default Font,9'
    $OKButton.Size=New-Object System.Drawing.Size(100,26)
    $Form.Controls.Add($OKButton)
    $OKButton.Enabled = $false
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    
    if($form.ShowDialog() -eq "Cancel" -Or $ImagePath -Eq "") {exit}
    #
    If (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
    {
    start cmd -Args "/c dism /online /add-package /packagepath:`"$ImagePath`"&pause"
    }else{
    start cmd -Args "/c dism /online /add-package /packagepath:`"$ImagePath`"&pause" -Verb RunAS
    }