How to detect if /norestart is provided running msi - windows

I'm building MSi using visual studio setup project. I want to have different custom action run depending on /norstart option. What condition should I use to detect if /norestart is provided or not?
If this is impossible, I'm thinking about setting a property. Here is my theory. Use Orca to set REBOOT=Force. If I want to suppress reboot, run msi as
foo.msi /quiet REBOOT=ReallySuppress
And read the property from code, like
String inputFile = #"C:\Users\Administrator\Desktop\foo.msi";
// Get the type of the Windows Installer object
Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
// Create the Windows Installer object
WindowsInstaller.Installer installer = (WindowsInstaller.Installer) Activator.CreateInstance(installerType);
// Open the MSI database in the input file
var database = installer.OpenDatabase(inputFile, 0);
// Open a view on the Property table for the version property
var view = database.OpenView("SELECT * FROM `Property`");
//View view = database.OpenView("SELECT * FROM Property");
// Execute the view query
view.Execute(null);
// Get the record from the view
Record record = view.Fetch();
// Get the version from the data
//string version = record.get_StringData(2);
while (record != null)
{
logger.LogMessage(record.get_StringData(0) + '=' + record.get_StringData(1) + '=' + record.get_StringData(2) + '=' + record.get_StringData(3));
record = view.Fetch();
}
This doesn't work, because it always read REBOOT=Force.

I have figured this out by myself.
Set CustomActionData to be /reboot=[REBOOT].
Then read the value:
Context.Parameters["reboot"]
If /norestart option is provided, value of reboot will be ReallySuppress

Related

Open text file after MSI install

I have been following a solution to add a checkBox at the end of a MSI installation that opens the installed product :
Run exe after msi installation?
So far, so good.
However, I'd like to add another checkBox that opens a simple text file which contains the release notes. The file is already included in the setup project, along with the main output. I'm able to add a new checkBox. The only problem is how to open that text file : no custom action seems to fit this need as I can see here :
http://msdn.microsoft.com/en-us/library/windows/desktop/aa372048%28v=vs.85%29.aspx
Here's my current JS code :
var sql
var view
var checkboxTextForReleaseNotes = "See release notes";
var fileReleaseNotes = "ReleaseNotes.txt";
try
{
var fileIdForReleaseNotes = FindFileIdentifier(database, fileReleaseNotes);
if (!fileIdForReleaseNotes)
throw "Unable to find '" + fileReleaseNotes + "' in File table";
[ ... some actions to include another control as seen in link above ... ]
// Insert the new CheckboxReleaseNotes control
sql = "INSERT INTO `Control` (`Dialog_`, `Control`, `Type`, `X`, `Y`, `Width`, `Height`, `Attributes`, `Property`, `Text`, `Control_Next`, `Help`) VALUES ('FinishedForm', 'CheckboxReleaseNotes', 'CheckBox', '18', '140', '343', '12', '3', 'LAUNCH_RN', '{\\VSI_MS_Sans_Serif13.0_0_0}" + checkboxTextForReleaseNotes + "', 'CloseButton', '|')";
view = database.OpenView(sql);
view.Execute();
view.Close();
// Modify the Order of the EndDialog event of the FinishedForm to 1
sql = "SELECT `Dialog_`, `Control_`, `Event`, `Argument`, `Condition`, `Ordering` FROM `ControlEvent` WHERE `Dialog_`='FinishedForm' AND `Event`='EndDialog'";
view = database.OpenView(sql);
view.Execute();
record = view.Fetch();
record.IntegerData(6) = 1;
view.Modify(msiViewModifyReplace, record);
view.Close();
// Insert the Event to launch the release notes
sql = "INSERT INTO `ControlEvent` (`Dialog_`, `Control_`, `Event`, `Argument`, `Condition`, `Ordering`) VALUES ('FinishedForm', 'CloseButton', 'DoAction', 'OPEN_RN', 'LAUNCH_RN=1', '0')";
view = database.OpenView(sql);
view.Execute();
view.Close();
// Insert the custom action to open the release notes when finished
sql = "INSERT INTO `CustomAction` (`Action`, `Type`, `Source`, `Target`) VALUES ('OPEN_RN', '210', '" + fileIdForReleaseNotes + "', '')";
view = database.OpenView(sql);
view.Execute();
view.Close();
database.Commit();
}
catch (e)
{
WScript.StdErr.WriteLine(e);
WScript.Quit(1);
}
I know that type "210" for custom action is not the proper one ... but is there any ? Or must I really get through my way by launching a Jscript or VBScript ?
Editing : end of code completed. Tried also to add a custom action through the "vdproj" properties but it refuses because the file is not compatible.
All of the following is untested.
I think you want the custom action type 34 and also msidbCustomActionTypeAsync + msidbCustomActionTypeContinue to run it ASync/NoWait. So custom action type 34+192=226.
Source, according to the docs, doesn't have to be the same directory as the target executable.
Target is...
The Target column of the CustomAction table contains the full path and name of the executable file followed by optional arguments to the executable. The full path and name to the executable file is required. Quotation marks must be used around long file names or paths. The value is treated as formatted text and may contain references to properties, files, directories, or other formatted text attributes.
You can use the "start" shell command to load the text file using the shell. That will open the text file using the user's default text file viewer. You need the full path to start.exe and the full path to the release notes. Note how the docs say it will do string replacement on the Target field. There's a string format to get the full path to the a file given its File table key.
If a substring of the form [#filekey] is found, it is replaced by the full path of the file, with the value filekey used as a key into the File table.
Putting that all together, the following might work for Target:
"[SystemFolder]start.exe" "[#someFileKey]"
That all being said, if you're going to be doing more of these custom actions I would really look into Wix and authoring your own custom actions. Wix would save you from running this javascript post build. Authoring your own custom actions gives you direct access to either .NET or the Windows API. Opening a file with the shell in C#, for example, is pretty straight forward.

uninstalling applications using SCCM SDK

I have been trying to uninstall applications on devices or users using SCCM. I have been successful in creating an application assignment that would install applications, but I haven't been able to get it to uninstall. The code I have been using is:
IResultObject assignment = this.manager.CreateInstance("SMS_ApplicationAssignment");
IResultObject application =
this.manager.GetInstance("SMS_Application.CI_ID=16777339");
assignment["ApplicationName"].StringValue = application["LocalizedDisplayName"].StringValue;
assignment["AssignedCI_UniqueID"].StringValue = application["CI_UniqueID"].StringValue;
assignment["AssignedCIs"].IntegerArrayValue = new[] { application["CI_ID"].IntegerValue};
assignment["AssignmentName"].StringValue = "Deepak's deployment";
assignment["CollectionName"].StringValue = "Deepak's Collection of Devices";
assignment["DisableMomAlerts"].BooleanValue = true;
assignment["NotifyUser"].BooleanValue = false;
assignment["OfferFlags"].IntegerValue = 0;
assignment["DesiredConfigType"].IntegerValue = 1;
assignment["OverrideServiceWindows"].BooleanValue = false;
assignment["RebootOutsideOfServiceWindows"].BooleanValue = false;
assignment["SuppressReboot"].IntegerValue = 0;
assignment["TargetCollectionID"].StringValue = "UKN0000F";
assignment["EnforcementDeadline"].DateTimeValue = DateTime.Now.AddDays(1);
assignment["StartTime"].DateTimeValue = DateTime.Now;
assignment["UseGMTTimes"].BooleanValue = false;
assignment["UserUIExperience"].BooleanValue = false;
assignment["WoLEnabled"].BooleanValue = false;
assignment["RequireApproval"].BooleanValue = true;
assignment["OfferTypeId"].IntegerValue = 2;
assignment.Put();
This code will put up the application as an install deployment in SCCM. How do I get it as an uninstall deployment?
There is an AppAction enumeration, which I suspect is used by the client and not on the server.
typedef enum AppAction
{
appDiscovery = 0,
appInstall = 1,
appUninstall = 2
} AppAction;
Any help would be appreciated!
The setting that needs to be changed is DesiredConfigType.
For your code add the following before put():
assignment["DesiredConfigType"].IntegerValue = 2;
A value of 1 represents install (required) and 2 will uninstall (not allowed).
https://msdn.microsoft.com/en-us/library/hh949014.aspx
The way I do it is first use uninstall.exe to determine the guid of the program, and then create a program for the package I wish to uninstall, and just call uninstall.exe /whatever as the command. This works for most apps that show up in Add/Remove, and if it doesn't show up there then it'll have to be a hack (or script) anyway to uninstall. I believe the reason you're falling short is because if there is no command to uninstall the deployment in sccm, then it has nothing to run.
After you create an uninstall program, you could just call that deployment from your code, and voila.
As long as the target program that you are trying to use was installed via an MSI (Microsoft Installer) then you can loop through the registry to find your product (Registry Location: "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall") And just look at each DisplayName value.
In our environment, I accomplish this task by using Powershell, and we setup a program that specifically uninstalls whatever we are after.
Hope this helps...
Raged.

Unable to Set/Get INSTALLDIR property in "InstallscriptMSI" type project

Tried set/get the INSTALLDIR property using MsiSetProperty/MsiGetProperty methods. But it wouldn't work in case of InstallscriptMSI projects . What am I missing here?
Came across in an other forum that, Installshield properties have limited access in some cases.
NOTE:
MsiGetProperty and MsiSetProperty properties cannot be used for deferred InstallScript custom actions, which do not have access to the active .msi database and do not recognize any Windows Installer properties. They can access only the information that has been written into the execution script.
EXAMPLE:
// Include header file for built-in functions
#include "isrt.h"
// Include header file for MSI API functions and constants
#include "iswi.h"
export prototype Func1(HWND);
function Func1(hMSI)
STRING svName;
NUMBER nvSize, nResponse;
begin
// Retrieve the user's name from the MSI database
nvSize = 256;
MsiGetProperty (hMSI, "USERNAME", svName, nvSize);
nResponse = AskYesNo ("Your name will be registered as " +
svName + ". Is this correct?", YES);
if nResponse = NO then
AskText ("Enter the name that will be registered for " +
"this product.", svName, svName);
MsiSetProperty(hMSI, "USERNAME", svName);
endif;
end;

How do I add/update a property inside an MSI from the command-line?

I have an MSI installer in which I need to add or modify a short text property from the command-line.
This has to be done after the installer is built; I cannot modify the process that produces the installer in the first place. It also has to be executed headless from a script.
When I say "property," it could be an MSI property, a value that gets written to the registery at install-time, or any other mechanism that can get this short custom text into the installed application when it runs.
Example VBScript that you could use to update (or add) a property post-build...
Option Explicit
Const MSI_FILE = "myfile.msi"
Dim installer, database, view
Set installer = CreateObject("WindowsInstaller.Installer")
Set database = installer.OpenDatabase (MSI_FILE, 1)
' Update
Set view = database.OpenView ("UPDATE Property SET Value = '" & myproperty & "' WHERE Property = 'MYPROPERTY'")
' .. or Add (Insert)
Set view = database.OpenView ("INSERT INTO Property (Property, Value) VALUES ('MYPROPERTY', '" & myproperty & "')")
view.Execute
database.Commit
Set database = Nothing
Set installer = Nothing
Set view = Nothing
For more information check out the Windows Installer SDK (part of the Windows SDK)
There's a bunch of example scripts that you can use from the command line to do various MSI manipulation tasks
For example WiRunSQL.vbs lets you execute arbitrary SQL against an MSI.
c:\> msiexec /i yourmsi.msi THEPROPERTYNAME=valueofproperty
For more information type msiexec at the commandline.
EDIT: or change the .msi file itself by using sql statements and updating the property in the properties table:
http://msdn.microsoft.com/en-us/library/aa372021(VS.85).aspx
http://msdn.microsoft.com/en-us/library/aa368568(VS.85).aspx
This is to add to #saschabeaumont 's answer in '09. Currently using dotNet 4.0
Option Explicit
Const MSI_FILE = "myFilePath.msi"
Const PROPERTY_STRING_Value = "FooBar"
Dim installer, database, view
Set installer = CreateObject("WindowsInstaller.Installer")
Set database = installer.OpenDatabase (MSI_FILE, 1)
' Update
Set view = database.OpenView ("UPDATE Property SET Value = '" & PROPERTY_STRING_Value & "' WHERE Property = 'MYPROPERTY'")
' .. or Add (Insert)
Set view = database.OpenView ("INSERT INTO Property (Property, Value) VALUES ('MYPROPERTY', '" & PROPERTY_STRING_Value & "')")
view.Execute()
database.Commit()
Set database = Nothing
Set installer = Nothing
Set view = Nothing

How to get the installation directory?

The MSI stores the installation directory for the future uninstall tasks.
Using the INSTALLPROPERTY_INSTALLLOCATION property (that is "InstallLocation") works only the installer has set the ARPINSTALLLOCATION property during the installation. But this property is optional and almost nobody uses it.
How could I retrieve the installation directory?
Use a registry key to keep track of your install directory, that way you can reference it when upgrading and removing the product.
Using WIX I would create a Component that creates the key, right after the Directy tag of the install directory, declaration
I'd use MsiGetComponentPath() - you need the ProductId and a ComponentId, but you get the full path to the installed file - just pick one that goes to the location of your installation directory. If you want to get the value of a directory for any random MSI, I do not believe there is an API that lets you do that.
I would try to use Installer.OpenProduct(productcode). This opens a session, on which you can then ask for Property("TARGETDIR").
Try this:
var sPath = this.Context.Parameters["assemblypath"].ToString();
As stated elsewhere in the thread, I normally write a registry key in HKLM to be able to easily retrieve the installation directory for subsequent installs.
In cases when I am dealing with a setup that hasn't done this, I use the built-in Windows Installer feature AppSearch: http://msdn.microsoft.com/en-us/library/aa367578(v=vs.85).aspx to locate the directory of the previous install by specifying a file signature to look for.
A file signature can consist of the file name, file size and file version and other file properties. Each signature can be specified with a certain degree of flexibility so you can find different versions of the the same file for instance by specifying a version range to look for. Please check the SDK documentation: http://msdn.microsoft.com/en-us/library/aa371853(v=vs.85).aspx
In most cases I use the main application EXE and set a tight signature by looking for a narrow version range of the file with the correct version and date.
Recently I needed to automate Natural Docs install through Ketarin. I could assume it was installed into default path (%ProgramFiles(x86)%\Natural Docs), but I decided to take a safe approach. Sadly, even if the installer created a key on HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall, none of it's value lead me to find the install dir.
The Stein answer suggests AppSearch MSI function, and it looks interesting, but sadly Natural Docs MSI installer doesn't provide a Signature table to his approach works.
So I decided to search through registry to find any reference to Natural Docs install dir, and I find one into HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components key.
I developed a Reg Class in C# for Ketarin that allows recursion. So I look all values through HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components and if the Main application executable (NaturalDocs.exe) is found into one of subkeys values, it's extracted (C:\Program Files (x86)\Natural Docs\NaturalDocs.exe becomes C:\Program Files (x86)\Natural Docs) and it's added to the system environment variable %PATH% (So I can call "NaturalDocs.exe" directly instead of using full path).
The Registry "class" (functions, actually) can be found on GitHub (RegClassCS).
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("NaturalDocs.exe", "-h");
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
var process = System.Diagnostics.Process.Start (startInfo);
process.WaitForExit();
if (process.ExitCode != 0)
{
string Components = #"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components";
bool breakFlag = false;
string hKeyName = "HKEY_LOCAL_MACHINE";
if (Environment.Is64BitOperatingSystem)
{
hKeyName = "HKEY_LOCAL_MACHINE64";
}
string[] subKeyNames = RegGetSubKeyNames(hKeyName, Components);
// Array.Reverse(subKeyNames);
for(int i = 0; i <= subKeyNames.Length - 1; i++)
{
string[] valueNames = RegGetValueNames(hKeyName, subKeyNames[i]);
foreach(string valueName in valueNames)
{
string valueKind = RegGetValueKind(hKeyName, subKeyNames[i], valueName);
switch(valueKind)
{
case "REG_SZ":
// case "REG_EXPAND_SZ":
// case "REG_BINARY":
string valueSZ = (RegGetValue(hKeyName, subKeyNames[i], valueName) as String);
if (valueSZ.IndexOf("NaturalDocs.exe") != -1)
{
startInfo = new System.Diagnostics.ProcessStartInfo("setx", "path \"%path%;" + System.IO.Path.GetDirectoryName(valueSZ) + "\" /M");
startInfo.Verb = "runas";
process = System.Diagnostics.Process.Start (startInfo);
process.WaitForExit();
if (process.ExitCode != 0)
{
Abort("SETX failed.");
}
breakFlag = true;
}
break;
/*
case "REG_MULTI_SZ":
string[] valueMultiSZ = (string[])RegGetValue("HKEY_CURRENT_USER", subKeyNames[i], valueKind);
for(int k = 0; k <= valueMultiSZ.Length - 1; k++)
{
Ketarin.Forms.LogDialog.Log("valueMultiSZ[" + k + "] = " + valueMultiSZ[k]);
}
break;
*/
default:
break;
}
if (breakFlag)
{
break;
}
}
if (breakFlag)
{
break;
}
}
}
Even if you don't use Ketarin, you can easily paste the function and build it through Visual Studio or CSC.
A more general approach can be taken using RegClassVBS that allow registry key recursion and doesn't depend on .NET Framework platform or build processes.
Please note that the process of enumerating the Components Key can be CPU intense. The example above has a Length parameter, that you can use to show some progress to the user (maybe something like "i from (subKeysName.Length - 1) keys remaining" - be creative). A similar approach can be taken in RegClassVBS.
Both classes (RegClassCS and RegClassVBS) have documentation and examples that can guide you, and you can use it in any software and contribute to the development of them making a commit on the git repo, and (of course) opening a issue on it's github pages if you find any problem that you couldn't resolve yourself so we can try to reproduce the issue to figure out what we can do about it. =)

Resources