Import-Module not working from within WinForms Application

Viewed 888

I have a VB.net app where I invoke Import-Module on a PowerShell from within my vb.net Window Application but the error says it could not find the module. Error as below.

Import-Module : The specified module 'MSOnline' was not loaded because no valid module file was found in any module directory.

enter image description here

When I load the same Module by launching the PowerShell externally in the usual way it works fine. Image as below.

enter image description here

The VB script is as below

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim procStartInfo As New ProcessStartInfo
    Dim procExecuting As New Process

    With procStartInfo
        .UseShellExecute = True
        .FileName = "powershell.exe"
        .WindowStyle = ProcessWindowStyle.Normal
        .Verb = "runas" 'add this to prompt for elevation
        Dim psscript As String = My.Resources.mymsolPS
        procStartInfo.Arguments = psscript
        procExecuting = Process.Start(procStartInfo)
    End With

End Sub

My PowerShell Script is saved in my.resource as a txt file. My PowerShell Script is as below.

Import-Module Msonline
Connect-msolService

I replaced the PowerShell script to Get-Help and that works only it dosnt work when I use Import-Module Msonline.

One more information that can be shared is the module is stored in the below location.

C:\Windows\System32\WindowsPowerShell\v1.0\Modules\MSOnline\MSOnline.psd1

Any Help is greatly appreciated. Thanks in Advance

Update 2: More fiddling with it found some thing which i am not sure if is relevant.

If I launch the powershell from within my VB.net and run the below command I cant see the MSOnline module.

PS C:\windows\system32>> cd $env:WINDIR\System32\WindowsPowerShell\v1.0\Modules\
PS C:\windows\System32\WindowsPowerShell\v1.0\Modules>> dir

If I run the PowerShell directly from my system and run the above script I can see the Module

d-----       11/22/2017   2:59 PM                MSOnline

Still a mystery for me which I cant crack. :(

2 Answers

A difference I notice is when launching from your app, or locally, the directory is either your user, or system.. so maybe the way PS is being loaded it can't find the module.

What about if you provide a full path to the module?

I've had much better luck using RunSpace - I use it to pass any powershell commands - here are a snippet from one of the sites and some examples to look at:

'Create the runspace.
Using R As System.Management.Automation.Runspaces.Runspace = _
System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()

   'Create the pipeline
   Using P As System.Management.Automation.Runspaces.Pipeline = R.CreatePipeline()

      'Open the runspace.
      R.Open()

      'Create each command (in this case just one)...
      Dim Cmd As New System.Management.Automation.Runspaces.Command("C:\script.ps1", True)

      '...and add it to the pipeline.
      P.Commands.Add(Cmd)

      'Execute the commands and get the response.
      Dim Result As System.Collections.ObjectModel.Collection(Of _
      System.Management.Automation.PSObject) = P.Invoke()

      'Close the runspace.
      R.Close()

      'Display the result in the console window.
      For Each O As System.Management.Automation.PSObject In Result
         Console.WriteLine(O.ToString())
      Next

   End Using
End Using

The last one provides a pretty solid breakdown of what it's doing. Let me know if you can't get it to work, I can try to see if this import works on an app.

I actually found the solution after hours of pain. This is pretty silly solution.

Went I went to my application Properties I found that the Preferred run was set to 32 bit hence when my PowerShell was launched from within it was looking for the module under SYSWOW where its suppose to look it under System32. I unchecked the "Preferred 32 BIT" and not it imports the module from system 32.

Thought I should share this silly miss so that others should not suffer the same.

Related