0

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.

4
  • $indicatorFilePath is never assigned to, so Test-Path $indicatorFilePath is probably the source of that error Commented Jul 22, 2024 at 9:51
  • Correct, I used an outdated definition of the script. I now changed it, please have a look. It is still not installing. Commented Jul 22, 2024 at 10:15
  • Okay, so now you know what happens - "C:\Program Files (x86)\LG Electronics\OnScreen Control\bin" doesn't exist Commented Jul 22, 2024 at 10:23
  • No, afaik this should be created once the installation runs successfully. This is just the check if the installation was successful. I tested this by installing it manually and it always creates that folder. Commented Jul 22, 2024 at 11:19

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the recommendation, I added this to the script but sadly "Installation failed" is the final output now. Could it be that this .exe does not support silent installation?
Quite possibly. Inspect the exit code and then go consult the installer documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.