Pinky Pal-lingayan
Pinky Pal-lingayan

Reputation: 11

Is it possible to successfully get results when running PsExec in MS Access using Shell.Exec()?

I'm trying to get the disk status of few servers using PSEXEC for the command in shell and append it to the table. But I only get the caption from the shell result.

here is my code:

Option Compare Database
Option Explicit

Const svrUname = "username"
Const svrPass = "password"
Const ForReading = 1

Sub First()
    Dim svr(2, 1) As String

    svr(0, 0) = ""
    svr(0, 1) = "server name"
    svr(1, 0) = "PSEXEC \\(IPADD) -u " & svrUname & " -p " & svrPass " 
    svr(1, 1) = ""

    Dim i As Integer
    For i = 0 To 1
        Call ShellRun(svr(i, 0) & "WMIC logicaldisk get size, caption, " _ 
        & "freespace", svr(i, 1))

    Next

End Sub
Public Sub ShellRun(sCmd As String, svrName As String)
        '  Run a shell command, returning the output as a string
        Dim oShell As Object
        Dim oExec As Object
        Dim oOutput As Object
        Dim a As String

        ' Run command
        Set oShell = CreateObject("Wscript.Shell")
        Set oExec = oShell.Exec(sCmd)
        Set oOutput = oExec.StdOut

        ' handle the results as they are written to and read from the StdOut object
        Dim sInfo As String
        Dim sLine As String

        While Not oOutput.AtEndofStream
            sLine = oOutput.Readline
        Call EXTRACTINFO(sLine, svrName) 
        a = a & sLine
SKIP:
    Wend
    MsgBox a
    oExec.Terminate

End Sub

and the result is only like this and the result is only like this

I was hoping to get these results when run from MS access enter image description here

What seems to be the problem?

Upvotes: 0

Views: 202

Answers (1)

Gustav
Gustav

Reputation: 55961

If I run this code:

Sub First()

    Const svrUname = "username"
    Const svrPass = "password"
    Const ForReading = 1

    Dim svr(2, 1) As String

    svr(0, 0) = ""
    svr(0, 1) = "server name"
    svr(1, 0) = "PSEXEC \\(IPADD) -u " & svrUname & " -p " & svrPass
    svr(1, 1) = ""

    Dim i As Integer
    For i = 0 To 0
        Call ShellRun(svr(i, 0) & "WMIC logicaldisk get size, caption, " _
        & "freespace", svr(i, 1))
    Next

End Sub

Public Sub ShellRun(sCmd As String, svrName As String)

    '  Run a shell command, returning the output as a string
    Dim oShell As Object
    Dim oExec As Object
    Dim oOutput As Object
    Dim a As String

    ' Run command
    Set oShell = CreateObject("Wscript.Shell")
    Set oExec = oShell.Exec(sCmd)
    Set oOutput = oExec.StdOut

    ' handle the results as they are written to and read from the StdOut object
    Dim sInfo As String
    Dim sLine As String

    While Not oOutput.AtEndofStream
        sLine = oOutput.Readline
        'Call EXTRACTINFO(sLine, svrName)
        a = a & sLine
SKIP:
    Wend
    MsgBox a
    oExec.Terminate

End Sub

it returns:

enter image description here

So double-check your EXTRACTINFO function.

Command file:

C:\Folder\PsExec \\server -u user -p password WMIC logicaldisk get size, caption, freespace > C:\SomeFolder\DiskSize.txt

Then read this file.

Upvotes: 1

Related Questions