Windows DevCenter    
 Published on Windows DevCenter (http://www.windowsdevcenter.com/)
 See this if you're having trouble printing code examples


Some Useful Scripts from Readers

by Mitch Tulloch
05/24/2005

Readers sometimes send me scripts, tips, and suggestions for articles. I appreciate this a lot, and if you have an idea for a WindowsDevCenter article that could benefit other readers, feel free to and I'll be glad to work your idea into a future article.

In this vein, I thought I'd share with you two scripts sent in by readers so you can benefit from their expertise in Windows scripting. These scripts are presented as is, with no warranties, because you may need to customize them for your own Windows environment.

Remotely Enabling Remote Desktop

This first script harks back to an article I wrote called Windows Server Hacks: Remotely Enable Remote Desktop. My idea was this: What if you've deployed a Windows Server 2003 box in a remote location but forgot to enable Remote Desktop on it so you can administer it remotely? In my article I showed a Registry hack you can use to do this, after which you have to reboot the remote machine to have it take effect. But reader Ronni Pedersen, an infrastructure specialist in Denmark, wrote a script that accomplishes this without a reboot! Here is Ronni's email, along with his script:

A comment to your article: "Windows Server Hacks: Remotely Enable Remote Desktop".

FYI: This can be done a whole lot easier...

I've created this script, that'll do the trick...

And we don't need to reboot the server!


'========================================================
'Script name: EnableRemoteDesktop.vbs
'Author: Ronni Pedersen
'Last modified: 14/01-2005
'Version: 1.04
'========================================================
'========================================================

strComputer = InputBox ("Target Computer", 
   "Enable Remote Desktop", default, 100, 100)
strBruger = InputBox ("Username (DOMAIN\Administrator)", 
   "Enable Remote Desktop", default, 100, 100)
strPass = InputBox ("Password", "Enable Remote Desktop", 
   default, 100, 100)

'========================================================

strNameSpace = "root\cimv2" 
strClass = "Win32_TerminalServiceSetting"
Const ENABLE_CONNECTIONS = 1
Const DISABLE_CONNECTIONS = 0

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
Set objSWbemServices = objSWbemLocator.ConnectServer (strComputer, strNameSpace, 
   strBruger, strPass)

Set colClass = objSWbemServices.ExecQuery("Select * from " & strClass)
For Each objTing in colClass
   errAngivelse = objTing.SetAllowTSConnections(ENABLE_CONNECTIONS)
Next

If errAngivelse = 0 THEN
   Wscript.echo "Remote Desktop is now enabled on " + strComputer
Else
   Wscript.echo "Houston, we have a problem...!!!"
End If

'========================================================
' End of Script
'========================================================

If you have questions about this script,

Default Printers and Roaming Profiles

This second script was sent to me by Pierre, a network administrator based in Montreal. Pierre had read two articles I wrote for WindowsNetworking.com (here and here) about managing printers using Group Policy, and wanted to share a script he wrote to set default printers for users with roaming user profiles. Here is Pierre's email in full, along with his script, which you can customize to meet your needs:

Excuse my English, it's not very good. I read a lot in windowsnetworking.com and other leading network administration sites. But I didn't find a way that satisfies me to control default printer in a roaming profile.

I did something on my network:

Example:

The content of this script "Set Default DeskJet HP 895c (Parts Counter).vbs" is:


------------------- Start ------------------------

Set objShell = WScript.CreateObject("WScript.Shell")
Set colSystemEnvVars = objShell.Environment("System")
'colSystemEnvVars("DefaultPrinter") = "The exact 
   Name of the printer set in printer control panel"
colSystemEnvVars("DefaultPrinter") = "DeskJet HP 895c (Parts Counter)"

-------------------- End -------------------------

Inside the default logon script add this command:

------------------- Start ------------------------

'(this code set the default printer to the "DefaultPrinter")

If WshShell.ExpandEnvironmentStrings("%DefaultPrinter%") = "" then

Else

Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.SetDefaultPrinter WshShell.ExpandEnvironmentStrings("%DefaultPrinter%")

End if

------------------- End -------------------

In the company I have 5 printers. So I have 5 global security groups + 5 policies + 1 default logon script. With that I can define 5 different default printers for unlimited computer and users. Tested on w2k & wxp.

Again, if you have any questions about this script,

Mitch Tulloch is the author of Windows 2000 Administration in a Nutshell, Windows Server 2003 in a Nutshell, and Windows Server Hacks.

Windows Server Hacks

Related Reading

Windows Server Hacks
100 Industrial-Strength Tips & Tools
By Mitch Tulloch

Return to the Windows DevCenter.

Copyright © 2009 O'Reilly Media, Inc.