在vb.net中执行Powershell命令

时间:2014-02-23 19:35:02

标签: powershell

我需要用vb.net创建一个gui作为vm扩展开关acl。 powershell片段是

add-VMNetworkAdapterExtendedAcl -computername "Ati-PC" -Vmname "Windows7"
    -action deny -diretion inbound -remoteport "80" -protocol "TCP" -weight 10
    -LocalIPAddress "ANY" -RemoteIPAddress "ANY" -LocalPort "ANY" RemotePort "80"

我为“创建”按钮编写的VB代码是

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
    Dim runSpace As Runspace = RunspaceFactory.CreateRunspace()
    runSpace.Open()
    Dim pipeLine As Pipeline = runSpace.CreatePipeline()
    Dim ExtendedACL As New Command("Add-VMNetworkAdapterExtendedACL")
     ExtendedACL.Parameters.Add("vmname", txtVMName.Text)
    ExtendedACL.Parameters.Add("ComputerName", txtPhySvr.Text)
    ExtendedACL.Parameters.Add("Action", cmbAction.Text)
    ExtendedACL.Parameters.Add("Direction", cmbDirection.Text)
    ExtendedACL.Parameters.Add("Protocol", cmbProtocol.Text)
    ExtendedACL.Parameters.Add("LocalIPAddress", txtLocalIP.Text)
    ExtendedACL.Parameters.Add("RemoteIPAdddress", txtRemoteIP.Text)
    ExtendedACL.Parameters.Add("LocalPort", txtLocalPort.Text)
    ExtendedACL.Parameters.Add("RemotePort", txtRemotePort.Text)
    ExtendedACL.Parameters.Add("weight", txtWeight.Text)
    pipeLine.Commands.Add(ExtendedACL)
    Dim vNameProcesses As Collection(Of PSObject) = pipeLine.Invoke()

    Dim psObject As PSObject
    For Each psObject In vNameProcesses
        Dim process As Process = TryCast(psObject.BaseObject, Process)
    Next

End Sub

但是这段代码是一个例外。有人可以帮我找到问题吗?

此致 加扬

0 个答案:

没有答案