Cannot add plugin, Path is null, PSProfile doesn't import anymore
Hi,
I have a function in a ps1 file in a Plugins folder.
I added this folder in the pluginspath configuration, and the function/file is auto-completed when I do add-psprofileplugin.
but when I restart the powershell session, Importing the psprofile module returns me an exception that the path is null.
PSMessageDetails :
Exception : System.Management.Automation.CmdletInvocationException: Cannot bind argument to parameter 'Path' because it is null. ---> System.Management.Automation.ParentContainsErrorRecordException: Cannot bind argument to parameter
'Path' because it is null.
at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)
--- End of inner exception stack trace ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections,
FunctionContext funcContext)
at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
TargetObject :
CategoryInfo : InvalidData: (:) [Import-Module], CmdletInvocationException
FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ImportModuleCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, C:\Users\avezina\Documents\WindowsPowerShell\Modules\PSProfile\0.6.3.20200627\PSProfile.psm1: line 991
at _loadPlugins, C:\Users\avezina\Documents\WindowsPowerShell\Modules\PSProfile\0.6.3.20200627\PSProfile.psm1: line 956
at Load, C:\Users\avezina\Documents\WindowsPowerShell\Modules\PSProfile\0.6.3.20200627\PSProfile.psm1: line 240
at <ScriptBlock>, C:\Users\avezina\Documents\WindowsPowerShell\Modules\PSProfile\0.6.3.20200627\PSProfile.psm1: line 7263
at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}
hello @avezinaATastus ! Thanks for opening this issue up! Can you elaborate on what commands you used (assuming Add-PSProfilePluginPath, but want to confirm). Also, is the function something that you're integrating with PSProfile as a plugin or is it just a function you want loaded into your current session when PSProfile imports?
You should be able to pull up the configuration file @ $PSProfile.Settings.ConfigurationPath. If you open that in your preferred editor and search for PluginPath, you should see the item you added. If you clear that bad path out then reimport PSProfile (e.g. either with -Force or by restarting your PowerShell session), the module should import fine.
I'm going to consider this as a bug for the PluginPaths section so it handles failures a bit better (e.g. log that something failed but no block the overall module import), but I'd also like to make sure that you're using the correct section of PSProfile depending on what the ultimate goal is with that file you're adding to your configuration 😃
I have one repo/folder that has a folder named Plugins. This folder hosts ps1 files, and they have one function each (Might put multiple functions per file if there is nothing against it).
It is just a function I want available in my powershell session.
Add-PSProfilePluginPath -Path C:\Users\avezina\gitrepos\My.PSProfile\Plugins -Save
Add-PSProfilePlugin -Name Start-WindowsSandbox -Save
The file Start-WindowsSandbox.ps1 contains this :
Function Start-WindowsSandbox {
[cmdletbinding(DefaultParameterSetName = "config")]
[alias("wsb")]
Param(
[Parameter(ParameterSetName = "config")]
[ValidateScript( { Test-Path $_ })]
[string]$Configuration = "C:\Sandbox\WinSandBox.wsb",
[Parameter(ParameterSetName = "normal")]
[switch]$NoSetup
)
Write-Verbose "Starting $($myinvocation.mycommand)"
if ($NoSetup) {
Write-Verbose "Launching default WindowsSandbox.exe"
c:\windows\system32\WindowsSandbox.exe
}
else {
Write-Verbose "Launching WindowsSandbox using configuration file $Configuration"
Invoke-Item $Configuration
}
Write-Verbose "Ending $($myinvocation.mycommand)"
}
After doing the two commands (add-*pluginpath and add-*plugin) opening a new powershell session/console won't be able to load PSProfile.
Removing (with Remove-PSProfilePlugin -Name Start-WindowsSandbox -Save and Remove-PSProfilePluginPath -Path C:\Users\avezina\gitrepos\My.PSProfile\Plugins -Save ) in the initial session didn't solve the problem. After removing the pluginpath, the pluginspath in the config was a simple string. After a $psprofile.refresh(), it became an array (containing one string).
Oohh, could it be because of the if ($this.Plugins.Count) in _loadPlugins()
Breaking on line 955 of the PSProfile.psm1 (where the if ($this.Plugins.Count) is) and testing with if ($this.Plugins.Count) {write-host "True"} else {write-host "False"} returns true. I'm going to try with -ge 1