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 @@ -1870,18 +1870,7 @@ protected override void ProcessRecord()
{
if (this.UseWindowsPowerShell)
{
var WindowsPowerShellCompatRemotingSession = CreateWindowsPowerShellCompatResources();
if (WindowsPowerShellCompatRemotingSession != null)
{
foreach(PSModuleInfo moduleProxy in ImportModule_RemotelyViaPsrpSession(importModuleOptions, this.Name, this.FullyQualifiedName, WindowsPowerShellCompatRemotingSession, true))
{
moduleProxy.IsWindowsPowerShellCompatModule = true;
System.Threading.Interlocked.Increment(ref s_WindowsPowerShellCompatUsageCounter);

string message = StringUtil.Format(Modules.WinCompatModuleWarning, moduleProxy.Name, WindowsPowerShellCompatRemotingSession.Name);
WriteWarning(message);
}
}
ImportModulesUsingWinCompat(this.Name, this.FullyQualifiedName, importModuleOptions);
}
}
else
Expand All @@ -1890,6 +1879,27 @@ protected override void ProcessRecord()
}
}

internal override IList<PSModuleInfo> ImportModulesUsingWinCompat(IEnumerable<string> moduleNames, IEnumerable<ModuleSpecification> moduleFullyQualifiedNames, ImportModuleOptions importModuleOptions)
{
PSSession WindowsPowerShellCompatRemotingSession = CreateWindowsPowerShellCompatResources();
if (WindowsPowerShellCompatRemotingSession == null)
{
return new List<PSModuleInfo>();
}

var moduleProxyList = ImportModule_RemotelyViaPsrpSession(importModuleOptions, moduleNames, moduleFullyQualifiedNames, WindowsPowerShellCompatRemotingSession, usingWinCompat: true);
foreach(PSModuleInfo moduleProxy in moduleProxyList)
{
moduleProxy.IsWindowsPowerShellCompatModule = true;
System.Threading.Interlocked.Increment(ref s_WindowsPowerShellCompatUsageCounter);

string message = StringUtil.Format(Modules.WinCompatModuleWarning, moduleProxy.Name, WindowsPowerShellCompatRemotingSession.Name);
WriteWarning(message);
}

return moduleProxyList;
}

private void SetModuleBaseForEngineModules(string moduleName, System.Management.Automation.ExecutionContext context)
{
// Set modulebase of engine modules to point to $pshome
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2367,14 +2367,7 @@ internal PSModuleInfo LoadModuleManifest(
{
if (importingModule)
{
var commandInfo = new CmdletInfo("Import-Module", typeof(ImportModuleCommand));
using var ps = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace);
ps.AddCommand(commandInfo);
ps.AddParameter("PassThru", true);
ps.AddParameter("Name", moduleManifestPath);
ps.AddParameter("UseWindowsPowerShell", true);

var moduleProxies = ps.Invoke<PSModuleInfo>();
IList<PSModuleInfo> moduleProxies = ImportModulesUsingWinCompat(new string [] {moduleManifestPath}, null, new ImportModuleOptions());

// we are loading by a single ManifestPath so expect max of 1
if(moduleProxies.Count > 0)
Expand Down Expand Up @@ -4867,6 +4860,8 @@ internal void CleanupWindowsPowerShellCompatResources()
}
}

internal virtual IList<PSModuleInfo> ImportModulesUsingWinCompat(IEnumerable<string> moduleNames, IEnumerable<ModuleSpecification> moduleFullyQualifiedNames, ImportModuleOptions importModuleOptions) {throw new System.NotImplementedException();}

private void RemoveTypesAndFormatting(
IList<string> formatFilesToRemove,
IList<string> typeFilesToRemove)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function New-EditionCompatibleModule
param(
[Parameter(Mandatory = $true)][string]$ModuleName,
[string]$DirPath,
[string[]]$CompatiblePSEditions)
[string[]]$CompatiblePSEditions,
[string]$ErrorGenerationCode='')

$modulePath = Join-Path $DirPath $ModuleName

Expand All @@ -41,7 +42,7 @@ function New-EditionCompatibleModule

New-Item -Path $modulePath -ItemType Directory

New-Item -Path $psm1Path -Value "function Test-$ModuleName { `$true } function Test-${ModuleName}PSEdition { `$PSVersionTable.PSEdition }" -Force
New-Item -Path $psm1Path -Value "$ErrorGenerationCode function Test-$ModuleName { `$true } function Test-${ModuleName}PSEdition { `$PSVersionTable.PSEdition }" -Force

if ($CompatiblePSEditions)
{
Expand Down Expand Up @@ -130,7 +131,6 @@ function New-TestNestedModule
[scriptblock]::Create($newManifestCmd).Invoke()
}


Describe "Get-Module with CompatiblePSEditions-checked paths" -Tag "CI" {

BeforeAll {
Expand Down Expand Up @@ -363,6 +363,50 @@ Describe "Import-Module from CompatiblePSEditions-checked paths" -Tag "CI" {
}
}

Describe "Additional tests for Import-Module with WinCompat" -Tag "CI" {
BeforeAll {
$ModuleName = "DesktopModule"
$basePath = Join-Path $TestDrive "WinCompatModules"
Remove-Item -Path $basePath -Recurse -ErrorAction SilentlyContinue
# create an incompatible module that generates an error on import
New-EditionCompatibleModule -ModuleName $ModuleName -CompatiblePSEditions "Desktop" -Dir $basePath -ErrorGenerationCode '1/0;'
}

Context "Tests that ErrorAction/WarningAction have effect when Import-Module with WinCompat is used" {
BeforeAll {
Add-ModulePath $basePath
}

AfterAll {
Restore-ModulePath
}

It "Verify that Error is generated with default ErrorAction" -Skip:(-not $IsWindows) {
$LogPath = Join-Path $TestDrive (New-Guid).ToString()
pwsh -NoProfile -NonInteractive -c "[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('TestWindowsPowerShellPSHomeLocation', `'$basePath`');Import-Module $ModuleName" *> $LogPath
$LogPath | Should -FileContentMatch 'divide by zero'
}

It "Verify that Warning is generated with default WarningAction" -Skip:(-not $IsWindows) {
$LogPath = Join-Path $TestDrive (New-Guid).ToString()
pwsh -NoProfile -NonInteractive -c "[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('TestWindowsPowerShellPSHomeLocation', `'$basePath`');Import-Module $ModuleName" *> $LogPath
$LogPath | Should -FileContentMatch 'loaded in Windows PowerShell'
}

It "Verify that Error is Not generated with -ErrorAction Ignore" -Skip:(-not $IsWindows) {
$LogPath = Join-Path $TestDrive (New-Guid).ToString()
pwsh -NoProfile -NonInteractive -c "[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('TestWindowsPowerShellPSHomeLocation', `'$basePath`');Import-Module $ModuleName -ErrorAction Ignore" *> $LogPath
$LogPath | Should -Not -FileContentMatch 'divide by zero'
}

It "Verify that Warning is Not generated with -WarningAction Ignore" -Skip:(-not $IsWindows) {
$LogPath = Join-Path $TestDrive (New-Guid).ToString()
pwsh -NoProfile -NonInteractive -c "[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('TestWindowsPowerShellPSHomeLocation', `'$basePath`');Import-Module $ModuleName -WarningAction Ignore" *> $LogPath
$LogPath | Should -Not -FileContentMatch 'loaded in Windows PowerShell'
}
}
}

Describe "PSModulePath changes interacting with other PowerShell processes" -Tag "Feature" {
$PSDefaultParameterValues = @{ 'It:Skip' = (-not $IsWindows) }

Expand Down