Closed KaylorDave closed 5 years ago
@KaylorDave WinModule uses implicit remoting to talk to a Windows PowerShell session but before it can do that, it has to create the remote session and that takes significant time since it's starting another instance of PowerShell. Unfortunately there isn't much more we can do to speed that up.
@BrucePay thanks for getting back! Sorry I should have been more specific. I meant it is slowing things down in comparison to the previous version of WinModule. I do not encounter the error or slowdown when using the older version. Only this pre-release candidate. Happy to provide any additional information I can to resolve this, I've currently just reverted to version 0.0.1.
To give some specific data on the difference in experience, I've replaced the contents of my profile with just 2 lines and run some tests with the only difference in profile being the 'RequiredVersion' of WindowsCompatibility (WinModule) being imported-
Import-Module -Name WindowsCompatibility -RequiredVersion 0.0.1
Import-WinModule -Name microsoft.powershell.management
New pwsh window with 0.0.1 loading-
PowerShell 6.1.0
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
Loading personal and system profiles took 3832ms.
New pwsh window with 1.0.0 loading-
PowerShell 6.1.0
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
Loading personal and system profiles took 4817ms.
Set-Location : The variable '$PSBoundParameters' cannot be retrieved because it has not been set.
+ CategoryInfo : InvalidOperation: (PSBoundParameters:String) [Set-Location], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined,Microsoft.PowerShell.Commands.SetLocationCommand
I've done some debugging, but unfortunately I can't seem to get the error to happen when not during profile load time so I haven't been able to trace the exact line the error is occurring at. However, I have been able to narrow it down based on verbose output to be inside the module's psm1 file and located at either line 537 or lines 539-541 (all one command).
The error is for sure in this code block though-
if ($noClobberNames)
{
$importModuleParameters.PassThru = $true
foreach ($name in $noClobberNames)
{
$module = Import-Module -Name $name -NoClobber @importModuleParameters
# Hack using private reflection to keep the proxy module from shadowing the real module.
$null = [PSModuleInfo].
GetMethod('SetName',[System.Reflection.BindingFlags]'Instance, NonPublic').
Invoke($module, @($module.Name + '.WinModule'))
if($PassThru.IsPresent)
{
$module
}
}
}
I haven't been able to repro this but i believe the problem is line 101:
[PSSession] $session = Initialize-WinSession @PSBoundParameters -PassThru
I'll update the code to use explicit parameters. That should hopefully fix your problem.
This fixed it, thanks!
When importing a module inside of the PowerShell profile, the WindowsCompatibility module is throwing an error regarding Set-Location and $PSBoundParameters. The imported module still works correctly, but this slows down profile loading significantly (and adds red to my PowerShell sessions immediately).
pwsh version info-
Contents of $profile file-
Import-WinModule -Name microsoft.powershell.management
Error text received-
Full error context-