MSEndpointMgr / IntuneWin32App

Provides a set of functions to manage all aspects of Win32 apps in Microsoft Intune.
MIT License
351 stars 91 forks source link

IntuneWin32App v1.4.3 Invoke-Executable.ps1 fails due to hidden window #134

Closed jaspain closed 8 months ago

jaspain commented 11 months ago

In the module update from v1.4.2 to v1.4.3, Invoke-Executable.ps1 was modified to parameterize the value of $ProcessStartInfoObject.RedirectStandardOutput and provide a value of $False. This function still doesn't work properly with PowerShell v7.40. Apparently the error stems from trying to run IntuneWinAppUtil.exe in a hidden window. It can be fixed by setting the following property values of $ProscessStartInfoObject in lines 19 - 22:

$ProcessStartInfoObject.CreateNoWindow = $false
$ProcessStartInfoObject.UseShellExecute = $true
$ProcessStartInfoObject.RedirectStandardOutput = $false
$ProcessStartInfoObject.RedirectStandardError = $false

Alternatively, in New-IntuneWin32AppPackage.ps1, line 105, substituting

$PackageInvocation = Start-Process -FilePath $IntuneWinAppUtilPath -ArgumentList "-c ""$($SourceFolder)"" -s ""$($SetupFile)"" -o ""$($OutputFolder)"" -q" -PassThru -Wait

for

$PackageInvocation = Invoke-Executable -FilePath $IntuneWinAppUtilPath -Arguments "-c ""$($SourceFolder)"" -s ""$($SetupFile)"" -o ""$($OutPutFolder)"" -q" -RedirectStandardOutput $false

also fixes the problem.

jaspain commented 11 months ago

See also IntuneWinAppUtil.exe cannot be launched from c# code without console visible.

NickolajA commented 10 months ago

This has annoyed me quite a lot, that it's not possible to hide the output. When using the module and this function inside of VS Code, the output produced from IntuneWinAppUtil.exe breaks the output session of VS Code somehow. It's a bummer that this method that I changed to with 1.4.3 doesn't seem to work well in PowerShell 7.4.0, it works well in 5.x though that I usually perform most tests on.

I'll add a work around in 1.4.4 to fix this, hopefully Microsoft fixes their newly introduced issue as well. While I'm at it, I'll forward it to them!

jaspain commented 10 months ago

Thank you again, @NickolajA. Jeff.

martincaddick commented 10 months ago

I've had heaps of heartache for months with this from MS. Then I tried this and it just worked. Hidden away in https://github.com/Microsoft/Microsoft-Win32-Content-Prep-Tool is a -q switch. Could it be that simple?

.\intunewinapputil.exe -c $SourceFolder -s $SetupFile -o $OutputFolder -q

NickolajA commented 10 months ago

Would you mind helping me out in testing if this updated code blocks work:

  1. Edit the Invoke-Executable.ps1 script in the Private folder:
function Invoke-Executable {
    param(
        [parameter(Mandatory = $true, HelpMessage = "Specify the file name or path of the executable to be invoked, including the extension.")]
        [ValidateNotNullOrEmpty()]
        [string]$FilePath,

        [parameter(Mandatory = $false, HelpMessage = "Specify arguments that will be passed to the executable.")]
        [ValidateNotNull()]
        [string]$Arguments,

        [parameter(Mandatory = $false, HelpMessage = "Specify whether standard output should be redirected.")]
        [ValidateNotNull()]
        [bool]$RedirectStandardOutput = $true,

        [parameter(Mandatory = $false, HelpMessage = "Specify whether standard error output should be redirected.")]
        [ValidateNotNull()]
        [bool]$RedirectStandardError = $true,

        [parameter(Mandatory = $false, HelpMessage = "Specify whether to create a new window for the executable.")]
        [ValidateNotNull()]
        [bool]$CreateNoWindow = $true,

        [parameter(Mandatory = $false, HelpMessage = "Specify whether to create a new window for the executable.")]
        [ValidateNotNull()]
        [bool]$UseShellExecute = $false
    )
    try {
        # Create the Process Info object which contains details about the process
        $ProcessStartInfoObject = New-object -TypeName "System.Diagnostics.ProcessStartInfo"
        $ProcessStartInfoObject.FileName = $FilePath
        $ProcessStartInfoObject.CreateNoWindow = $CreateNoWindow
        $ProcessStartInfoObject.UseShellExecute = $UseShellExecute
        $ProcessStartInfoObject.RedirectStandardOutput = $RedirectStandardOutput
        $ProcessStartInfoObject.RedirectStandardError = $RedirectStandardError 

        # Add the arguments to the process info object
        if ($Arguments.Count -gt 0) {
            $ProcessStartInfoObject.Arguments = $Arguments
        }

        # Create the object that will represent the process
        $Process = New-Object -TypeName "System.Diagnostics.Process"
        $Process.StartInfo = $ProcessStartInfoObject

        # Start process
        [void]$Process.Start()

        # Wait for the process to exit
        $Process.WaitForExit()

        # Return an object that contains the exit code
        return [PSCustomObject]@{
            ExitCode = $Process.ExitCode
        }
    }
    catch [System.Exception] {
        throw "$($MyInvocation.MyCommand): Error message: $($_.Exception.Message)"
    }
}
  1. Update the New-IntuneWin32AppPackage.ps1 script in the Public folder on line 105:
$PackageInvocation = Invoke-Executable -FilePath $IntuneWinAppUtilPath -Arguments "-c ""$($SourceFolder)"" -s ""$($SetupFile)"" -o ""$($OutPutFolder)"" -q" -RedirectStandardOutput $false -RedirectStandardError $false -CreateNoWindow $false -UseShellExecute $true

Instead of changing the behaviour for all instances where Invoke-Executable is used, I've opted to parameterize the configuration options for the ProcessStartInfo object.

jaspain commented 10 months ago

@NickolajA I tested with PowerShell 7.4.0 and IntuneWin32App 1.4.3 as patched. I'm confirming that your updated code blocks work. I briefly see the window in which IntuneWinAppUtil.exe is running, as would be expected with Invoke-Executable -CreateNoWindow $false. Note that with PowerShell 7.4.0, I also had to patch \Private\Invoke-AzureStorageBlobUploadChunk.ps1 per issue Add-IntuneWin32App.ps1 'CommitFile' failed error with PowerShell 7.4.0 #128 to get the intunewin file to upload properly. Thanks. Jeff.

NickolajA commented 10 months ago

@jaspain: great thanks! Now I need to test this in an ADO pipeline as well yo see if it's working well unattended where there's no interactive user session logged on.

Regarding the other function, that code has already been added to my local branch of 1.4.4 with a comment thanking you for the fix.

AScott-WWF commented 10 months ago

I've just noticed PowerShell v7.4.1 was released yesterday - I haven't yet been able to get this working, but I'm hopeful... as one of the fixes mentions: Fix Start-Process -PassThru to make sure the ExitCode property is accessible for the returned Process object Source: https://github.com/PowerShell/PowerShell/releases/tag/v7.4.1

jaspain commented 10 months ago

@AScott-WWF @NickolajA Confirmed that with PowerShell 7.4.1, IntuneWin32App 1.4.3 does work properly with or without the patches to Invoke-Executable.ps1 and New-IntuneWin32AppPackage.ps1 described above. The patch to Invoke-AzureStorageBlobUploadChunk.ps1 described in issue #128 is required in any event. IntuneWin32App 1.4.2 unpatched does not work with PowerShell 7.4.1.

crcobb commented 10 months ago

@NickolajA Your code above fixes the issue I was seeing with running IntuneWInAppUtil.exe. It does now open a command window but it doesn't crash now. Thanks

twelch-ricohnz commented 9 months ago

I can't for the life of me get this working with PowerShell 7.4.1 - it still crashes out with -532462766 If I run it in PowerShell 5.1 it works fine.

jaspain commented 9 months ago

@twelch-ricohnz Asking for confirmation that your environment is PowerShell 7.4.1, IntuneWin32App 1.4.3, and that you applied the patch to \Private\Invoke-AzureStorageBlobUploadChunk.ps1 per Add-IntuneWin32App.ps1 'CommitFile' failed error with PowerShell 7.4.0 #128, My comment on 11/21/2023. This has been working consistently for me with no errors.

twelch-ricohnz commented 9 months ago

@jaspain I suspect we have our wires crossed here.

I get "WARNING: Unexpected error occurred while packaging Win32 app. Return code from invocation: -532462766" when running New-IntuneWin32AppPackage

Doesn't your fix above relate to Add-IntuneWin32App ?

My environment is definitely 7.4.1, IntuneWin32App 1.4.3 and IntuneWinAppUtil 1.8.5.0

jaspain commented 9 months ago

@twelch-ricohnz Sorry for the confusion. When New-IntuneWin32AppPackage invokes IntuneWinAppUtil.exe, does it open a separate window transiently? IntuneWinAppUtil.exe is known not to work when run in a hidden window. In New-IntuneWin32AppPackage.ps1, try replacing line 105: $PackageInvocation = Invoke-Executable -FilePath $IntuneWinAppUtilPath -Arguments "-c ""$($SourceFolder)"" -s ""$($SetupFile)"" -o ""$($OutPutFolder)"" -q" -RedirectStandardOutput $false with $PackageInvocation = Start-Process -FilePath $IntuneWinAppUtilPath -ArgumentList "-c ""$($SourceFolder)"" -s ""$($SetupFile)"" -o ""$($OutputFolder)"" -q" -PassThru -Wait

twelch-ricohnz commented 9 months ago

@jaspain - confirmed as working! Nice one.

Any idea when we might see it rolled into a released version; since we use this widely across our team?

jaspain commented 9 months ago

Thank you, @twelch-ricohnz. It's my understanding that the package author @NickolajA has incorporated this fix and the fix for Invoke-AzureStorageBlobUploadChunk.ps1 into v1.4.4. I haven't heard anything about a release date, and so I continue to monitor this project for news. In any event the workarounds have been effective and reliable.

NickolajA commented 8 months ago

It's coming very soon! :)

NickolajA commented 8 months ago

I see that there's been some additional testing here since I posted the proposed changes.

@twelch-ricohnz are you saying that with the changes to the Invoke-Execution function and then New-IntuneWin32AppPackage.ps1 function in the Public folder on line 105, it still doesn't work?

twelch-ricohnz commented 8 months ago

@NickolajA I can confirm that both fixes advised by @jaspain resolved the issues I faced thanks.

NickolajA commented 8 months ago

Thank you for verifying! 1.4.4 is out now.

jaspain commented 8 months ago

@NickolajA I'm confirming that v1.4.4 resolves this issue by executing IntuneWin32App.exe in a visible window. To use a hidden window again, we'll have to wait for the Microsoft-Win32-Content-Prep-Tool project to resolve their issue IntuneWinAppUtil.exe cannot be launched from c# code without console visible #111.