We help IT Professionals succeed at work.

Run VBScript on a remote computer with parameters

Rick Rudolph
Rick Rudolph asked
on
Medium Priority
42 Views
Last Modified: 2020-02-28
I have a vbscript that runs correctly on an Exchange Server The goal is to create a mailbox from the command line.

This is the script:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell.exe -noexit c:\temp\RJRcreatemailbox.ps1 RickRudolph rjr@somedomain.com")


This script runs a powershell script which creates a mailbox for RickRudolph, email address of rjr@somedomain.com

Now what I want to do is to be able to run this script remotely from MSAccess VBA and pass the 2 parameters to the Exchange Server

Any ideas on the best way to do this?

Thank you,

Rick
Comment
Watch Question

David Johnson, CDSimple Geek from the '70s
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
why run vbs to run PowerShell?

you can try connecting with WMI to the remote machine
   Set objProcess = objWMIService.Get("Win32_Process")

Dim strProcess
    strProcess = "c:\windows\system32\Windowspowershell\v1.0\powershell.exe -noexit c:\temp\RJRcreatemailbox.ps1 RickRudolph rjr@somedomain.com")


Dim lngReturn, intPID
    lngReturn = objProcess.Create(strProcess, null, null, intPID)

    If lngReturn = 0 Then
        Wscript.Echo strProcess + " started. PID: " & intPID
    Else
        Wscript.Echo "Error: " & lngReturn
    End If

Open in new window


Why the -noexit parameter?

Author

Commented:
I was unsure of how to run the PowerShell script from VBA

Thank you for the idea. I have never used WMI, I don't see any reference to the server name, should that be placed in front of the c:\temp with UNC naming?

The noexit should go

Can you clarify how the server name is referenced?

Thank you,
Simple Geek from the '70s
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
https://docs.microsoft.com/en-ca/windows/win32/wmisdk/connecting-to-wmi-on-a-remote-computer?redirectedfrom=MSDN
https://docs.microsoft.com/en-ca/windows/win32/wmisdk/connecting-to-wmi-remotely-with-vbscript
strComputer = "Computer_B"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer & "\Root\CIMv2")

Open in new window


I'd use PowerShell remoting https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/running-remote-commands?view=powershell-7

https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/running-remote-commands?view=powershell-7

$session = Enter-PSSession Server01
c:\temp\RJRcreatemailbox.ps1 RickRudolph rjr@somedomain.com"
end-possession

Open in new window

or

Invoke-Command -ComputerName Server01 -ScriptBlock {c:\temp\RJRcreatemailbox.ps1 RickRudolph rjr@somedomain.com"}

Open in new window

Author

Commented:
David,

thanks very much, a big help.


Rick