Installing MSI application via PowerShell - powershell

I am trying to build a powershell script to install, an application on 15 workstations. The application requires the following:
Application process to be stopped
Previous version to be uninstalled
Install new client.
I am running into a few snags though. The uninstaller is telling me that the application isn't installed, and or the application is not a valid 64-bit application. I can run the uninstaller fine if I use the uninstall string from command line. My uninstall log file, does not tell me much, it just tells me that Powershell.
If I run through the installer manually, I reach a point after accepting the EULA, and begin the actual installation by selecting "install" that button comes up with the "Admin" shield. Automating it I get an MSI error 1603 which I cannot get past...
This is a medical application to be installed in a very restricted, environment. Thank you for your help!
Here is my code:
$SourcePath = "LOCATION OF Source"
$Destination = "C:\Temp"
$App = Get-WmiObject win32_product | where{$_.name -like "mammoscreen*"}
$Install = 'C:\Temp\tpx-client-v2.2.3.msi'
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path "C:\Temp\TheraInstall.log" -append
#Do some stuff and begin logging
Write-Output "Start Logging"
Get-Date
Write-Host "Copying files..." -ForegroundColor Green
Copy-Item $SourcePath -Destination $Destination
Write-Host "Stopping process..." -ForegroundColor Cyan
Stop-Process -ProcessName tpx-client-agent
#Uninstall old versions of software
Write-Host "Uninstalling previous versions..." -ForegroundColor yellow
Start-Process 'C:\Windows\System32\msiexec.exe' -ArgumentList "/X $($App.IdentifyingNumber)/qn /norestart /L*v C:\Temp\UninstallTest.log" -Wait
#Installs the new Client
Write-Host "Installing new agent v2.2.3..." -ForegroundColor Green
Start-Process 'C:\Windows\System32\msiexec.exe' -Wait -ArgumentList '/I $Install /qn /norestart /L*v C:\Temp\InstallTest.log'
#Stop logging
Stop-Transcript
#Start-Process -Filepath "tpx-client-agent"

Related

Cannot uninstall application using Powershell

I am trying to upgrade an application using an .msp file but every time I run the script I get a message saying the product I'm trying to upgrade from does not exist. My thought was there might be a bug in the original MSI so I decided to uninstall the application and a full install to the new version. When I run the script to Uninstall it works on some machines but not on others. When I try to uninstall using powershell it fails. When I use MSIEXEC /X I get the following message
"This action is only valid for products that are currently installed".
Next I decided to try and repair using MSIEXEC /fa and I get this:
"This installation package cannot be opened. Verify that the package exists and that you can access it, or contat the application vendor to verify tat this is a valid Winds Installer package"
The strange thing is I can uninstall the application with no problems from Add\Remove programs with not issues and install the upgrade. The problem with this is that I have over 3000 users. From SCCM, I tried to supercede the old version to uninstall before installing the new version and that also failed. Any help you guys can provide would be helpful. The application is Taxprep Forms 2021.1.0 upgrading to 2021.2.0. i have added 2 of the scripts I tried.
For this script I tried doing a repair first then uninstall. I also tried using both product code and app name:
Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Bypass
$uninstall32 = gci “HKLM:SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\” | foreach { gp $_.PSPath } | ? { $_ -match “Taxprep Forms 2021” } | select UninstallString
$uninstall64 = gci “HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\” | foreach { gp $_.PSPath } | ? { $_ -match “Taxprep Forms 2021” } | select UninstallString
if ($uninstall64) {
$uninstall64 = $uninstall64.UninstallString -Replace “msiexec.exe”,”” -Replace “/I”,”” -Replace “/X”,””
$uninstall64 = $uninstall64.Trim()
Write "Repairing Old install..."
Start-Process "msiexec.exe" -arg "/fa $uninstall64" -Wait
Write “Uninstalling…”
start-process “msiexec.exe” -arg “/X$uninstall64 /qb” -Wait}
if ($uninstall32) {
$uninstall32 = $uninstall32.UninstallString -Replace “msiexec.exe”,”” -Replace “/I”,”” -Replace “/X”,””
$uninstall32 = $uninstall32.Trim()
Write "Repairing Old install..."
Start-Process "msiexec.exe" -arg "/fa $uninstall32" -Wait
Write “Uninstalling…”
start-process “msiexec.exe” -arg “/X$uninstall32 /qb” -Wait}
I also tried:
Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Bypass -Force
$DisplayName = "Taxprep Forms 2021"
$TaxprepCheck = (Get-ItemProperty HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -eq $DisplayName}) -ne $null
$PublicUser = "C:\Users\Public\CCH\Taxprep Forms 2021\"
$UnInstallCheck = "C:\Users\Public\CCH\Taxprep Forms 2021\Forms2021Uninstall.txt"
$Uninstalled = "C:\Logs\TaxprepForms2021.1.0Uninstalled.txt"
$TaxprepVer = Get-WmiObject -Class Win32_Product | Where-Object{$_.Name -eq $DisplayName}
If(-Not $TaxprepCheck)
{
New-Item $Uninstalled -Force -ErrorAction Stop
Set-Content $Uninstalled "Taxprep Forms 2021.1.0 is not installed"
}
else
{
$TaxprepVer.Uninstall()
New-Item $Uninstalled -Force -ErrorAction Stop
Set-Content $Uninstalled "Taxprep Forms 2021.1.0 is successfully Uninstalled"
}
If (Test-Path -Path $PublicUser)
{
New-Item $UnInstallCheck -Force -ErrorAction Stop
Set-Content $UnInstallCheck "Taxprep Forms 2021.1.0 Uninstalled"
}
Else
{
New-Item -ItemType "directory" -Path $PublicUser
New-Item $UnInstallCheck -Force -ErrorAction Stop
Set-Content $UnInstallCheck "Taxprep Forms 2021.1.0 Uninstalled"
}
I have verified that the application is installed. It's in the registry. These scripts work on some computer and not on others. The machines it doesnt work on I have to uninstall the application through add\remove.
Ok so it seemed I may have caused my own issue. When I initially deployed the application in SCCM, I set the User experience to “Install as User”. The switch I used when calling msiexec was All Users = 2. I switched the install to “Install for System and set All Users = “”. I would need to deploy the msi and do a full install rather than an upgrade using the msp. Installing as User may have messed up the registry setting for the initial install.

PowerShell Start-Process Not Starting when script called remotely

I am trying to remotely install Octopus Deploy Tentacle to a VM. I have a powershell script that I've written that handles this business for me. It works exactly as expected when I am physically logged into the machine, but when I am remotely executing it on the machine it doesn't run the installer. Every other part of the script works fine though.
The portion of the script that downloads and installs:
try{
$url = "https://octopus.com/downloads/latest/OctopusTentacle64"
$downloadPath = "$env:TEMP\octopus\"
if (Test-Path $downloadPath) {
Remove-Item -Path $downloadPath -Recurse -Force
}
New-Item -Type Directory -Path $downloadPath | Out-Null
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
(New-Object System.Net.WebClient).DownloadFile($url, "$downloadPath\Octopus.Tentacle.msi")
Start-Process -FilePath "$downloadPath\Octopus.Tentacle.msi" -ArgumentList "/passive" -Wait -verbose
Write-Output "Octopus Tentacle Installed."
} catch { throw "error downloading tentacle. try again later."}
I am using this to attempt to run the file remotely(edit, copied wrong line):
Invoke-Command -ComputerName $vmName -FilePath "\\$vmName\Installers\Install-Calamari.ps1" -Credential $creds -ArgumentList $deployTag, $envID
What am I missing?

Elevating PowerShell script permissions

I am trying to run script to manage some VHD Disks, but the disk mount is failing due to elevated permissions required. The user the script is run under is a local admin, but UAC is blocking it I think. The error which comes back is: “DiskState=Failed to mount disk - "Access to a CIM resource was not available to the client”
Ideally I need to the script to run under elevated command prompt automatically. Any idea's how I can achieve that programmatically?
The script I am running is this:
$location = "C:\temp"
$name = "downloadfile"
$Author = "FSLogix"
$FilePath = "Filepath here"
$LogFilePath = "Logfilepath here"
# Force to create a zip file
$ZipFile = "$location\$Name.zip"
New-Item $ZipFile -ItemType File -Force
$RepositoryZipUrl = "https://github.com/FSLogix/Invoke-FslShrinkDisk/archive/master.zip"
# download the zip
Write-Host 'Starting downloading the GitHub Repository'
Invoke-RestMethod -Uri $RepositoryZipUrl -OutFile $ZipFile
Write-Host 'Download finished'
#Extract Zip File
Write-Host 'Starting unzipping the GitHub Repository locally'
Expand-Archive -Path $ZipFile -DestinationPath $location -Force
Write-Host 'Unzip finished'
# remove the zip file
Remove-Item -Path $ZipFile -Force
# Run the FSLogix Optimisation
C:\temp\Invoke-FslShrinkDisk-master\Invoke-FslShrinkDisk.ps1 -Path $FilePath -Recurse -PassThru -LogFilePath $LogFilePath\logfile.csv
You can elevate the PS script using the Powershell as a separate process and make it "run as admin" like below:
start-process PowerShell -verb runas
OR
Powershell -Command "Start-Process PowerShell -Verb RunAs"
Apart from that , you can condition it as well. There is a beautiful conditional code shared by PGK which can help as well:
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" +$myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}

Cannot move file after long running task

I have a windows powershell script executed by Windows Task Scheduler. The powershell code starts an executable cli program (java), which creates it's own logfile, then when finished my powershell script tries to rename the file then upload it to a remote server. However, I'm finding that it only successfully uploads the file ever 3rd or 4th execution and am not sure why. The history in task scheduler provides no details as to what might have happened (file lock?). Any ideas on how I can solve this? Here is a basic example of what I'm doing:
$old_log_name = "old_logfilename"
$new_log_name = "new_logfilename"
C:\path\to\my-java-program.exe -pass -some -options
Move-Item -Path $old_log_name -Destination $new_log_name
gsutil cp $new_log_name gs://cool-bucket-with-cf
I'm certain that the problem is with renaming the file, and not with uploading it, because can see so in windows file explorer.
Should I be checking to see if the file is available to be renamed or not?
Edit (revised code based on comments below):
$app_version = "4.5.7"
$now = get-date
$now = $now.ToString("yyyyMMddhhmm")
$old_log_name = "D:\myapp_$app_version.log"
$new_log_name = "D:\myapp_servername_$app_version.$now.log"
$arguments = "-a -c C:\myapp\config\Stage-$app_version.xml"
Start-Process C:\myapp\bin\4.5.7\myapp.exe -ArgumentList $arguments -NoNewWindow -Wait
Move-Item -Path $old_log_name -Destination $new_log_name
gsutil cp $new_log_name gs://path-to-mybucket
Why don't you start a process with your java program, so it wait for it to exit ?
$old_log_name = "old_logfilename"
$new_log_name = "new_logfilename"
Start-Process C:\path\to\my-java-program.exe -ArgumentList "your options" -NoNewWindow -Wait
Move-Item -Path $old_log_name -Destination $new_log_name
gsutil cp $new_log_name gs://cool-bucket-with-cf
Update
Since this is a scheduled task, you need to add a new event log in order to log events from your script (you need administrative rights for this) :
New-EventLog -LogName Application -Source TheScriptThatCallsMyJavaProgram
Then, replace Start-Process call with the following (EventId is random here) :
Write-EventLog -LogName Application -Source TheScriptThatCallsMyJavaProgram -EventId 3001 -Message "my-java-program.exe is about to start" # This logs the start of the program
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "C:\path\to\my-java-program.exe"
$pinfo.Arguments = "your options"
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
Write-EventLog -LogName Application -Source TheScriptThatCallsMyJavaProgram -EventId 3002 -Message "my-java-program.exe is exited" # This logs the end of the program

Powershell Popup Box not showing when installing through SCCM

Not sure how to fix it but I think i have a hunch on why
I have a powershell script installs an applications but before it kicks off it shows a msgbox that simply displays a message to the user
The script works perfectly when I run it manually and even running it as the System account through psexec works as well
However when deploying this through SCCM - Software center, it installs without displaying the msgbox..
Now I think it might be because its not showing in the context of the current logged in user.. but I would of thought running it through Psexec as system would not work either...
Can anyone help? I have deployed it as an application through sccm using this script:
<#
.Date: 01-Jun-2016
.Ansys 16.2 Install Script
# Set up some Variables
$workingDirectory = (split-path $myinvocation.mycommand.path -parent)
# Display a warning message before installation begins
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.Interaction]::MsgBox('Ansys 16.2 takes over 30 mins to install. Please do not log out or shutdown your computer during the installation. You can continue working as normal while it is being installed. Once complete you will see in Software Center say "installed" next to Ansys 16.2.', 'OKOnly,SystemModal,Exclamation', 'Warning')
# ***** Install Application ******
Start-Process -FilePath "$WorkingDirectory\ANSYS162_WINX64_Disk1\setup.exe" -ArgumentList "-silent -disablerss -licserverinfo `"::licensing-b`"" -Wait -ErrorAction SilentlyContinue
Start-Sleep -s 3
# ***** Delete Shortcut and unlicensed products *******
Remove-Item "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\ANSYS 16.2\Uninstall ANSYS 16.2.lnk" -Force -ErrorAction SilentlyContinue
Remove-Item "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\ANSYS 16.2\ANSYS Icepak 16.2.lnk" -Force -ErrorAction SilentlyContinue
Remove-Item "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\ANSYS 16.2\Aqwa" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\ANSYS 16.2\ACP" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\ANSYS 16.2\ANSYS Client Licensing" -Recurse -Force -ErrorAction SilentlyContinue "#>
Make sure that you have checked "allow the user to interact with this program" option while deployment
click here to see how to set user interaction