向PowerShell输入多个命令并返回输入

时间:2019-06-28 06:09:00

标签: vba powershell

我有一个具有大量VBA自动化功能的xlsm电子表格。当前,它发出几个单独的PowerShell命令,并读取输出(我需要PowerShell,以便利用现有cmdlet与另一个软件包进行接口连接。)

我希望通过将一个PowerShell实例与Excel一起打开来增强最终用户的体验,以便我可以根据需要通过管道传输任何新命令,而不必发出相同的先前命令集(包括要求用户登录)每一次)。有谁知道连接到现有PowerShell实例并执行新命令的方法?用于创建Powershell命令并读取输出的当前方法基于用户“ Nilpo”的以下示例:WshShell object's Exec method

Sub shellTest(command As String)
    Dim shell, exec
    Dim retVal, output As Variant
    Dim i As Integer

    Set shell = CreateObject("WScript.Shell")
    Set exec = shell.exec(command)
    While exec.Status = 0 'still running
        Application.Wait (50)
    Wend

    If exec.Status = 2 Then 'failed
        retVal = exec.StdErr.ReadAll
    Else
        retVal = exec.StdOut.ReadAll
    End If

    If retVal <> "" Then
        Debug.Print retVal
    End If
End sub

'Feed in commands
Sub Commands()
    'Log in to app, somehow need to keep PowerShell open for next command?
    shellTest "PowerShell new-pwlogin -BentleyIMS"

    'This is where I need to maintain previous command (login) to get user info
    shellTest "get-PWCurrentUser"
End Sub

0 个答案:

没有答案