Is it possible to write a Windows 10 script opening speaker settings? - windows

I want to write a windows 10 batch script to change my microphone's output volume, as Skype apparently is not able to provide me with consistent settings. Can you point me in the right direction on how to achieve this?

To change the volume You can build 2 vbs files :
soundown.vbs
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "{" & chr(174) & " 5}"
soundup.vbs
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "{" & chr(175) & " 20}"
and then call them from your .batfile :
#echo off
cscript.exe /nologo soundown.vbs
or the same with soundup.vbs
EDIT:
You can modify the steps it will modify the volume by modifing the value 5 and 20 in the VBSfiles

Related

schedule vbscript without task scheduler

I have a VBScript, Abc.vbs, which contains:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "D:\Abc.bat" & Chr(34), 0
Set WshShell = Nothing
How do I schedule Abc.vbs to run without Windows Task Scheduler ?
'Put this code above your own code in the VBS file.
Do While(cStr(Time) <> "22:00:00") 'Please check the date/time format on your computer and make changes accordingly. This code schedules the script to 10 PM
Wscript.Sleep = 100
Loop
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "D:\Abc.bat" & Chr(34), 0
Set WshShell = Nothing
Now double click this VBS and leave it as it is.
It depends on your environment, operating system, when you want it to run and what tools you have available to you.
In windows you could use registry keys or group policy to run your script at logon which can then run in a loop and then at set intervals run your batch file.
You could install an executable as windows service, which basically does what the above but uses an executable.
If you have access to SQL service it has the ability to schedule jobs as well.
There are probably a few more ways but again they depend on your specific use case.

Run batch file with parameters

I would like to run a batch file with some extra character at the end.
If I write it directly to cmd it is working, like change directory to "c:\Users\Public\Uploader\" and write the following: start.bat "cmd:file.import c:\Users\tom\Desktop\a.xml"
I do not know how to write it in VBScript, because the following script is not working:
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.CurrentDirectory="C:\Users\Public\Uploader\"
WshShell.Run "start.bat" & "cmd:file.import C:\Users\tom\Desktop\a.xml"
The a.xml should dynamically change based on the last modified file in the folder.
Thank you for your help!
If you're trying to duplicate what you're doing in the command prompt, you need to add a space between the batch file name and its arguments. You also need to add the quotes.
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.CurrentDirectory="C:\Users\Public\Uploader\"
WshShell.Run "start.bat " & chr(34) & "cmd:file.import C:\Users\tom\Desktop\a.xml" & chr(34)

How to delay in VB Script?

How can I delay a Vb Script?
The following codes did not work for me:
wscript.sleep 1000
Delay 10
Sub Delay( seconds )
Dim wshShell, strCmd
Set wshShell = CreateObject( "Wscript.Shell" )
strCmd = "%COMSPEC% /C (PING -n " & ( seconds + 1 ) & " 127.0.0.1 >NUL 2>&1 || PING -n " & seconds & " ::1 >NUL 2>&1)"
wshShell.Run strCmd, 0, 1
Set wshShell = Nothing
End Sub
dim oshell
set oshell = CreateObject("Wscript.Shell")
obshell.run "%windir%\system32\rundll32.exe kernel32.dll Sleep 5000"
The first statement, WScript.Sleep 1000, does work. Your error must be somewhere else.
Proof
Create a file test.vbs on your desktop:
WScript.Echo Time()
WScript.Sleep 2000
WScript.Echo Time()
Run as follows:
C:\Users\...\Desktop>cscript test.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
11:31:34
11:31:36
Note the two second difference. (If this exact script produces a different outcome on your PC, please say so in the comments and accept my apology.)
You cannot use the WScript object in VB script custom actions. The WScript object is supplied by the script environment when you run it in Windows Script Host, and is not in the MSI environment. That means there is no way to do a delay, so maybe you could describe the problem you're having that the delay might solve.

WshShell.AppActivate doesn't seem to work in simple vbs script

total vbs scripting newb here. I'm trying to automate closing a certain open window, namely a program called HostsMan. This is on Windows 8.1 Pro 64 bit, and this is what my script currently looks like:
Set WshShell = CreateObject("WScript.Shell")
WshShell.AppActivate "HostsMan"
WshShell.SendKeys "%{F4}"
The second line doesn't seem to work. I know line 3 works because it activates the Windows shutdown menu. Is there something I'm missing?
Update/more info: Manually entering alt-F4 does close it, so I know this should work. I also tested this script with other open windows and they close just fine. Additionally, HostsMan is opened with Admin privileges, so I tried running the script as a task set with highest privileges to see if that would do it, and still no go. But that does work with other open windows running with Admin privileges. Frustrating!
I've tried it, too, and couldn't get it to work. There must be something about the window class, perhaps, where AppActivate doesn't see it as a top-level window?
In any event, AppActivate also lets you pass the process ID instead of the window title. When I installed HostsMan, the process name was hm.exe, so I'll use that in my example below.
Set Processes = GetObject("winmgmts:").InstancesOf("Win32_Process")
For Each Process In Processes
If StrComp(Process.Name, "hm.exe", vbTextCompare) = 0 Then
' Activate the window using its process ID...
With CreateObject("WScript.Shell")
.AppActivate Process.ProcessId
.SendKeys "%{F4}"
End With
' We found our process. No more iteration required...
Exit For
End If
Next
Alternative solution using WMIService (no loop through all processes required):
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * from Win32_Process WHERE Name = '" & ProcessName & "'")
If colItems.Count = 0 Then
WScript.Echo "Process not found"
Wscript.Quit
End If
For Each objProcess in colItems
WshShell.AppActivate(objProcess.ProcessId)
Exit For
Next
The key here is to put a small pause after the 'run' command but before the 'appactivate' command so the application shows up in the process list.
WshShell.Run "calc"
WScript.Sleep 100
WshShell.AppActivate "Calculator"
To solve the problem of AppActivate you have to use loop and if condition statement to check if the active windows is the target windows or not because sometime you deselect or the system deselect the target windows before execute the sendkeys directly so you got error.
I create this tight strict way
Set WshShell = CreateObject("WScript.Shell")
for i=0 to 300 ' this loop will continue about 30 sec if this not enough increase this number
Rtn=WshShell.AppActivate("HostsMan") ' HostMan have to be the windows title of application or its process ID
If Rtn = True Then
WshShell.SendKeys "%{F4}" ' send key to click ALT+F4 to close
wscript.sleep 100 ' stop execute next line until finish close app
Rtn=WshShell.AppActivate("HostsMan")
If Rtn=False Then ' using nested If to sure of selected windows is false because its close
Exit For ' exit for loop directly and execute what after for next
End If
End If
wscript.sleep 100
Next
Dim sh : Set sh =CreateObject("Wscript.Shell")
sh.Exec "calc.exe"
Wscript.Sleep 1000
sh.Exec "taskkill /f /FI ""WINDOWTITLE eq ""calc*"""
OR
sh.Run "taskkill /f /im calc.exe",0,False

VBScript saying "No Application is associated with the specified file for this operation"?

I'm writing a simple VBScript to write to a custom windows event log using eventcreate.
FOR I = 0 to 5
Set WshShell = WScript.CreateObject("WScript.Shell")
strCommand = "eventcreate /l Application /t Information /so Test-Log /id 66 /d TEST"
WshShell.Run strCommand
Next
However, whenever I try to run it through the command prompt, I get the following message:
C:\testlog.vbs(6, 5) (null): No application is associated with the specified file for this operation.
From what I can tell, I'm doing exactly what the online examples are telling me to do, I just can't seem to replicate it. What am I doing wrong?
I ran your script and it worked as expected on a win 7 laptop. Ensure you are running the script with admin rights. I changed the script a little, I moved the Set statement out for the For...Next loop. There is no need to continue to set the WshShell object on each loop, setting it once for the entire script is fine in this instance.
Dim WshShell, strCommand
Set WshShell = WScript.CreateObject("WScript.Shell")
For I = 0 to 5
strCommand = "eventcreate /l Application /t Information /so Test-Log /id 66 /d TEST"
WshShell.Run strCommand
Next

Resources