A client wanted to test an inhouse developed application in Azure Remote App. The application required a mapped drive to an Azure File Share. Unless one would want users to manually map a network drive, or for that matter publish a script (or an executable) that would map the Azure File Share it would require a script, preferably a logon-script, to run prior to starting the application.
I did a quick Google search in case Azure RemoteApp had a special feature to do this in a per RemoteApp collection-ish way, but all I could find were “it can’t be done”-posts, and posts stating that you would need a hybrid Azure deployment to use Group Policies. The latter is true, however, running scripts in a Azure RemoteApp VM is possible without hybrid Azure deployments. Actually there is more than one way.
I am going to asume that you have created a VM with the requirements of enabling the VM to be used as an Azure RemoteApp Image, and that the VM has not already been sysprep’ed.
You can run scripts at 6 different times, 4 during startup/logon, and 2 during logoff/shutdown.
Preparing the script-files
So, for the purpose of demonstration, let’s create 6 different scripts – and for simplicity I’ll stick to simple .cmd-files.
- Create a folder named scripts in the root of the system drive, c:\scripts
- Create a subfolder named logs, c:\scripts\logs
- Create 6 different files name like this:
- serverStartup.cmd
- serverRun.cmd
- userLogon.cmd
- userRun.cmd
- userLogoff.cmd
- serverShutdown.cmd
- Insert the following code into the server-scripts:
@Echo off Set LogPath=c:\scripts\logs Set LogFile=%~n0.log Set Log=%LogPath%\%LogFile% Echo == %date% %time% == Begin execution of %~n0 >>%Log% Echo. rem --- Insert commands here: rem --- this sample will simply output the contents of the scripts\log folder to the LogFile dir %LogPath% >>%Log% Echo Error code: %errorlevel% >>%Log% Echo. Echo == %date% %time% == Finished executing %~n0 >>%Log%
- Insert the following code into the user-scripts:
@Echo off Set LogPath=c:\scripts\logs Set LogFile=%~n0-%username%.log Set Log=%LogPath%\%LogFile% Echo == %date% %time% == Begin execution of %~n0 >>%Log% Echo. rem --- Insert commands here: rem --- this sample will simply output the contents of the scripts\log folder to the LogFile dir %LogPath% >>%Log% Echo Error code: %errorlevel% >>%Log% Echo. Echo == %date% %time% == Finished executing %~n0 >>%Log%
Enabling Startup, Logon, Logoff and Shutdown scripts
With hybrid Azure deployments you would use the Group Policy Management Console but because this server is not part of an Active Directory Domain, you will need to use the Local Group Policy Editor.
Open the Group Policy Editor
- Press Windows+R to open the run dialog or Windows+X and click Run.
- Type gpedit.msc and hit Enter
serverStartup.cmd
- To enable the serverStartup.cmd-script go to Computer Configuration > Policies > Windows Settings > Scripts (Startup/Shutdown) and double-click on Startup.
- Click Add and type c:\scripts\serverStartup.cmd and click OK
- Click OK once more to close the Startup window.
serverShutdown.cmd
- To enable the serverShutdown.cmd-script go to Computer Configuration > Policies > Windows Settings > Scripts (Startup/Shutdown) and double-click on Shutdown.
- Click Add and type c:\scripts\serverShutdown.cmd and click OK
- Click OK once more to close the Shutdown window.
userLogon.cmd
- To enable the userLogon.cmd-script go to User Configuration > Policies > Windows Settings > Scripts (Logon/Logoff) and double-click on Logon.
- Click Add and type c:\scripts\userLogon.cmd and click OK
- Click OK once more to close the Shutdown window.
userLogoff.cmd
- To enable the userLogoff.cmd-script go to User Configuration > Policies > Windows Settings > Scripts (Logon/Logoff) and double-click on Logoff.
- Click Add and type c:\scripts\userLogoff.cmd and click OK
- Click OK once more to close the Shutdown window.
That’s it in regards to running scripts using Group Policies, and you can now close the Local Group Policy Editor.
Let’s get the last 2 *Run.cmd-script files up and running.
Enabling the server- and userRun scripts
For this you will need to use the registry editor.
Open the Registry Editor
- Press Windows+R to open the run dialog or Windows+X and click Run.
- Type Regedit and hit Enter
serverRun.cmd
- To enable the serverRun.cmd-script go to HKEY_LOCAL_MACHINE > SOFTWARE > Microsoft > Windows > CurrentVersion > Run
- On the right-hand side right-click, go to New and select String value
- Type serverRun-script as the name
- Double-click on the newly created value and enter c:\scripts\serverRun.cmd into the Value data field.
- Click OK to close the Edit string-dialog.
userRun.cmd
Enabling the userRun.cmd script is not as simple. One could try to do the same as with the serverRun.cmd but doing so in the HKEY_CURRENT_USER hive. But that will not work since each user has his or her own HKCU hive. So we need to add the userRun.cmd to the Default Users hive which is copied to new users at their first logon.
- To enable the userRun.cmd-script go to HKEY_LOCAL_MACHINE
- Click the File menu and select Load Hive
- In the Filename-field enter C:\Users\Default\ntuser.dat and click OK
- In the next dialog enter DefaultUser as the Key name
- Now, go to HKEY_LOCAL_MACHINE > DefaultUser > SOFTWARE > Microsoft > Windows > CurrentVersion > Run
- On the right-hand side right-click, go to New and select String value
- Type userRun-script as the name
- Double-click on the newly created value and enter c:\scripts\userRun.cmd into the Value data field.
- Click OK to close the Edit string-dialog.
- Go back to HKEY_LOCAL_MACHINE and select the added DefaultUser Hive
- Click the File menu and select Unload Hive
That’s all there is to it. Close the Registry Editor.
Finally
Now is the time to reboot the server, logon and check the c:\scripts\logs folder where you should be able to find one log file for each script-file that has run.
You’ll notice that the user-specific log-files have appended the username. In turn, that will be true for all users, and this will enable you to debug any script errors without looking throug hundreds of lines.
Once you have confirmed that all scripts are executed correctly, you should edit them to fit your needs. You will most likely not be needing all 6 scripts, so simply edit them and insert rem at the beginning of every line. This way it will be easy for you to enable and tailor the to your needs anytime in the future. Ofcourse, you can reverse the steps you used to enable the different scripts.
Conclusion
Before running sysprep on this soon-to-be script-enabled Azure RemoteApp Template Image , I would suggest doing an Export of the VM in HyperV, to prevent you from having to do this all over Again.
Feel free to post questions or comments below.