0

I am working on a script that would return driver information of given remote computers.
I am wondering if it is possible or is there a workaround where I get it to work with higher privileges.
For example I would create a form that would pop up, asks for user credentials, and the script would run using the entered users credentials.

Here is the script so far:

Sub start()


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_PnPSignedDriver", , 48)
For Each objItem In colItems


    Debug.Print "Description: " & objItem.Description
    Debug.Print "DeviceClass: " & objItem.DeviceClass
    Debug.Print "DeviceID: " & objItem.DeviceID
etc...

So is this possible? Is there a way to achieve this?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Divin3
  • 538
  • 5
  • 12
  • 27
  • Note: Using `On Error Resume Next` as first line makes you blind for errors. This hides all error messages but the errors still occur. You cannot fix them because you don't see them. Implement a complete error handling or remove it. – Pᴇʜ Jul 20 '18 at 07:26
  • 1
    If you run your Excel with RunAs then your code would have the same privileges, as the user credentials you run Excel at. If this is an option for you. Otherwise have a look if [ShellExecute](https://stackoverflow.com/a/24744972/3219613) meets your needs. – Pᴇʜ Jul 20 '18 at 07:35
  • @Pᴇʜ yes I know, when I start writing a code first I want the main part to give me any usefull results, next step is I want to find out if it is possible to do everything I want, and after that I will write the error handling part. I am on the second stage. I don't know if it is even possible to get this working in vba as I would like it to – Divin3 Jul 20 '18 at 07:42
  • @Pᴇʜ - I would like to write it so it would ask for access whenever it needs to run, so RunAs is not really an option. Unless I can get the excel file to reopen itself with other credentials. I have used Shell before but didn't used ShellExecute, can it return data to Excel? – Divin3 Jul 20 '18 at 07:51
  • Note that *especially* during debugging/developing an `On Error Resume Next` is extremely unuseful because you won't even see if there were errors or not. You just can't fix errors that you don't see at all. Hiding error messages instead of fixing the error is never an option especially not when you try to debug your code. • And well, I think the process (in this case Excel) needs the privileges. Your VBA can only access what it has privileges for and it runs within the same process as your Excel. So either RunAs Excel or any other process with the desired privileges. – Pᴇʜ Jul 20 '18 at 10:27
  • @Pᴇʜ ok, you convinced me I removed the `On Error Resume Next` part. And also you gave me an idea that might work out! – Divin3 Jul 20 '18 at 10:49
  • Feel free to come back to post your solution if it worked out. Might be useful for further readers. – Pᴇʜ Jul 20 '18 at 11:26

1 Answers1

0

After some digging, I found out that Win32_PnPSignedDriver supports to be called with user/pass/domain. The following code works for me perfectly.

Set objWMIService = objSWbemLocator.ConnectServer(strComputer, "Root\CIMv2", strUser, strPassword, "MS_409", "ntlmdomain:" + strDomain)

Set colItems = objWMIService.ExecQuery("Select * from Win32_PnPSignedDriver", , 48)
Divin3
  • 538
  • 5
  • 12
  • 27