I have got this working line of code
I would like to run via VBScript a share program on a remote computer in a domain environment. The first part is ok where it is asking me to enter a computer name, but the problem is in the second part. I don't know how to run the program on the remote computer that I've entered in the first part.
computer = inputbox ("What computer do you wish to check? (Press Enter if this computer)","Computer")
set WMI = GetObject("WinMgmts://" & computer)
If computer="" then computer = "this computer"
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("""\\compname\Share\progr.exe""")
Set objShell = nothing
After running this script, it runs the program on my computer, not on the remote computer. I want to run the program on a specific computer that I have entered from keyboard.
From vbscript, the better way is using wmi. Please refer to this to get a complete information on how to get a connection to a remote computer via wmi and the problems you will face to do it.
Follow the information in microsoft page, and you will end with a objWMIService variable pointing to the WMI services of the remote machine. Then,
Set objProcess = objWMIService.Get("Win32_Process")
Dim strProcess
strProcess = "notepad.exe"
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
use objWMIService (remember, its the remote machine) to get a reference to the processes collection (of the remote machine), and create a new process.
Related
I have a batch script MyBatch.bat, it successfully calls MyVbScript.vbs by the statement:
cscript //nologo %~dp0\MyVbScript.vbs %NewPort%
Where NewPort is a environment variable value.
MyVbScript.vbs is getting called successfully (tested) and value of NewPort is passed to VBS successfully (tested) from batch script.
But I am NOT able to generate the URL for launching in default browser. I want to generate the URL like http://localhost:7006/MyWebApplication where 7006 is value of NewPort variable.
MyVbScript.vbs contains:
Option Explicit
Dim wsh
Dim port
Dim myNum
Set wsh=WScript.CreateObject("WScript.Shell")
port = WScript.Arguments(0)
'MsgBox port//For testing
wsh.Run " "http://localhost:"&port&"/MyWebApplication" "
'End of VB script
How to generate the URL and launch it in default browser. I don't want to specify Chrome or Mozilla and IE because user may not have a particular browser.
The following worked:
wsh.Run "http://localhost:"&port&"/MyWebApplication"
Any seasoned programmer can suggest other code too if I have written more LOC unnecessarily.
I use a very simple version control whereby I use the "tag" property on the Switchboard to record the version of the database.
On a linked "mastertable" I have a master record that shows the current version. If this is out of sync, then there is code in the switchboard to initiate a simple file copy of the new client version to the user's desktop.
I can't use any EXE type auto-installers to do this, so had to come up with an all Access solution, but having some issues with shell commands and timing that is causing the "auto" part of the installer to be inconsistent.
Below are my codes for the Client and my standalone "Installer" database (which only has one form that opens on startup and initiates the copy code). I use lookup tables for all file locations, but will use strings in my example.
Client.mdb:
strInstaller = "c:\Installer\Installer.mdb"
set obj = CreateObject("access.application") 'previously tried SHELL command
with obj
.visible = true
.userControl = true
.openCurrentDatabase (strInstaller)
end with
application.quit
Simple enough. So the above code just opens my Installer mdb which opens a form and executes the following on open.
Installer.mdb:
strFileName = "ClientDB"
strMaster = "D:\" & strFileName
strClient= dlookup("DBPath", "UserTbl", "LanID = '" & MyID & "'") & "\" & strFileName
if len(dir(strClient)) <> 0 then
kill strClient
end if
filecopy strMaster, strClient
The dlookup in the strClient simply looks up the path where the user opened up the instance of the client mdb. (I record this on every instance).
My issues is that I am not getting consistent results. Sometimes it will copy the file, and sometimes it won't. I've changed it to run from a button on the Installer Form, and it works every time, so I'm guessing it has something to do with timing.
I've tried putting a pause function before the kill command, and that seems to help if I set the pause to 3 or 4 seconds. I originally used Shell to open the Installer, but got rid of it as I heard that it was running concurrently with the installer.mdb code.
I'm thinking it's something obvious, but I've been staring at this for about an hour and can't figure it out. Ideally, I don't want the user to interact with this form using an "Install" button, but would like it to happen in the background. i.e. I want to set the .visible = false at some point.
Can anyone see an issue with this method, or suggest a better method to push out new copies of the client - and I can't use any EXE install programs.
You likely want to use the shell() command. You can use CreateObject, but that feature is NOT available in the runtime. (and worse, it will not work).
The simple trick is ONCE you shell out to the upgrade program, you want to QUIT the main program (since you can’t copy over it while it is running).
The code I use is thus:
strCurrentDir = CurrentProject.Path & "\"
' path to msaccess.exe is determined here
strShellProg = q & SysCmd(acSysCmdAccessDir) & "msaccess.exe" & q & " "
' path to current dir...assume upgrade program in same folder
strShellProg = strShellProg & q & strCurrentDir & "UpGrade.accDE" & q
If Shell(strShellProg, vbNormalFocus) > 0 Then
Application.Quit
Else
MsgBox "Un able to run upgrade", vbCritical, AppName
Application.Quit
End If
In above “q” is = """" (a single double quote).
So you shell out, and then immediate do a application.Quit. The upgrade program should have a “prompt” like “about to upgrade – ok”. That “prompt the user has to hit gives the main application time to quit. I also useally write out the locations to a text file in the above code - but the above steps to shell() is the main takeaway solution here.
As noted, you can use create object, but THEN if you quit the main application, then that variable holding the upgrade program will also go out of scope (or worse, the main application cannot shut down because it is “hosting” an automated copy of the upgrade application by CreateObject.
So you in practical terms don’t want to use createObject, since this means the main program is “hosting” or “holding” a copy of the upgrade program and the main program really can’t quit.
I am trying to: 1 Discover the user that is logged into the machine, add it to variable 1.
Then find out the SID in HKEY_USERS for this said user, add it to variable 2.
Once i have the SID, pipe it into a .reg file replacing the values that i have in it.
Code 1 finds the user. the result then goes into the code 2 replacing "Myusername" This finds the SID.
I then need to send this to a .reg file i made to replace certain registry paths.
Am i on the right track? Or way off? can anyone make these 2 peices of code work together?
Thanks guys. Any help is appreciated.
So far i have:
Code 1:
Set objNetwork = CreateObject("Wscript.Network")
Wscript.Echo "The current user is " & objNetwork.UserName
and
Code 2:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objAccount = objWMIService.Get _
("Win32_UserAccount.Name='Myusername',Domain='gnb'")
Wscript.Echo objAccount.SID
Is there a way to launch two Explorer windows side-by-side (vertically tiled) with a Batch script?
If not, how might I do this with VBS?
I have modified the VBS script above by Hackoo to do exactly what the OP wants...
The comments in the script explain exactly what it will do.
If the two windows don't set into correct position, increase the 'Sleep' time and try again.
If you want a horizontal split, use 'objShell.TileHorizontally'.
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Launches two Explorer windows side-by-side filling the screen dimensions.
''' Minimizes all current open windows before launch; if this is not done,
''' the current open windows will also be resized along with our two windows.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim Calc,AppData,objShell
Calc = "%windir%\system32\calc.exe"
AppData = "%AppData%"
Set objShell = CreateObject("shell.application")
objShell.MinimizeAll
Call Explore(Calc)
WScript.Sleep 800
Call Explore(AppData)
WScript.Sleep 800
objShell.TileVertically
Set objShell = nothing
'*****************************************************
Function Explore(Path)
Dim ws
set ws = CreateObject("wscript.shell")
Explore = ws.run("Explorer /n,/select,"& Path &"")
End Function
'*****************************************************
This might be in the same category as your question. :)
How can a batch file run a program and set the position and size of the window?
Unfortunately it seems that its not possible without any external third part software in batch. Probably easier in VBS - if so the answer should be in the link.
Try this code :
Option Explicit
Dim Calc,AppData
Calc = "%windir%\system32\calc.exe"
AppData = "%AppData%"
Call Explore(Calc)
Call Explore(AppData)
'*****************************************************
Function Explore(Path)
Dim ws
set ws = CreateObject("wscript.shell")
Explore = ws.run("Explorer /n,/select,"& Path &"")
End Function
'*****************************************************
I'm working on setting up Host Scripts for Mokafive player on Windows. I've made a few VBSCripts and have worked with batch files trying to gather information on Mokafive Player start. I'm trying to capture data such as Firewall status, and Encryption status. I've tried multiple variations on gathering this data back to the Mokafive Management Server, such as echoing the results out in VBScrpt, Echoing the results back into the .bat file needed to run the VBScript, with no success.
Does anyone have any code, or examples of a working Host Script for Mokafive Player?
After meeting with several engineers it looks like the VBScript must echo out the results from each condition to an output file. This file will then be read by the launcher XML as a key value pair and parsed out to the database. so per each line that needs to be output you'd use the following...
Set objfs = CreateObject("Scripting.FileSystemObject")
outputfile = "outputfile.out
Set objFile = objfs.CreateTextFile(outputfile,True)
'Your Script Here
objFile.Write "Key=" & "Value" & vbNewLine