3

I'm not quite sure why, but if I execute a script from a local folder outside my solution things run fine. When I call the file inside my project I get the following error:

System.Management.Automation.PSSecurityException: 'AuthorizationManager check failed.'

Inner Exception
FileNotFoundException: C:\path\to\myproject\Modules\PSDiagnostics\PSDiagnostics.psm1

This is the code I am trying to run:

InitialSessionState _initialSessionState = InitialSessionState.CreateDefault2();
_initialSessionState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
//var script = Environment.CurrentDirectory + @"\MachineInfo.ps1";
var script = @"C:\scripts\MachineInfo.ps1";
using (var run = RunspaceFactory.CreateRunspace(_initialSessionState))
{
    run.Open();
    var ps = PowerShell.Create(run);
    ps.AddCommand("Import-Module");
    ps.AddParameter("SkipEditionCheck");
    ps.AddArgument("CIMcmdlets");
    ps.Invoke();
    var err = run.SessionStateProxy.PSVariable.GetValue("error");
    System.Diagnostics.Debug.WriteLine(err);//This will reveal any error loading
                
    ps.AddCommand(script);
    ps.AddArgument(Machine);
    var result = ps.Invoke();
    run.Close();
}

Can anyone help me understand why it only works if I call script externally (see the commented out line) from the project source? I'm setting MachineInfo.ps1 to Copy Always and Content (I've tried None too) for Build Action.

This is running via PowerShell 7 inside a C# WinUI 3 .NET Core app. PSDiagnostics.psm1 does not exist inside C:\scripts nor should it have to exist inside my app directory.

nkasco
  • 326
  • 1
  • 2
  • 13
  • I have no explanation, but note that if your script is located in your application folder, use `AppContext.BaseDirectory`, not `Environment.CurrentDirectory`. Also, `ps.Streams.Error` offers direct access to the (non-terminating) errors that occurred. – mklement0 Mar 12 '22 at 17:36
  • I ended up finding these modules in a different folder within my project: `runtimes\win\lib\net6.0\Modules`. Still doesn't quite make sense, my best guess is there is something within the PowerShell SDK that is dynamically choosing module locations based on the provided path. I copied those modules into the root of my project and all seems to be fine now. A band-aid, but it's fine for now. – nkasco Mar 13 '22 at 20:12

1 Answers1

0

Turns out this is a known issue, if you are building a self-contained application. The modules are not deployed into the publish folder correctly. See https://github.com/PowerShell/PowerShell/issues/15274

I fixed this by adding a build step that copies the modules into the correct place. There is a more sophisticated solution in the mentioned Github issue, I reduced it to a simpler build task since I am only targeting Windows.

<!--
Fix PS modules being in wrong place when publishing with a runtime identifier.
-->
<Target Name="PostPublish" AfterTargets="Publish">
  <ItemGroup>
    <PowerShellModules Include="$(ProjectDir)$(OutputPath)runtimes\win\lib\net6.0\Modules\**\*.*"/>
  </ItemGroup>
  <Copy SourceFiles="@(PowerShellModules)" DestinationFiles="$(PublishDir)\Modules\%(RecursiveDir)%(Filename)%(Extension)" />
</Target>
Thomas Glaser
  • 1,670
  • 1
  • 18
  • 26