# Solved: help needed with batch file



## night-fury (Nov 27, 2014)

Hi,

I am in need to know if there is any way wherein we can enable/disable windows 7 audio remotely on domain systems. I am in a situation where users are given audio access upon request for a certain period of time and then have to disable it. To do so, everytime we have to go, have the user reboot the system and enable/disable audio from BIOS.

I have also tried removing the users rights from 'services.msc' file and gave permissions only to local administrator and domain admins for accessing services.

However, this did not help. From the user account (who does not have rights on services.ms file), if i right click on the volume icon in taskbar and select playback devices, it prompts me that audio service is not running, do you want to enable it. If I click 'yes' it enables the service 

any help on this wouldb really appreciated..

thanks


----------



## Phantom010 (Mar 9, 2009)

I'll need to think about it a little longer, but something simple would be to create two .reg files. One to delete the Audio service key completely in the registry, and another to bring it back. Using two simple batch files would accelerate the process.


----------



## valis (Sep 24, 2004)

have a read here: group policy is out, but it looks doable yet.


----------



## night-fury (Nov 27, 2014)

Phantom010 said:


> I'll need to think about it a little longer, but something simple would be to create two .reg files. One to delete the Audio service key completely in the registry, and another to bring it back. Using two simple batch files would accelerate the process.


Sure. The idea sounds good. please let me know if you find something. I am also digging on my own. thanks for the reply though.


----------



## night-fury (Nov 27, 2014)

valis said:


> have a read here: group policy is out, but it looks doable yet.


I already tried the commands, before posting the thread here. They do the jab, but again sound can be enabled by starting the service from services.msc.

Also, group policy will be on the entire OU whereas I need to enable/disable service as per need basis. And I can nt make anymore containers in AD for the sole reason of enabling disabling sound to have different group policies.

It further struck me while replying, that even if group policy is applied, it will disable the sound on system startup but then again, can be enabled by enabling windows audio service from service.msc !!!

I appreciate you getting back on this though.


----------



## Phantom010 (Mar 9, 2009)

gsg said:


> Sure. The idea sounds good. please let me know if you find something. I am also digging on my own. thanks for the reply though.


Do you know how to write a .reg file and a batch file? I don't have my Windows 7 computer with me to create them.


----------



## night-fury (Nov 27, 2014)

Phantom010 said:


> Do you know how to write a .reg file and a batch file? I don't have my Windows 7 computer with me to create them.


I will search on internet to write one. i have written a few batch files but i am no pro, those were elementary.

how can i or what should i look for in a reg key relating to the audio drivers?


----------



## Phantom010 (Mar 9, 2009)

The *Windows Audio* service key is located in:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\AudioSrv

http://computerstepbystep.com/windows_audio_service.html


----------



## Phantom010 (Mar 9, 2009)

To create a .reg file, first export the original AudioSrv key where you want to save the file. That will be to enable the service.

Then, create another .reg file with an empty AudioSrv key. That will be to disable the service.

The batch files aren't mandatory, as you can merge the .reg files to the registry by simply double-clicking on them. It all depends on how you intend to apply the controls.


----------



## night-fury (Nov 27, 2014)

here's what I did after your advice:


Exported the audio service key - marked as enable audio
copied the file and changes the values to be blank - marked as disable audio.
copied both files in c drive of the target system. 

PS: i copied both the reg values on a shared drive and executed the below commands giving path to the shared folder/file, but it did not work....any idea?

connected remotely to command prompt of the target system via pstools (psexec).



> Executed following commands to disable audio:
> regedit.exe /s c:\dis_audiosrv.reg
> sc stop "audiosrv"
> sc config "audiosrv" start= disabled
> ...


it worked !  thank you very much.

only one down side, if someone restores the system using system restore, audio gets enabled. but thats ok for now. may be we can use the settings when a new system is created.

i am working on a batch file now, with the hope to create one which simply asks me a system name or IP, thats all. It shud automatically run the commands asking me if audio is to be enabled or disabled.

*I will post it here if I succeed. Any help would be much appreciated.*

-thanks a ton again


----------



## night-fury (Nov 27, 2014)

In regards to the above, here's what I could come up with:



> @echo off
> cd C:\pstools
> set /p input= Enter the hostname or IP of the system you wish to connect to:
> psexec.exe \\%input% cmd
> pause


PS: copy the PSTools in C drive. If you have the folder at a different location, change the path in the code.

when i enter the system name or IP address, it connect me to the command prompt of the target remote system. Then I have to *manually type or copy/paste* the commands to enable or disable audio.

*can anyone help to guide me how can the other commands be set for them to be auto-executed after i enter the system name of IP address.*


----------



## night-fury (Nov 27, 2014)

Hi,

I have a batch file that helps me connect to command prompt of any system in the network, remotely of course. here's the code for that;



> @echo off
> cd C:\pstools
> set /p input= Enter the hostname or IP of the system you wish to connect to:
> psexec.exe \\%input% cmd
> pause


Now there are some commands that I wish to execute after the batch file connects me to the remote command prompt. but even if i mention the commands in the script (after _psexec.exe \\%input% cmd_), those are not executed.

Can anyone please help on this.


----------



## Squashman (Apr 4, 2003)

Put all the commands in another batch file and use the -C option with psexec.


----------



## night-fury (Nov 27, 2014)

Squashman said:


> Put all the commands in another batch file and use the -C option with psexec.


Thanks you for the pointer 
I did try, but something is wrong. can you please give ma syntax for this?

here's what i am doing:
I have 2 registry files, 1 to remove some registry values and 2nd to add those values back to registry as per need.

1) do i need to copy both these reg files onto the remote system or can i access these files from a shared location?
2) do i need t use -c with psexec in the initial set of commands or while calling the commands from CMD of remot system?


----------



## Squashman (Apr 4, 2003)

```
psexec.exe \\remotecomputer Secondbatch.bat
```
Of course you can use a Network Share.


----------



## night-fury (Nov 27, 2014)

Squashman said:


> ```
> psexec.exe \\remotecomputer Secondbatch.bat
> ```
> Of course you can use a Network Share.


*scenario:* i have 3 systems
1) from which I am executing commands
2) sharePC: on which the folder is shared and has the second batch and other files that i need (chose this as this server is always on)
3) target PC

I did use the network share. the folder was given permission for everyone to read and execute. Placed the second batch file in this shared folder.

connected to target computer like u advised, via *psexec.exe \\remotecomputer Secondbatch.bat*. Once i am on CMD of remote PC, the second batch file that was called does not work 

now second batch file is called as;



> psexec.exe \\targetPC net use \\sharePC\share_folder\file.bat "password" /user:sharePC\administrator


----------



## Squashman (Apr 4, 2003)

```
psexec.exe \\targetPC net use \\sharePC\share_folder\file.bat "password" /user:sharePC\administrator
```
I have no idea what you are trying to do but that is definitely not valid syntax for any of those commands.


----------



## night-fury (Nov 27, 2014)

Squashman said:


> ```
> psexec.exe \\targetPC net use \\sharePC\share_folder\file.bat "password" /user:sharePC\administrator
> ```
> I have no idea what you are trying to do but that is definitely not valid syntax for any of those commands.


its as per:
psexec.exe \\targetPC secondbatchfile

Secondbatch file is located on a shared drive (so that its accessible from everywhere) and hence full path to the second batch file is given in the below command.

*net use \\sharePC\share_folder\file.bat "password" /user:sharePC\administrator*

is to connect to a shared drive on a network with the ID/password as provided in the command. Here the path is to the batch file that located in the shared folder.

if I execute this command from any given system, it works fine and connects to the shared drive (or path) as applicable. Only when I am calling this with psexec, it is not working.


----------



## Squashman (Apr 4, 2003)

It seems like you are using one batch file to launch another batch file that launches another batch file. Three batch files. This isn't making any sense to me.


----------



## night-fury (Nov 27, 2014)

Squashman said:


> It seems like you are using one batch file to launch another batch file that launches another batch file. Three batch files. This isn't making any sense to me.


Nope, only 2 as per my initial query.

1 is being used to connect to remote system using psexec while calling the scondbatch file in the process of connecting.

I guess its not clear though my explanations. no worries, i appreciate you getting back to me and for giving the initial idea of calling a second batch file. I will put in some more time in it, and if successful, i will post it here with the exact commands.


----------



## Squashman (Apr 4, 2003)

If you could post all the code you are trying to run and all the files and computers involved I could probably give you a better solution. I think you are somewhat confused on when to use PSEXEC and how to use the NET USE command.


----------



## night-fury (Nov 27, 2014)

Squashman said:


> If you could post all the code you are trying to run and all the files and computers involved I could probably give you a better solution. I think you are somewhat confused on when to use PSEXEC and how to use the NET USE command.


Ok, thank you. Here is what is required:

I need to enable/disable audio on systems in my network as per the user requests. This is a timed access, once the duration for enabled audio is reached, we disable the audio.

To enable or disable we use BIOS. Now whenever this activity is to be carried out (enable or disable), we have to go the user and ask him to save all his unsaved work, reboot the system and enable/disable audio as applicable.

From the help of forum members here, i have created two registry keys which when merged performs the function of enabling and disabling audio by adding/removing reg values. along with the regsistry values, are batch file which merge/demerge those registry values and then run the commands to stop/start audio service.

*On the basis of this scenario, i have batch files as below:*
1) batch files to connect to remote system's command prompt. Code for this is:

```
@echo off
cd C:\pstools
set /p input= Enter the hostname or IP of the system you wish to connect to: 
psexec.exe \\%input% cmd 
pause
```
2) batch file to enable/disable audio. Codes for these are:
Code to enable audio (*EA=Enable audio)

```
regedit.exe /s c:\EA.reg
sc start audiosrv
sc config "audiosrv" start= auto
```
Code to disable audio (*DA=disable audio)

```
regedit.exe /s c:\DA.reg
sc stop "audiosrv"
sc config "audiosrv" start= disabled
```
*Steps that I follow now:*


from my system, I copy the enable (or disable) registry key + batch file to remote system
Use the first batch file to connect to remote system
execute the batch file (from #1 above) from remotely connected CMD.

once I create this, my whole team can use this. this is why I do not want the registry files and enable/disable batch files on my system but on a server that is powered on 24x7. Hence the use of 'NET USE' earlier.

_My idea was to connect to the system remotely and then use the .reg & .bat files from shared folder on a server without having the need to copy them to remote system first._

*Steps that I wish to follow:*


use first batch file to connect to remote system
from there, i should access the EA/DA batch files from a share location

OR anything better as you mentioned. I have limited knowledge of scripting, so this was all i could pull off from browsing and reading a few articles.

All systems are on same LAN.

I have attached all files here. i could not upload 'PS Tools' (required for psexec.exe to run) folder as then the attachment size increases from what is allowed here. I am sure you would have them, but for quick download, in case required, here is the link:
http://technet.microsoft.com/en-in/sysinternals/bb896649.aspx

let me know what you think.

(whatever your reply is, thank you for reading this much )


----------



## foxidrive (Oct 20, 2012)

gsg said:


> I need to enable/disable audio on systems in my network as per the user requests. This is a timed access, once the duration for enabled audio is reached, we disable the audio.


You mention BIOS but I'm not sure that you are using the BIOS - I didn't look too hard though.

Are you selling a product and limiting the time spent listening? Or are you really just responding to a user request to mute the audio - because you could probably just mute the audio output without rebooting if that's the case.


----------



## Squashman (Apr 4, 2003)

I need to talk with some of my old Network Admin buddies. I am pretty sure we muted and disabled the audio controls in a different way.


----------



## valis (Sep 24, 2004)

gsg, I've merged your threads together. I had thought it looked familiar, but didn't catch it until now.

thanks, 

v


----------



## night-fury (Nov 27, 2014)

foxidrive said:


> You mention BIOS but I'm not sure that you are using the BIOS - I didn't look too hard though.
> 
> Are you selling a product and limiting the time spent listening? Or are you really just responding to a user request to mute the audio - because you could probably just mute the audio output without rebooting if that's the case.


I mentioned BIOS just to explain that until I started working on this, we were using BIOS to enable disable audio from there. thats all. thanks.

I am not selling any product, and yes we are responding to requests in way that we enable audio for them for specific time period after approvals from their project mangers.

When the time duration is over, we have to act on our own to remove the access so that they don't just sit around watchin movies on youtub , calling friends on skype calls, etc etc


----------



## night-fury (Nov 27, 2014)

valis said:


> gsg, I've merged your threads together. I had thought it looked familiar, but didn't catch it until now.
> 
> thanks,
> 
> v


Ok Valis, thanks. I thought of continuing in the same thread but then it was a diff thing i was tryin to do, so created separate one. no worries.


----------



## valis (Sep 24, 2004)

well, yes, worries. Please don't create duplicate thread for the same issue, as it can get a bit confusing.


----------



## foxidrive (Oct 20, 2012)

This is a way of including all registry files and code for enabling and disabling into one batch file.

You will note that when echoing text into a file using this particular method, that some characters need to be escaped with the ^ character, 
like these for example "^ ) & | < >"

You'll only see the ) characters escaped below as that's all that are needed, and they become ^)

The other change is that %SystemRoot% is now %SystemRoot:\=\\% which includes the actual path in the entry and doubles the slashes.
I think it's only a descriptive text label but if you really need %SystemRoot% as literal text then change it to %%SystemRoot%%

So if you call this batch file changeaud.bat then you can launch it either of these ways:


```
call changeaud.bat ea
call changeaud.bat da
```
I'm not so clear on how you intend to use this, but this will put the code in one file.
It's untested - so test it well first.


```
@echo off
if /i "%~1"=="ea" goto :enable
if /i "%~1"=="da" goto :disable
goto :EOF

:disable
(
echo.Windows Registry Editor Version 5.00
echo.
echo.[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\AudioSrv]
echo."Start"=
echo."DisplayName"=""
echo."Group"="AudioGroup"
echo."ImagePath"=
echo."Description"=""
echo."ObjectName"=""
echo."ErrorControl"=
echo."Type"=
echo."DependOnService"=
echo."ServiceSidType"=
echo."RequiredPrivileges"=
echo."FailureActions"=
echo.
echo.[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\AudioSrv\Parameters]
echo."ServiceDll"=hex(2^)25,00,53,64,00,6c,00,6c,00,00,\
echo."ServiceDllUnloadOnStop"=dword:00000009
)>"%temp%\da.reg"
regedit.exe /s "%temp%\da.reg"
del "%temp%\da.reg"
sc stop "audiosrv"
sc config "audiosrv" start= disabled
goto :EOF


:enable
(
echo.Windows Registry Editor Version 5.00
echo.
echo.[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\AudioSrv]
echo."Start"=dword:00000002
echo."DisplayName"="Windows Audio"
echo."Group"="AudioGroup"
echo."ImagePath"=hex(2^):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,\
echo.  74,00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,73,\
echo.  00,76,00,63,00,68,00,6f,00,73,00,74,00,2e,00,65,00,78,00,65,00,20,00,2d,00,\
echo.  6b,00,20,00,4c,00,6f,00,63,00,61,00,6c,00,53,00,65,00,72,00,76,00,69,00,63,\
echo.  00,65,00,4e,00,65,00,74,00,77,00,6f,00,72,00,6b,00,52,00,65,00,73,00,74,00,\
echo.  72,00,69,00,63,00,74,00,65,00,64,00,00,00
echo."Description"="@%SystemRoot:\=\\%\\System32\\audiosrv.dll,-201"
echo."ObjectName"="NT AUTHORITY\\LocalService"
echo."ErrorControl"=dword:00000001
echo."Type"=dword:00000020
echo."DependOnService"=hex(7^):41,00,75,00,64,00,69,00,6f,00,45,00,6e,00,64,00,70,00,\
echo.  6f,00,69,00,6e,00,74,00,42,00,75,00,69,00,6c,00,64,00,65,00,72,00,00,00,52,\
echo.  00,70,00,63,00,53,00,73,00,00,00,4d,00,4d,00,43,00,53,00,53,00,00,00,00,00
echo."ServiceSidType"=dword:00000001
echo."RequiredPrivileges"=hex(7^):53,00,65,00,43,00,68,00,61,00,6e,00,67,00,65,00,4e,\
echo.  00,6f,00,74,00,69,00,66,00,79,00,50,00,72,00,69,00,76,00,69,00,6c,00,65,00,\
echo.  67,00,65,00,00,00,53,00,65,00,49,00,6d,00,70,00,65,00,72,00,73,00,6f,00,6e,\
echo.  00,61,00,74,00,65,00,50,00,72,00,69,00,76,00,69,00,6c,00,65,00,67,00,65,00,\
echo.  00,00,53,00,65,00,49,00,6e,00,63,00,72,00,65,00,61,00,73,00,65,00,57,00,6f,\
echo.  00,72,00,6b,00,69,00,6e,00,67,00,53,00,65,00,74,00,50,00,72,00,69,00,76,00,\
echo.  69,00,6c,00,65,00,67,00,65,00,00,00,00,00
echo."FailureActions"=hex:80,51,01,00,00,00,00,00,00,00,00,00,03,00,00,00,14,00,00,\
echo.  00,01,00,00,00,60,ea,00,00,01,00,00,00,c0,d4,01,00,00,00,00,00,00,00,00,00
echo.
echo.[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\AudioSrv\Parameters]
echo."ServiceDll"=hex(2^):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,\
echo.  00,74,00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,\
echo.  41,00,75,00,64,00,69,00,6f,00,73,00,72,00,76,00,2e,00,64,00,6c,00,6c,00,00,\
echo.  00
echo."ServiceDllUnloadOnStop"=dword:00000001
)>"%temp%\ea.reg"
regedit.exe /s "%temp%\ea.reg"
del "%temp%\ea.reg"
sc start audiosrv
sc config "audiosrv" start= auto
goto :EOF
```
*I changed the echo. statements to remove the space I added after each one, in case the REG file format can't handle the leading spaces.*


----------



## night-fury (Nov 27, 2014)

valis said:


> well, yes, worries. Please don't create duplicate thread for the same issue, as it can get a bit confusing.


oki doki, will keep that in mind !


----------



## night-fury (Nov 27, 2014)

Hey, thanks Foxidrive.

As far as i understand, what the code does is: call the enable/disable registry strings as defined and put them in temp folder. Then the *regedit.exe /s* calls it from the temp location and then deletes the file.

For testing, I copied the batch file with above code on a test system and called it to disable audio with *call changeaud.bat da* command.

But i got error(s) as below:


```
C:\>call changeaud.bat da
[SC] OpenService FAILED 5:

Access is denied.

[SC] OpenService FAILED 5:

Access is denied.
```
I logged in with administrator, so no issue with access rights.


----------



## foxidrive (Oct 20, 2012)

It most certainly looks like a permissions issue.

Open a CMD prompt using "run as adminstrator" and then try the batch files.
You may not be aware that being logged in as Administrator doesn't give you complete access, in modern windows.


----------



## night-fury (Nov 27, 2014)

foxidrive said:


> It most certainly looks like a permissions issue.
> 
> Open a CMD prompt using "run as adminstrator" and then try the batch files.
> You may not be aware that being logged in as Administrator doesn't give you complete access, in modern windows.


awesome !!!! it worked... 

a small glitch though, nothing major. when i call da (disable audio), it works like a charm. but when i call ea (enable audio), it gives me some error, but when i call ea the second time, it does what it is supposed to...

I connected to remote system via Psexec (PS Tools), and called the batch file (which i remotely copied onto the system in c drive), again that worked as desired with the above exception.

here's the output:



> C:\Windows\System32>call c:\changeaud.bat da
> 
> SERVICE_NAME: audiosrv
> TYPE : 20 WIN32_SHARE_PROCESS
> ...


In the earlier post, you mentioned that you are not clear on how I am going to use this. That was my initial query too.

Here a brief again, i would appreciate any input/pointer in this regards.


I want to keep this 'changeaud.bat' file on a server in a shared folder which is accessible from everywhere on the network.
then, i can connect to a remote system from my machine (or my team members from their respective systems), and call the *changeaud.bat* file from the shared folder.

the main reason for keeping it on shared folder is, so that i can have a way to set the path in the code which remains same no matter which system i am accessing it from.


----------



## night-fury (Nov 27, 2014)

hey, i identified the glitch.

In the code for enable, that you posted earlier, I changed the order of commands as follows (in red) and it resolved the issue.

here's what it was earlier:

)>"%temp%\ea.reg"
regedit.exe /s "%temp%\ea.reg"
del "%temp%\ea.reg"
sc start audiosrv
sc config "audiosrv" start= auto
here's the output:

changed the order as:

)>"%temp%\ea.reg"
regedit.exe /s "%temp%\ea.reg"
del "%temp%\ea.reg"
sc config "audiosrv" start= auto
sc start audiosrv

only left with the part,in blue, which I mentioned in the last post above.


----------



## foxidrive (Oct 20, 2012)

gsg said:


> hey, i identified the glitch.
> 
> In the code for enable, that you posted earlier, I changed the order of commands as follows (in red) and it resolved the issue.


I'm glad you solved it. The same problem exists in your original files too, as I pasted the same commands into the version I posted.

As for executing it, test this - I don't use psexec myself so it may need a bit of tweaking if it fails - paste any screen messages.


```
@echo off
psexec \\computername -u user -p psswd -c "\\server\share\changeaud.bat" ea
```


----------



## night-fury (Nov 27, 2014)

yes, they were pasted from my post had the same order originally. But when i executed them that way, i didn't encounter the error. May be after clubbing them something changed, anyhow its resolved...

now coming to the last reply of yours....*AWESOME again*.....it worked. As squashman mentioned, i was using a wrong syntax and wrong commands too i suppose. but its all working now !!

i have used the format: *batchfile \\targetPC secondbatchfile*



> @echo off
> cd C:\pstools
> set /p input= Enter the hostname or IP of the system you wish to connect to:
> psexec.exe \\%input% -u user -p psswd -c "\\server\share\changeaud.bat" ea
> pause


it throws an error each time as mentioned below, *but it does its job.*



> While disabling audio
> 
> PsExec v2.11 - Execute processes remotely
> Copyright (C) 2001-2014 Mark Russinovich
> ...


Thank you very very much *Phantom010, Squashman, foxidrive* for all your help !! really appreciate....you guys ROCK 

wish i could get that kool with scripting !!


----------

