vb.netpowershellpnp.powershell

VB .NET hosted powershell not running module cmdlets


I have a VB.NET 2015 Ent app that created a couple years ago on Win 10, among other things it runs powershell commands, I got a new computer (win 11), I've set everything up but the shell commands are not working anymore, but just the ones coming from installed modules, native commands run just fine. I'm usinsg PNP.Powershell v1.9.0 to upload files to a OneDrive folder.

When I created it I imported "System.Management.Automation.Runspaces" from NuGet, but it is not available anymore so I replaced it with "Microsoft.PowerShell.5.ReferenceAssemblies". Windows PowerShell is version 5.1

This was my code:

Dim runspace As Runspace = RunspaceFactory.CreateRunspace()
        runspace.Open()
        Dim pipeline As Pipeline = runspace.CreatePipeline()
pipeline.Commands.AddScript("Connect-PnPOnline -Url '" & webUrl & "' -ClientId '" & cid & "' -ClientSecret '" & cs & "' -WarningAction Ignore")
        pipeline.Commands.AddScript("Get-PnPContext")
        pipeline.Commands.AddScript("$Items = Get-PnPFolderItem -FolderSiteRelativeUrl $RelativeURL")
pipeline.Commands.AddScript("$RelativeURL = '" & libNameTemp & "'")
                pipeline.Commands.AddScript("$CSVFile ='" & filePath & "'")
                pipeline.Commands.AddScript("$FolderObject = Get-PnPFolder -Url $RelativeURL")
                pipeline.Commands.AddScript("$Upload = Add-PnPFile -Path $CSVFile -Folder $FolderObject")
pipeline.Invoke()

When Invoke runs, it fails on "Connect-PnPOnline" and throws this error:

System.Management.Automation.CommandNotFoundException: The 'Connect-PnPOnline' command was found in the module 'PnP.PowerShell', but the module could not be loaded. For more information, run 'Import-Module PnP.PowerShell'

I've tried running import module right before the connect cmdlet, but that one fails with this error:

System.Management.Automation.CmdletInvocationException: Errors occurred while loading the format data file: Microsoft.PowerShell, , C:\Program Files\WindowsPowerShell\Modules\PnP.PowerShell\1.9.0\PnP.PowerShell.Format.ps1xml: The file was skipped because of the following validation exception: AuthorizationManager check failed

I tried this way instead...

Dim pipeline As PowerShell = PowerShell.Create()

Try, does not catch any error, but the cmdlet won't run and Pipeline.HadErrors is True and InvocationStateInfo.Reason is Nothing

All the powershell script runs perfectly fine when ran on PS ISE

thanks for your help!

Tried replicating same environment as before expecting it to run but it didn't


Solution

  • I found what the issue was, and it was not the nuget package but the architecture the project was targeting. My app was calling PowerShell 32 bits. The PNP.PowerShell module was only installed on the 64 bits, so I installed it on the 32 and added a validation code on the script as well. It is now working perfectly.