簡體   English   中英

PowerShell和.NET

[英]PowerShell and .NET

我一直在尋找解決方案,以解決嘗試從VB.net運行PowerShell時遇到的問題。 我沒有很豐富的編碼經驗,所以如果我使用的術語不正確,請找借口。

下面,我復制了正在使用的代碼。 當我編譯代碼並執行程序時,我遇到了一些指向PowerShell模塊的異常。 我已經做了很多搜索,以確保我沒有缺少必需的PowerShell模塊和/或程序集。

Imports System.Collections.ObjectModel
Imports System.Management.Automation
Imports System.Management.Automation.Runspaces
Imports System.Management.Automation.Host
Imports System.Text
Public Class Form1
Private Function RunScript(ByVal Script As String) As Object
    Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()
    MyRunSpace.Open()
    Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
    MyPipeline.Commands.AddScript(Script)
    Dim results As Collection(Of PSObject) = MyPipeline.Invoke()
    MyRunSpace.Close()
    Dim MyStringBuilder As New StringBuilder()
    For Each obj As PSObject In results
        MyStringBuilder.AppendLine(obj.ToString())
    Next
    Return MyStringBuilder
End Function
Private Function GetAD()
    Dim Script As New StringBuilder()
    Script.Append("$ADUSER= Get-ADUser -filter { cn -eq " + Chr(34) + "Common Name" + Chr(34) + " }" + vbCrLf)
    Script.Append("$ADUSER" + vbCrLf)
    Return Script.ToString()
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Call RunScript(GetAD)
End Sub


Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
    RichTextBox1.Text = ToString()
End Sub
End Class

以下是PowerShell管道處理腳本並將其返回給編譯器時,編譯器返回的異常。 再次確保已在計算機上安裝了RSAT。 還要確保我有一些舊的NET 3.5 SDK。 到目前為止,有關此特定問題的所有搜索結果都沒有幫助。

+       Exception   Exception thrown: 'System.DllNotFoundException' in System.Management.Automation.dll ("Unable to load DLL 'wldp.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)")    System.DllNotFoundException

Exception thrown: 'System.Management.Automation.ItemNotFoundException' in System.Management.Automation.dll ("Cannot find path 'C:\windows\system32\windowspowershell\v1.0\Modules\ActiveDirectory\Microsoft.ActiveDirectory.Management' because it does not exist.")

Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll ("Could not load file or assembly 'Microsoft.ActiveDirectory.Management' or one of its dependencies. The system cannot find the file specified.")

Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll ("Could not load file or assembly 'file:///C:\windows\system32\windowspowershell\v1.0\Modules\ActiveDirectory\Microsoft.ActiveDirectory.Management' or one of its dependencies. The system cannot find the file specified.")

Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll ("Could not load file or assembly 'Microsoft.ActiveDirectory.Management' or one of its dependencies. The system cannot find the file specified.")

Exception thrown: 'System.Management.Automation.PSArgumentException' in System.Management.Automation.dll ("The Windows PowerShell snap-in 'Microsoft.ActiveDirectory.Management' is not installed on this computer.")

任何幫助或建議,將不勝感激。

今天我遇到了同樣的問題。 我弄清楚我的問題是什么,也可能是你的問題。 我也不擅長編程。

這個

Script.Append("$ADUSER" + vbCrLf)應該是Script.Append("$ADUSER.Name" + vbCrLf)

.Name添加到命令后,將毫無問題地輸出它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM