1

是不是觉得 在我发布这个之后的片刻,我终于让它工作了。我会发布我所做的,以防其他人在未来遇到这个问题。这对我来说是一个很大的疏忽!

在用户名行之前,我添加了这一行:
“Import-Module DataOnTap”& Environment.NewLine & _**

我正在尝试从 VB.Net 2010 代码中运行 PowerShell 脚本。当我在 PowerShell 中运行代码时,它运行良好。当我在 VS2010 中运行它时,我收到错误:“术语 'connect-nacontroller' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,或者如果路径已包含,请验证路径是否正确,然后重试。” 当它到达“Dim results As Collection(Of PSObject) = MyPipeline.Invoke()”行时会发生错误。

这是我的代码(我更改了用户名和密码,一旦我开始工作,我将更改它进入代码的方式。我还更改了卷的名称):

Protected Sub ExecuteCode_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    resultBox.Text = RunScript(powerShellCodeBox.Text)
End Sub

Private Function RunScript(ByVal scriptText As String) As String
    Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()
    MyRunSpace.Open()
    Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
    Dim userName As String = "abc"
    Dim password As String = "123"
    Dim myscript As String = "$username = " & Chr(34) & userName & Chr(34) & Environment.NewLine & _
                 "$password = ConvertTo-SecureString  " & Chr(34) & password & Chr(34) & " -AsPlainText -Force" & Environment.NewLine & _
                 "$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName,$password" & Environment.NewLine & _
                 "connect-nacontroller mydrive -credential $cred" & Environment.NewLine
    MyPipeline.Commands.AddScript(myscript)
    MyPipeline.Commands.Add("Out-String")
    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.ToString()

End Function
4

1 回答 1

0

在用户名行之前,我添加了这一行:“Import-Module DataOnTap”& Environment.NewLine & _**

于 2012-05-22T12:41:26.247 回答