2017-11-23 59 views
2

私はVB.netアプリケーションを持っています。これは私のVB.NETウィンドウアプリケーション内からPowerShell上でImport-Moduleを起動しますが、モジュールが見つからないというエラーが表示されます。エラーは次のとおりです。WinFormsアプリケーション内からImport-Moduleが動作しない

インポートモジュール:モジュールディレクトリに有効なモジュールファイルが見つからなかったため、指定されたモジュール 'MSOnline'がロードされませんでした。私はそれが正常に動作し、通常の方法で外部からPowerShellを起動して、同じモジュールをロード

enter image description here

。画像は以下の通りです。

enter image description here

VBスクリプトが

私のPowerShellスクリプトはtxtファイルとしてmy.resourceに保存され

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の次のようです。私のPowerShellスクリプトは以下の通りです。

Import-Module Msonline 
Connect-msolService 

私はGet-HelpにPowerShellスクリプトを置き換え、それは私がImport-Module Msonlineを使用する場合にのみ、それは仕事をdosnt動作します。

さらに共有できる情報は、モジュールが以下の場所に保存されていることです。

C:\ WINDOWS \ System32に\ WindowsPowerShell \ v1.0を\モジュール\ MSOnline \ MSOnline.psd1

任意のヘルプは大歓迎です。アドバンス

で おかげ

アップデート2: はもっとそれをいじるには、私は関連性がある場合はわからないいくつかのことを発見しました。

VB.netからpowershellを起動し、以下のコマンドを実行すると、MSOnlineモジュールが表示されません。

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

私は私のシステムから直接PowerShellを実行して、上記のスクリプトを実行した場合、私はまだ、モジュール

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

私は割れカント私のために謎を見ることができます。 :(ので、多分方法は、PSはそれがモジュールを見つけることができませんロードされます。..あなたのアプリから起動したとき、私は気づく

答えて

0

私は実際に数時間の痛みの後に解決策を見つけました。これはかなり愚かな解決策です。

私は自分のアプリケーションに行きましたプロパティ私は、PowerShellが起動されたときに、優先実行が32ビットに設定されていることがわかりました。私は "Preferred 32 BIT"のチェックを外し、システム32からモジュールをインポートしません。

私はこの愚かなミスを分かち合い、他の人が同じように苦しんではいけないと思います。

0

違いがある、または局部的に、ディレクトリは、ユーザー、またはシステムのいずれかである。

モジュールのフルパスを指定するとどうなりますか?

'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 

リットルそれが何をしているのかについて、かなり確かな内訳を提供しています。あなたがそれを動作させることができない場合私に教えてください、私はこのインポートがアプリケーション上で動作するかどうかを確認しようとすることができます。

+0

上記のコマンドは、通常のPowerShellスクリプトを使用すると魅力的です。例えばスクリプトファイルをget-helpで置き換えると、import-Moduleを使用するとモジュールが見つかりません。私が孤立したのは、「CD C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ Modules \ MSOnline \」と入力した場合です。 –

関連する問題