oleg-shilo/wixsharp

Execute CA which needed Powershell in Version 5.1

Torchok19081986 opened this issue ยท 6 comments

Hallo, i write currently Bundle that needs to execute CA where i have to check if any Powershell Version installed and if yes, get version of it.
CA is relativ simple. Get Envriroment.GetEnvironmentVariable("WINDIR")\powershell.exe works fine, but how do i get output of this
version send back my bundle ?
I can get output from powershell by set in process args to $PSVersionTable , but cant get add or show it to bundle installer during runtime.
Any idea i would appreaciate.

This was always a difficult scenario. WiX does not support it directly. It allows sending Bundle.Variable to msi (via property). But does not allow reading it back.

Thus CA for bundle is impossible in principle. CA is only available for MSIs.

The only way out of it is to use the custom BA where you can do your check at startup of the BA.
Interestingly enough these guys also opted to the same approach: https://stackoverflow.com/questions/45491158/set-a-wix-burn-bootstrapper-variable-from-a-bundled-msi

Thansk for info, @oleg-shilo. I run , again, in issue, that cant be solved normally, like always. ๐Ÿ˜„. Embeded Links in Stackoverflow is dead, unfortunatelly. I cant also read it direct from bundle, right ? I just need output of version in bundle. I cant create wix bundle variable and in dialog load event get output of version of powershell directly ? Bundle is own process straight forward , right?

i created test wpf project in NET 8.0 and want to show if somehow possible. Is harder as i thought.
Screenshot 2024-05-15 152703
need all packages from nuget to execute following code

using PowerShell ps = PowerShell.Create();
ps.AddScript("$PSVersionTable.PSVersion");
try
{
    var result = ps.Invoke();
    if (ps.HadErrors)
    {
        // Handle errors
        MessageBox.Show("Error executing PowerShell command");
        return;
    }

    if (result.Count > 0)
    {
        var psVersion = result[0].Properties["Major"].Value.ToString() + "." +
        result[0].Properties["Minor"].Value.ToString();

        MessageBox.Show("PowerShell version: " + psVersion);

        //Bootstrapper.Engine.StringVariables["POWERSHELLVERSION"] = psVersion;
    }
    else
    {
        MessageBox.Show("Result is 0");
    }
}
catch (Exception ex)
{
  
    MessageBox.Show("PowerShell is not installed or cannot be accessed: " + ex.Message);
}

most annoying fact, all packages shoudl have same version major and minor installed on targetsystem, and if not it cant find powershell.exe
๐Ÿ˜ตโ€๐Ÿ’ซ๐Ÿ˜ตโ€๐Ÿ’ซ๐Ÿ˜ตโ€๐Ÿ’ซ.

Great, txs for sharing.

And if someone wants only a simple command execution then this can be done even like this:

RunPowerShellScript("Get-Desktop 1 | Switch-Desktop");
. . .

static void RunPowerShellScript(string command)
{
    var startInfo = new ProcessStartInfo
    {
        FileName = "powershell.exe",
        Arguments = $"-NoProfile -ExecutionPolicy unrestricted -Command \"{command}\"",
        UseShellExecute = false,
        CreateNoWindow = true
    };
    Process.Start(startInfo);
}

thanks again, @oleg-shilo . One thing, works until powershell version 5.1. In Version later than 5.1 powershell.exe was named pwsh.exe . MS has added NET Framework >=5.0 into powershell, for better using .

Noted
Txs