I am trying to create a script that will use the copy con to write a file
Set objNetwork = CreateObject("Wscript.Network")
CurrentUser = objNetwork.UserName
Set Wshell = CreateObject("WScript.Shell")
Wshell.Run "%COMSPEC% cd C:\Users\" & CurrentUser & _
"\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup & copy con master.vbs & x = 1 & x=2^Z", 0, True
The problem here is that while using ^Z does simulate the output of Crtl+Z, CMD doesn't treat them the same.
As you can see, I wanted the console window to be hidden, so using something like SendKeys won't work here.
Any suggestions?
Related
This question already has an answer here:
Prevent VBscript app from showing Console Window
(1 answer)
Closed 1 year ago.
Hey guys im trying to start a programm hidden with less priority but the command prompt still pop out.
Dim WShell
Set WShell = CreateObject("WScript.Shell")
WShell.Run "cmd /c Start /belowNormal " & "C:\Users\Desktop\sonso.exe -uri www.google.de",0
Set WShell = Nothing
You asked how to reduce exe priority from VBS without using CMD so here is an example.
(I admit it is not all my own work, but performs as advertised for Notepad.exe)
Don't ask me exactly how it works and it took me ages to find as the vbs example is "missing" from the Microsoft setpriority-method-in-class-win32-process link I include.
Set Priority.vbs
' Set priority of process to Below Normal on Server 2008 & Vista+'
' From https://learn.microsoft.com/en-gb/windows/win32/cimwin32prov/setpriority-method-in-class-win32-process'
' Below Normal (16384) Indicates a process that has priority above IDLE_PRIORITY_CLASS (64),'
' but below NORMAL_PRIORITY_CLASS (32). NOTE:- combined namespace is \\. \Root\CIMV2
' Others ABOVE_NORMAL (32768) HIGH_PRIORITY (128) *REAL_TIME ( 256) *Note To set Realtime,
' the caller must have SeIncreaseBasePriorityPrivilege (SE_INC_BASE_PRIORITY_PRIVILEGE).
' Without this privilege, the highest the priority can be set to is High Priority.
' Use your RUN commands here and replace name = Notepad.exe below or replace Notepad.exe with arg[0]
Set objShell = WScript.CreateObject("WScript.Shell")
' With CMD but minimised so we can open it from taskbar
' objShell.Run "%comspec% /d /c start /min Notepad.exe fred.txt", 0, True
' Without CMD BUT NOT hidden, otherwise how are you going to edit anything or close Notepad.
objShell.Run "c:\windows\Notepad.exe fred.txt"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\Root\CIMV2")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Notepad.exe'")
For Each objProcess in colProcesses
objProcess.SetPriority(16384)
Next
I am trying to create a script that will use the copy con to write a file
Set objNetwork = CreateObject("Wscript.Network")
CurrentUser = objNetwork.UserName
Set Wshell = CreateObject("WScript.Shell")
Wshell.Run "%COMSPEC% cd C:\Users\" & CurrentUser & _
"\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup & copy con master.vbs & x = 1 & x=2^Z", 0, True
The problem here is that while using ^Z does simulate the output of Crtl+Z, CMD doesn't treat them the same.
As you can see, I wanted the console window to be hidden, so using something like SendKeys won't work here.
Any suggestions?
So I have a .ps1 file which creates a form.
That Form takes 10-20secs depending on PCs performance and connection on first load.
Now I am currently using VBS to load a simple .gif file as a loading screen concurrently running the .ps1 file right after.
My issue at the moment is that, I want to close the loading screen when the form pops up. I tried to determine via processes but that failed because of the it loads powershell.exe but the form takes 10sec...
Is it this possible?
Of have you guys got a better idea to do this?
Dim i
Dim strComputer
Dim FindProc
Dim counter
counter = 0
strComputer = "."
FindProc = "powershell.exe"
'Load the gif file
Set objExplorer = CreateObject("InternetExplorer.Application")
With objExplorer
.Navigate "about:blank"
.Visible = 1
.Document.Title = "Show Image"
.Toolbar=False
.Statusbar=False
.Top=400
.Left=400
.Height=355
.Width=435
.Document.Body.InnerHTML = "<img src='\\10.10.67.173\Templates$\Scripts\Resources\loadingtest.gif'>"
End With
'Run the PS script
Set objShell = CreateObject("Wscript.shell")
objShell.Run "CMD /C START /B " & objShell.ExpandEnvironmentStrings("%SystemRoot%") & "\System32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -file \\10.10.67.173\Templates$\Scripts\FormSignature-V0.9.5.ps1", 0, False
'Determine when to close Loading screen
Do While counter < 3
wscript.Sleep 2000
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select Name from Win32_Process WHERE Name LIKE '" & FindProc & "%'")
If colProcessList.count>0 then
'Quit the process if it finds its running
WScript.Echo("found")
'objExplorer.quit
else
'Do nothing
WScript.Echo("not found")
End if
Set objWMIService = Nothing
Set colProcessList = Nothing
counter = counter + 1
Loop
Set objShell = Nothing
In your VB script:
Create a file in an folder the PS script can access.
Launch your loading image.
Launch your PS script.
In your wait loop, every second or so, check if the file still exists.
If it does, close the instance of IE and exit your script.
In your PS script:
After your form initialization code is finished or the first action after the form load, locate and delete the file.
I work in a hospital environment and right now im doing PC deployments. Part of the deployment requires us to view 2 files on a network drive looking for information regarding the old systems. They use specific ports and or TTY's to view information in each department.
I am trying to create a VBS file that can open 2 files in 2 different notepad windows. The first one opens up but the pcview.cfg keeps giving me an error. Im trying to link to the same location that the HBOWEM32 is pointed to. Can anyone solve? For security reasons I have taken out the exact location of the network drive. The code below prompts for a specific folder name which is the old pc name. After entering that data it opens the HBOWEM32 files fine but says it cannot find the other part. I Have manually looked inside the folder and the pcview.cfg file DOES exist. I just want a faster way of opening these rather than brute forcing through the run prompt.
Here is the code.
CONST strDir = "<Netowrk Location)"
Dim WshShell
set objShell = CreateObject("WScript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")
function findFolder(strDir, strFlag)
set objFolder = objFSO.GetFolder(strDir)
for each objSubFolder in objFolder.SubFolders
if (inStr(objSubFolder.Name, strFlag)) then
findFolder = objSubFolder.Path
exit function
else
findFolder = findFolder (objSubFolder.Path, strFlag)
end if
next
end function
strFlag = inputBox("Enter Computer Name:")
strWeb = findFolder(strDir, strFlag) & "\HBOWEM32.ini"
objShell.Run strWeb
Set WshShell = CreateObject ("WScript.Shell")
WshShell.Run ("notepad.exe """ + "\\<same location as above>\Pcview.cfg""")
Use Option Explicit
Don't create variables you don't use (WshShell, objShell)
Improve your variable names (strFlag seems to be a computer name, strWeb seems to be the full specification of a file)
Don't lump different info into one variable (strWeb contains the folder path to re-use and the specific file name)
Use diagnostics output (at least while developing)
In code:
Option Explicit
...
Dim strComputer : strComputer = InputBox("Enter Computer Name:")
Dim strFolder : strFolder = findFolder(strDir, strComputer)
Dim strIniFSpec : strIniFSpec = objFSO.BuildPath(strFolder, "HBOWEM32.ini")
WScript.Echo "will run '" & strIniFSpec & "'"
objShell.Run strIniFSpec
Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
Dim strCfgFSpec : strCfgFSpec = objFSO.BuildPath(strFolder, "Pcview.cfg")
Dim strCmd : strCmd = "notepad.exe """ & strCfgFSpec & """"
WScript.Echo "will run '" & strCmd & "'"
WshShell.Run strCmd
(not tested, please be carefull)
The below script works on Windows XP as expected. The script abc.bat would run as Administrator without a prompt for password(password is supplied automatically). But the same script is not working on our Windows 7 Box. Are there any changes required? I have no experience with VB Scripting. Thanks!
Option Explicit
Const USER = "administrator"
Const PASS = "*********"
Const WSNAME_COMMANDLINE = "C:\support\abc.bat"
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim WSHShell : Set WshShell = CreateObject("WScript.Shell")
Dim WshNetwork : Set WshNetwork = CreateObject("WScript.Network")
Dim oDic : Set oDic = CreateObject("Scripting.Dictionary")
Dim objArgs : Set objArgs = WScript.Arguments
Dim oProcessEnv : Set oProcessEnv = WshShell.Environment("PROCESS")
Dim sPathToRunAs, iReturnCode
sPathToRunAs = oProcessEnv("SystemRoot")&"\System32\runas.exe"
''msgbox sPathtorunas
if Not fso.FileExists(sPathToRunAs) Then : WScript.Quit(1) 'Can't find RunAs
'''msgbox "runas /user:" & USER & " " & CHR(34) & WSNAME_COMMANDLINE & CHR(34)
iReturnCode=WshShell.Run("runas /user:" & USER & " " & CHR(34) & WSNAME_COMMANDLINE & CHR(34), 2, FALSE)
Wscript.Sleep 40 ' Time for window to open.
WshShell.AppActivate(sPathToRunAs)' Activate the Window
Wscript.Sleep 3
WSHShell.SendKeys PASS & "~" ' Send the password
Wscript.Sleep 3
''msgbox "done"
Did you try the script on Windows 7 with UAC (User Account Control) disabled?
You can find a howto on disabling UAC here: http://windows.microsoft.com/en-US/windows-vista/Turn-User-Account-Control-on-or-off
If UAC is the reason for your issue, you may use windows task scheduler to avoid this like it is explained here: http://poundcomment.wordpress.com/2011/03/18/how-to-create-a-whitelist-uac-for-windows-7/