Failed to install [Name] MsiExec returned: 255, when running an cmd file from Powershell - powershell

I'm trying to run an MSI install command from Powershell by using a cmd file.
I create my install command:
msiexec /i [insert here a HUGE amount of param values] /lv install.log /passive
I then write it to temp.cmd and then execute from Powershell as follows:
$exitCode = (Start-Process -FilePath "temp.cmd" -Wait -Passthru).ExitCode
I then get the following error message:
CategoryInfo : OperationStopped: 255:StringRuntimeException
FullyQualifiedErrorId
Failed to install [Msi Name] MsiExec returned: 255
What causes this?

If you try and run the MsiExec command I was having trouble with from the commandline, I get the following error message:
The input line is too long
This error isn't obvious when executed in Powershell and you might think its an issue with MsiExec.
This is a bit of a cryptic error message and relates to cmd.exe having a total commandline length of 8191.
See the Microsoft KB article on cmd length: http://support.microsoft.com/kb/830473

Related

How to prevent a cmd execution from printing text back to the screen in PowerShell?

In PowerShell I'm doing a cmd call to execute a program.
$create = "CreateFile /username Test /password password123"
cmd.exe /c $cred
When this program is executed via command line it prints to the screen "Command ended successfully." and this text seems to be returned from the cmd to my PowerShell script and tries to execute the line. So I get the error:
cmd.exe : Command ended successfully
At E:\Install\Files Script.ps1:12 char:17
+ $returnString = cmd.exe /c $cred
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Command ended successfully:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
My understanding is that the line
cmd.exe : Command ended successfully
Is trying to be executed by my PowerShell script and therefore it's throwing an error. Is there a way for me to prevent my command line call to the program to prevent from printing to the window? I've tried calling
echo off
prior to executing and I get the same result. Any help would be appreciated.
After some research, I was able to find a solution that seems to work in my case. I've added:
/f >nul 2>&1
To my script, and it seems to have solved my problem. My new script:
$create = "CreateFile /username Test /password password123"
cmd.exe /c $cred

Use the start-process command to get the return value of the running program

Using the .bat file code below, I can get the value returned after running the program.
"C:\Program Files\www\www.exe" /start "/My app/abc" && echo %ERRORLEVEL% > c:\ResultCode.txt"
How to use the powershell code to achieve the same functionality
I use the code below and I didn't get the result.
Start-Process -FilePath "C:\Program Files\www\www.exe" -ArgumentList "/start `"/My Processes/abc`"" -RedirectStandardError "c:\ResultCode.txt"

Start-Process cmdlet with -ArgumentList throw "Invalid Argument" error

I want to register a dll using gacutil.exe.
Start-Process -Wait -FilePath "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\gacutil.exe" -ArgumentList '/u', "USB\CRM\Common"
It throw the error:
You don't need Start-Process if you just want to run a command at the PowerShell command line. PowerShell can run commands typed at its prompt. (It's a shell; one of the purposes of a shell is to run commands you enter.) Since the command contains spaces, enclose it in " and execute it with the & (call or invocation) operator.
& "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\gacutil.exe" /u USB\CRM\Common

Installation of UWP app fails with error 0x80070005: Adding a tile failed with unexpected error

I am trying a install a UWP app using the powershell command
Start-Process powershell " -ExecutionPolicy Unrestricted -NoExit -Command "cd \"C:\Users\Administrator\Documents\Visual Studio 2015\Projects\App3\AppPackages\App3\App3_1.0.0.0_x64_Test`"; & .\Add-AppDevPackage.ps1 -Force`" |Out-File C:\Users\Administrator\Desktop\log.txt" -Verb RunAs -Wait
where AppDevPackage.ps1 is the powershell script provided in the app.
The script runs fine when I run it by using right-click -> run with powershell.But when I try to invoke the script using a jenkins or using a scheduled task the below error is thrown:
Add-AppxPackage : Deployment failed with HRESULT: 0x80073CF6, Package could not be registered.
error 0x80070005: Adding a tile failed with unexpected error.
NOTE: For additional information, look for [ActivityId] 5e8c5432-8277-0004-c28f-8c5e7782d201 in
the Event Log or use the command line Get-AppxLog -ActivityID 5e8c5432-8277-0004-c28f-8c5e7782d201
At C:\Users\Administrator\Documents\Visual Studio
2015\Projects\App3\AppPackages\App3\App3_1.0.0.0_x64_Test\Add-AppDevPackage.ps1:388 char:13
+ Add-AppxPackage -Path $DeveloperPackagePath.FullName -Dep ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\Users\Admini....0.0.0_x64.appx:String) [Add-AppxPac
kage], IOException
+ FullyQualifiedErrorId : DeploymentError,Microsoft.Windows.Appx.PackageManager.Commands.AddA
ppxPackageCommand
I looked the event log as well,even that gives the same error code.I need to run this script for automation,hence can't run this manually.
I tried all sorts of things but configuring a scheduled task for the powershell script and invoking it from jenkins using the command :
schtasks /run /tn
is the only thing that worked for me.
Also make sure to run the scheduled task with the highest privileges using an user account which has administrative rights.

Powershell execute commandline locally

Powershell script executes commandline on local machine
$j = "remote_machine"
$comp = "\\"+$j
$command = 'D:\PSTools\PsExec.exe $comp -u Administrator -p plaintextpassword -accepteula powershell.exe c:\share\script.ps1'
Invoke-Expression "& $command"
This works, but it outputs following
PsExec.exe : At line:1 char:1
+ & D:\PSTools\PsExec.exe $comp -u Administrator -p plaintextpassword -accepteula powersh ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError PsExec v2.0 - Execute processes remotely Copyright (C) 2001-2013 Mark Russinovich
Sysinternals - www.sysinternals.com Connecting to
remote_machine...Starting PSEXESVC service on
remote_machine...Connecting with PsExec service on
remote_machine...Starting powershell.exe on remote_machine...
powershell.exe exited on remote_machine with error code 0.
How to fix?
After re-reading your question I see that this is the standard PSExec vs PowerShell issue that has been seen and discussed before. This is due to the fact that PSExec outputs its header lines to the stderr (Standard Error) stream for some of its text. The execution works fine, and it does show an exit code of 0 indicating that there is not actually an error.
This issue is only evident in PowerShell ISE, not the standard PowerShell console (unless you redirect StdErr to StdOut with PSExec <command & args> 2>&1 or something similar). To work around this, if you are going to run the script in the ISE, you can use Start-Process's -RedirectStandardError argument, or redirect StdErr through other means.