简体   繁体   English

PowerShell和.NET

[英]PowerShell and .NET

I've searched around for a solution for the problem I am facing when trying to run PowerShell from VB.net. 我一直在寻找解决方案,以解决尝试从VB.net运行PowerShell时遇到的问题。 I'm not very experienced in coding so excuse if I don't use the proper terminology. 我没有很丰富的编码经验,所以如果我使用的术语不正确,请找借口。

Below I have copied the code that I am working with. 下面,我复制了正在使用的代码。 When I compile the code and execute the program I get a few exceptions that Point to PowerShell modules missing. 当我编译代码并执行程序时,我遇到了一些指向PowerShell模块的异常。 I have done a lot of searching around to make sure I am not missing the required PowerShell modules and / or assemblies. 我已经做了很多搜索,以确保我没有缺少必需的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

Here are the Exceptions that the Compiler Returns when the PowerShell pipeline processes the Script and returns it back to compiler. 以下是PowerShell管道处理脚本并将其返回给编译器时,编译器返回的异常。 Again I have made sure that I have the RSAT installed on my computer. 再次确保已在计算机上安装了RSAT。 Also made sure that I had some old NET 3.5 SDK. 还要确保我有一些旧的NET 3.5 SDK。 All of the search results on this particular issue have not been helpful so far. 到目前为止,有关此特定问题的所有搜索结果都没有帮助。

+       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.")

Any help or suggestion will be greatly appreciated. 任何帮助或建议,将不胜感激。

I came across the same issue today. 今天我遇到了同样的问题。 I figured out what my issue was and may be yours as well. 我弄清楚我的问题是什么,也可能是你的问题。 I'm not that great at programming either. 我也不擅长编程。

This 这个

Script.Append("$ADUSER" + vbCrLf) should be Script.Append("$ADUSER.Name" + vbCrLf) Script.Append("$ADUSER" + vbCrLf)应该是Script.Append("$ADUSER.Name" + vbCrLf)

Once you add the .Name to the command, will output it without any issues. .Name添加到命令后,将毫无问题地输出它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM