Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WIX: Where and how should my CustomAction create and read a temporary file?

I have a script CustomAction (Yes, I know all about the opinions that say don't use script CustomActions. I have a different opinion.)

I'd like to run a command, and capture the output. I can do this using the WScript.Shell COM object, then invoking shell.Exec(). But, this flashes a visible console window for the executed command.

To avoid that, I understand I can use the shell.Run() call, and specify "hidden" for the window appearance. But .Run() doesn't give me access to the StdOut of the executed process, so that means I'd need to create a temporary file and redirect the exe output to the temp file, then later read that temp file in script.

Some questions:

  • is this gonna work?

  • How do I generate a name for the temporary file? In .NET I could use a static method in the System.IO namespace, but I am using script here. I need to insure that the use has RW access, and also that no anti-virus program is going to puke on this.

  • Better ideas? I am trying very hard to avoid C/C++.


I could avoid all this if there were a way to query websites in IIS7 from script, without resorting to the IIS6 Compatibility pack, without using .NET (Microsoft.Web.Administration.ServerManager), and without execing a process (appcmd list sites). I already asked a separate question on that topic; any suggestions on that would also be appreciated.

like image 713
Cheeso Avatar asked Mar 22 '11 17:03

Cheeso


People also ask

Where are temp files created?

Most programs will create temp files in a folder called C:\Users\AppData\Local\Temp — that's likely where your computer stores the majority of your temporary files. It's safe to empty out the AppData\Local\Temp folder and delete the temp files you find there.

How do I use temp files?

On your keyboard, press the Windows + R keys at the same time. In the Open field, type %temp%, then press ENTER. The temp folder will open. You can also access it on your Windows 10 PC via the shortcut button below, then choose Temporary files.

Which operator helps in creating temporary files?

A temp file can be created by directly running mktemp command. The file created can only be read and written by the file owner by default. To ensure the file is created successfully, there should be an OR operator to exit the script if the file fails to be created.

How are temp files created?

Explanation:Temporary files, often known as foo files, are created by your operating system when you are running or completing a task on the computer. ... For instance, if you are using graphics, video or media editing software, your computer will create temporary files to store and save information as you go along.


1 Answers

Answering my own question...

  1. yes, this is going to work.

  2. Use the Scripting.FileSystemObject thing within Javascript. There's a GetTempName() method that produces a file name suitable for temporary use, and a GetSpecialFolder() method that gets the location of the temp folder. There's even a BuildPath() method to combine them.

  3. so far I don't have any better ideas.

Here's the code I used:

function GetWebSites_IIS7_B()
{
    var ParseOneLine = function(oneLine) {
        ...regex parsing of output...
    };

    LogMessage("GetWebSites_IIS7_B() ENTER");

    var shell = new ActiveXObject("WScript.Shell");
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var tmpdir = fso.GetSpecialFolder(SpecialFolders.TemporaryFolder);
    var tmpFileName = fso.BuildPath(tmpdir, fso.GetTempName());
    var windir = fso.GetSpecialFolder(SpecialFolders.WindowsFolder);
    var appcmd = fso.BuildPath(windir,"system32\\inetsrv\\appcmd.exe") + " list sites";

    // use cmd.exe to redirect the output
    var rc = shell.Run("%comspec% /c " + appcmd + "> " + tmpFileName, WindowStyle.Hidden, true);
    // WindowStyle.Hidden == 0
    var ts = fso.OpenTextFile(tmpFileName, OpenMode.ForReading);
    var sites = [];

    // Read from the file and parse the results.
    while (!ts.AtEndOfStream) {
        var oneLine = ts.ReadLine();
        var line = ParseOneLine(oneLine);
        LogMessage("  site: " + line.name);
        sites.push(line);
    }
    ts.Close();
    fso.DeleteFile(tmpFileName);

    return sites;
}
like image 124
Cheeso Avatar answered Oct 14 '22 04:10

Cheeso