Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Management.Automation.Internal;
Comment thread
daxian-dbw marked this conversation as resolved.
Resolved
using System.Management.Automation.Language;
using System.Management.Automation.Runspaces;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;

Expand Down Expand Up @@ -524,6 +525,10 @@ internal static void EarlyParse(string[] args)
}

bool noexitSeen = false;
#if !UNIX
bool consoleAllocated = false;
bool consoleAttached = false;
#endif
for (int i = 0; i < args.Length; ++i)
{
(string SwitchKey, bool ShouldBreak) switchKeyResults = GetSwitchKey(args, ref i, parser: null, ref noexitSeen);
Expand All @@ -542,7 +547,51 @@ internal static void EarlyParse(string[] args)
break;
}
}
else if (MatchSwitch(switchKey, "windowstyle", "w"))
{
#if UNIX
break;
#else
++i;
if (i >= args.Length)
{
break;
}

try
{
ProcessWindowStyle style = (ProcessWindowStyle)LanguagePrimitives.ConvertTo(
args[i],
typeof(ProcessWindowStyle),
CultureInfo.InvariantCulture);

if (style.HasFlag(ProcessWindowStyle.Hidden))
{
// Attach to parent process console.
// AttachConsole(-1) returns false if we run pwsh from Win-R dialog.
// In the case we haven't parent console so ignore error and always set the flag to true.
consoleAttached = true;
AttachConsole(-1);
}
else
{
consoleAllocated = AllocConsole();
ConsoleControl.SetConsoleMode(style);
}
}
catch (PSInvalidCastException)
{
break;
}
#endif
}
}
#if !UNIX
if (!consoleAttached && !consoleAllocated)
{
AllocConsole();
}
#endif
}

/// <summary>
Expand Down Expand Up @@ -827,7 +876,6 @@ private void ParseHelper(string[] args)
{
ProcessWindowStyle style = (ProcessWindowStyle)LanguagePrimitives.ConvertTo(
args[i], typeof(ProcessWindowStyle), CultureInfo.InvariantCulture);
ConsoleControl.SetConsoleMode(style);
}
catch (PSInvalidCastException e)
{
Expand Down Expand Up @@ -1436,7 +1484,15 @@ private bool CollectArgs(string[] args, ref int i)

#if !UNIX
private bool _removeWorkingDirectoryTrailingCharacter = false;

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool AllocConsole();

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool AttachConsole(int dwProcessId);

#endif
}
} // namespace

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,16 @@ class ConsoleHostRawUserInterface : System.Management.Automation.Host.PSHostRawU
internal
ConsoleHostRawUserInterface(ConsoleHostUserInterface mshConsole) : base()
{
defaultForeground = ForegroundColor;
defaultBackground = BackgroundColor;
try
{
defaultForeground = ForegroundColor;
defaultBackground = BackgroundColor;
}
catch (HostException)
{
// Ignore this as it means we're running -WindowStyle Hidden
}

parent = mshConsole;
// cacheKeyEvent is a value type and initialized automatically

Expand Down
24 changes: 12 additions & 12 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ public sealed class UnmanagedPSEntry
/// </param>
public static int Start(string consoleFilePath, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 2)]string[] args, int argc)
{
#if DEBUG
if (args.Length > 0 && !string.IsNullOrEmpty(args[0]) && args[0].Equals("-isswait", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Attach the debugger to continue...");
while (!System.Diagnostics.Debugger.IsAttached)
{
Thread.Sleep(100);
}

System.Diagnostics.Debugger.Break();
}
#endif
// Warm up some components concurrently on background threads.
EarlyStartup.Init();

Expand All @@ -54,18 +66,6 @@ public static int Start(string consoleFilePath, [MarshalAs(UnmanagedType.LPArray
Thread.CurrentThread.CurrentUICulture = NativeCultureResolver.UICulture;
Thread.CurrentThread.CurrentCulture = NativeCultureResolver.Culture;

#if DEBUG
if (args.Length > 0 && !string.IsNullOrEmpty(args[0]) && args[0].Equals("-isswait", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Attach the debugger to continue...");
while (!System.Diagnostics.Debugger.IsAttached)
{
Thread.Sleep(100);
}

System.Diagnostics.Debugger.Break();
}
#endif
int exitCode = 0;
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ private T ReadValueFromFile<T>(ConfigScope scope, string key, T defaultValue = d

if (configData != emptyConfig && configData.TryGetValue(key, StringComparison.OrdinalIgnoreCase, out JToken jToken))
{
return jToken.ToObject<T>(serializer);
return jToken.ToObject<T>(serializer) ?? defaultValue;
}

return defaultValue;
Expand Down
2 changes: 1 addition & 1 deletion src/powershell-win-core/powershell-win-core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<Description>PowerShell on Windows top-level project</Description>
<AssemblyName>pwsh</AssemblyName>
<OutputType>Exe</OutputType>
<OutputType>WinExe</OutputType>
<TieredCompilation>true</TieredCompilation>
<TieredCompilationQuickJit>true</TieredCompilationQuickJit>
<RuntimeIdentifiers>win7-x86;win7-x64</RuntimeIdentifiers>
Expand Down