1010using System . Collections ;
1111using System . Threading ;
1212using System . Management . Automation . Internal ;
13- using System . Management . Automation . Runspaces ;
1413using System . Xml ;
1514using System . Runtime . InteropServices ;
1615using Dbg = System . Management . Automation . Diagnostics ;
@@ -222,6 +221,29 @@ private string Path
222221 }
223222 }
224223
224+ /// <summary>
225+ /// Gets true if Path is Console Application.
226+ /// </summary>
227+ private bool IsConsoleApplication => ! IsWindowsApplication ;
228+
229+ /// <summary>
230+ /// Gets true if Path is Windows Application.
231+ /// </summary>
232+ private bool IsWindowsApplication
233+ {
234+ get
235+ {
236+ if ( ! _isWindowsApplication . HasValue )
237+ {
238+ _isWindowsApplication = CheckIfWindowsApplication ( Path ) ;
239+ }
240+
241+ return _isWindowsApplication . Value ;
242+ }
243+ }
244+
245+ private bool ? _isWindowsApplication ;
246+
225247 #endregion ctor/native command properties
226248
227249 #region parameter binder
@@ -476,7 +498,7 @@ private void InitNativeProcess()
476498 bool notDone = true ;
477499 if ( ! string . IsNullOrEmpty ( executable ) )
478500 {
479- if ( IsConsoleApplication ( executable ) )
501+ if ( CheckIfConsoleApplication ( executable ) )
480502 {
481503 // Allocate a console if there isn't one attached already...
482504 ConsoleVisibility . AllocateHiddenConsole ( ) ;
@@ -532,7 +554,7 @@ private void InitNativeProcess()
532554 _isRunningInBackground = true ;
533555 if ( startInfo . UseShellExecute == false )
534556 {
535- _isRunningInBackground = IsWindowsApplication ( _nativeProcess . StartInfo . FileName ) ;
557+ _isRunningInBackground = IsWindowsApplication ;
536558 }
537559 }
538560
@@ -935,9 +957,9 @@ private static void KillChildProcesses(int parentId, ProcessWithParentId[] curre
935957 /// </summary>
936958 /// <param name="fileName"></param>
937959 /// <returns></returns>
938- private static bool IsConsoleApplication ( string fileName )
960+ private static bool CheckIfConsoleApplication ( string fileName )
939961 {
940- return ! IsWindowsApplication ( fileName ) ;
962+ return ! CheckIfWindowsApplication ( fileName ) ;
941963 }
942964
943965 /// <summary>
@@ -946,10 +968,22 @@ private static bool IsConsoleApplication(string fileName)
946968 /// <param name="fileName"></param>
947969 /// <returns></returns>
948970 [ ArchitectureSensitive ]
949- private static bool IsWindowsApplication ( string fileName )
971+ private static bool CheckIfWindowsApplication ( string fileName )
950972 {
973+ #if UNIX
974+ return false ;
975+ #else
951976 if ( ! Platform . IsWindowsDesktop ) { return false ; }
952977
978+ // SHGetFileInfo() does not understand reparse points and returns 0 ("non exe or error")
979+ // so we are trying to get a real path before.
980+ // It is a workaround for Microsoft Store applications.
981+ string realPath = Microsoft . PowerShell . Commands . InternalSymbolicLinkLinkCodeMethods . WinInternalGetTarget ( fileName ) ;
982+ if ( realPath is not null )
983+ {
984+ fileName = realPath ;
985+ }
986+
953987 SHFILEINFO shinfo = new SHFILEINFO ( ) ;
954988 IntPtr type = SHGetFileInfo ( fileName , 0 , ref shinfo , ( uint ) Marshal . SizeOf ( shinfo ) , SHGFI_EXETYPE ) ;
955989
@@ -968,6 +1002,7 @@ private static bool IsWindowsApplication(string fileName)
9681002 // anything else - is a windows program...
9691003 return true ;
9701004 }
1005+ #endif
9711006 }
9721007
9731008 #endregion checkForConsoleApplication
@@ -1266,7 +1301,7 @@ private void CalculateIORedirection(out bool redirectOutput, out bool redirectEr
12661301 redirectOutput = true ;
12671302 redirectError = true ;
12681303 }
1269- else if ( Platform . IsWindowsDesktop && IsConsoleApplication ( this . Path ) )
1304+ else if ( Platform . IsWindowsDesktop && IsConsoleApplication )
12701305 {
12711306 // On Windows desktops, if the command to run is a console application,
12721307 // then allocate a console if there isn't one attached already...
0 commit comments