Skip to content
Merged
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 @@ -48,28 +48,6 @@ static PowerShellProcessInstance()
/// <param name="useWow64"></param>
public PowerShellProcessInstance(Version powerShellVersion, PSCredential credential, ScriptBlock initializationScript, bool useWow64)
{
string psWow64Path = PwshExePath;

if (useWow64)
{
string procArch = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");

if (!string.IsNullOrEmpty(procArch) && (procArch.Equals("amd64", StringComparison.OrdinalIgnoreCase) ||
procArch.Equals("ia64", StringComparison.OrdinalIgnoreCase)))
{
psWow64Path = PwshExePath.ToLowerInvariant().Replace("\\system32\\", "\\syswow64\\");

if (!File.Exists(psWow64Path))
{
string message =
PSRemotingErrorInvariants.FormatResourceString(
RemotingErrorIdStrings.IPCWowComponentNotPresent,
psWow64Path);
throw new PSInvalidOperationException(message);
}
}
}

string processArguments = " -s -NoLogo -NoProfile";

if (initializationScript != null)
Expand All @@ -88,7 +66,7 @@ public PowerShellProcessInstance(Version powerShellVersion, PSCredential credent
// to 'false' in our use, we can ignore the 'WindowStyle' setting in the initialization below.
_startInfo = new ProcessStartInfo
{
FileName = useWow64 ? psWow64Path : PwshExePath,
FileName = PwshExePath,
Arguments = processArguments,
UseShellExecute = false,
RedirectStandardInput = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,20 +481,13 @@ public virtual ScriptBlock InitializationScript
private ScriptBlock _initScript;

/// <summary>
/// Launces the background job as a 32-bit process. This can be used on
/// Launches the background job as a 32-bit process. This can be used on
/// 64-bit systems to launch a 32-bit wow process for the background job.
/// </summary>
[Parameter(ParameterSetName = StartJobCommand.FilePathComputerNameParameterSet)]
[Parameter(ParameterSetName = StartJobCommand.ComputerNameParameterSet)]
[Parameter(ParameterSetName = StartJobCommand.LiteralFilePathComputerNameParameterSet)]
public virtual SwitchParameter RunAs32
{
get { return _shouldRunAs32; }

set { _shouldRunAs32 = value; }
}

private bool _shouldRunAs32;
public virtual SwitchParameter RunAs32 { get; set; }

/// <summary>
/// Powershell Version to execute the background job.
Expand Down Expand Up @@ -574,12 +567,24 @@ protected override void BeginProcessing()
RemotingErrorIdStrings.IPCPwshExecutableNotFound,
PowerShellProcessInstance.PwshExePath);

var exception = new PSNotSupportedException(message);
var errorRecord = new ErrorRecord(
exception,
"IPCPwshExecutableNotFound",
ErrorCategory.NotInstalled,
PowerShellProcessInstance.PwshExePath);
new PSNotSupportedException(message),
"IPCPwshExecutableNotFound",
ErrorCategory.NotInstalled,
PowerShellProcessInstance.PwshExePath);

ThrowTerminatingError(errorRecord);
}

if (RunAs32.IsPresent && Environment.Is64BitProcess)
{
// We cannot start a 32-bit 'pwsh' process from a 64-bit 'pwsh' installation.
string message = RemotingErrorIdStrings.RunAs32NotSupported;
var errorRecord = new ErrorRecord(
new PSNotSupportedException(message),
"RunAs32NotSupported",
ErrorCategory.InvalidOperation,
targetObject: null);

ThrowTerminatingError(errorRecord);
}
Expand Down Expand Up @@ -620,7 +625,6 @@ protected override void CreateHelpersForSpecifiedComputerNames()
}

NewProcessConnectionInfo connectionInfo = new NewProcessConnectionInfo(this.Credential);
connectionInfo.RunAs32 = _shouldRunAs32;
connectionInfo.InitializationScript = _initScript;
connectionInfo.AuthenticationMechanism = this.Authentication;
connectionInfo.PSVersion = this.PSVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,8 @@ Do you want to continue?</value>
<value>The pwsh executable cannot be found at "{0}".
Note that 'Start-Job' is not supported by design in scenarios where PowerShell is being hosted in other applications. Instead, usage of the 'ThreadJob' module is recommended in such scenarios.</value>
</data>
<data name="IPCWowComponentNotPresent" xml:space="preserve">
<value>The "{0}" executable file was not found. Verify that the WOW64 feature is installed.</value>
<data name="RunAs32NotSupported" xml:space="preserve">
<value>Cannot start a 32-bit 'pwsh' process from the 64-bit 'pwsh' installation. Install the 32-bit 'pwsh' if you need to run PowerShell in a 32-bit process.</value>
</data>
<data name="IPCServerProcessReportedError" xml:space="preserve">
<value>The background process reported an error with the following message: {0}.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ Describe "Job Cmdlet Tests" -Tag "CI" {
Wait-Job -Timeout 60 -id $j.id | Should -Not -BeNullOrEmpty
receive-job -id $j.id | Should -Be 2
}
It "-RunAs32 not supported from 64-bit pwsh" -Skip:(-not [System.Environment]::Is64BitProcess) {
{ Start-Job -ScriptBlock {} -RunAs32 } | Should -Throw -ErrorId "RunAs32NotSupported,Microsoft.PowerShell.Commands.StartJobCommand"
}
It "-RunAs32 supported in 32-bit pwsh" -Skip:([System.Environment]::Is64BitProcess) {
$job = Start-Job -ScriptBlock { 1+1 } -RunAs32
Receive-Job $job -Wait | Should -Be 2
}
}
Context "Jobs with arguments" {
It "Start-Job accepts arguments" {
Expand Down
2 changes: 0 additions & 2 deletions test/xUnit/csharp/test_PowerShellAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

namespace PSTests.Sequential
{
// Not static because a test requires non-const variables
public static class PowerShellHostingScenario
{
// Test that it does not throw an exception
[Fact]
public static void TestStartJobThrowTerminatingException()
{
Expand Down