术语“connect-nacontroller”不被识别为cmdlet,函数,脚本文件或可操作程序的名称

时间:2012-05-18 16:29:13

标签: vb.net powershell

不是这个数字。发布此消息后,我终于开始工作了。我将发布我所做的,以防万一其他人在期货中遇到这个问题。这对我来说是一个很大的疏忽!

在用户名行之前,我添加了这一行:
    “Import-Module DataOnTap”&环境。新浪潮& _ **

我正在尝试从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

1 个答案:

答案 0 :(得分:0)

在用户名行之前,我添加了这一行: “Import-Module DataOnTap”&环境。新浪潮& _ **