Search code examples
powershellintune

Problems with PowerShell script App installation


I have the following script which should install the OnScreen Control App.

$VerbosePreference = "Continue"

# Function to check if the script is running with elevated permissions
function Test-Admin {
    $currentUser = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
    return $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}

# Check if the script is running with elevated permissions
if (-not (Test-Admin)) {
    Write-Verbose "Script is not running with elevated permissions. Restarting with elevated permissions..."
    Start-Process powershell -ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
    exit
}

Write-Verbose "Script is running with elevated permissions."

# Define the path to a file that indicates successful installation
$indicatorFilePath = "C:\Program Files (x86)\LG Electronics\OnScreen Control\bin"

# Get the directory where the script is located
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
Write-Verbose "Script directory: $scriptDir"

# Define the relative path to the installer from the script directory
$installerPath = Join-Path -Path $scriptDir -ChildPath "OSC_9.23.exe"
Write-Verbose "Installer Path: $installerPath"

# Define the installation arguments
$installArgs = @('/S', '/v', '/qn')
Write-Verbose "Installation arguments: $installArgs"

# Check if the installer file exists
if (Test-Path $installerPath) {
    Write-Verbose "Installer file found. Starting installation..."
    try {
        Start-Process -FilePath $installerPath -ArgumentList $installArgs -NoNewWindow -Wait -PassThru

        # Check if the indicator file exists
        if (Test-Path $indicatorFilePath) {
            Write-Verbose "Installation successful. Indicator file found."
            exit 0
        } else {
            Write-Verbose "Installation failed. Indicator file not found."
            exit 1
        }
    } catch {
        Write-Verbose "Installation failed: $_"
        exit 1
    }
} else {
    Write-Verbose "Error: Installer file not found at path: $installerPath"
    exit 1
}

Now when I try to run it with powershell.exe -ExecutionPolicy Bypass -File "C:\File\To\Path\Install.ps1" it tells me VERBOSE: Installation failed. Indicator file not found..

But the Verbose output just before is telling me the correct path of the variable. What am I missing here?

I am trying to publish this app to intune to deploy it via Companyportal, but I need to get this script to work first. Happy for any ideas what to do here, been at it for too long already, which is why I added all of the verbose output.

EDIT: Updated script and output.


Solution

  • Assign the output from Start-Process ... -PassThru to a variable so you can wait for the process to finish:

    $installerProcess = Start-Process -FilePath $installerPath -ArgumentList $installArgs -NoNewWindow -Wait -PassThru
    
    $installerProcess.WaitForExit()
    
    # installer process has exited by now
    if ($installerProcess.ExitCode -ne 0) {
        Write-Verbose "Installation failed."
        exit $installerProcess.ExitCode
    }