<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>devtrends.com &#187; VBScript</title>
	<atom:link href="http://www.devtrends.com/index.php/category/software-development/vbscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.devtrends.com</link>
	<description>developing trends in information technology</description>
	<lastBuildDate>Tue, 06 Sep 2011 19:27:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Centralized Desktop and Favorites with a Centralized My Documents?</title>
		<link>http://www.devtrends.com/index.php/centralized-desktop-and-favorites-with-a-centralized-my-documents/</link>
		<comments>http://www.devtrends.com/index.php/centralized-desktop-and-favorites-with-a-centralized-my-documents/#comments</comments>
		<pubDate>Sun, 01 Aug 2010 02:25:37 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[VBScript]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Windows Server 2003]]></category>
		<category><![CDATA[Windows XP]]></category>
		<category><![CDATA[centralized desktop]]></category>
		<category><![CDATA[centralized favorites]]></category>
		<category><![CDATA[migrate desktop]]></category>
		<category><![CDATA[migrate favorites]]></category>

		<guid isPermaLink="false">http://www.devtrends.com/?p=592</guid>
		<description><![CDATA[What about Desktop and Favorites migration? Wouldn’t it be nice if there was a GPO that made the migration of your user’s Desktop and Favorites as easy as the migration...]]></description>
			<content:encoded><![CDATA[<p>What about Desktop and Favorites migration? Wouldn’t it be nice if there was a GPO that made the migration of your user’s Desktop and Favorites as easy as the migration of their My Documents? I think it would be nice. Since there isn’t, I created a script that would do the migration. This script should be run following a successful My Documents migration as the goal would be to put the files in the same location as the My Documents files.</p>
<p>When I first started the project, I was hoping that my script would run prior to showing the desktop. This is important because if it runs while the user has access to the desktop, that user may start using files that would prevent the script from running correctly (file locks). I had no luck; if anyone has an idea on how to do this, let me know. Otherwise, I created a .NET application that covers the entire screen on the primary monitor (if more than 1 monitor, then the second one is still visible).</p>
<p><strong>BlankScreen</strong></p>
<p>The BlankScreen application is written in VisualStudio .NET 2008 using .NET Framework 2.0 if I remember correctly. There are some changes you may want to make on this application to tailor it for your environment. The first change would be to put your logo on the application form. The second change, and really the more important change, is to change the ProcessPath variable to point to the UNC path of where you plan on putting the desktop.vbs file. This application just runs the VBScript.</p>
<p><a href="http://www.devtrends.com/wp-content/uploads/2010/07/BlankScreen.jpg"><img class="alignnone size-medium wp-image-593" title="BlankScreen" src="http://www.devtrends.com/wp-content/uploads/2010/07/BlankScreen-300x236.jpg" alt="" width="300" height="236" /></a></p>
<p>So you might be thinking, why would you have an application that is capable of much more advanced programming run a VBScript? Well, I wrote the script first and didn’t feel like porting it to VB.NET. If you feel like spending the time, go for it.</p>
<p><strong>desktop.vbs</strong></p>
<p>Enough chatter about the other stuff, on to the real product of this blog post, the VBScript.</p>
<p>The VBScript has four basic functions that it completes one after the other based on the success of the previous function. Step 1 is to get the username of the logged in user. Step 2 is to create the folders in the user’s My Documents location and copy the files into that new folders (nothing is moved). Step 3 is to update the registry for the Desktop and Favorites locations to the newly created folders. Step 4 renames the old folders in the C:\Documents and Settings\[user]\ folder to DesktopOLD and FavoritesOLD. I feared losing user files, so I did not make the script perform anything that couldn’t be reversed.</p>
<p>Just as you had to change a string variable in the BlankScreen application for your specific server, you also have to change the location of your user’s folder on the network. In my case it was \\fileserver\users\.</p>
<p><a href="http://www.devtrends.com/wp-content/uploads/2010/07/desktop_vbs.jpg"><img class="alignnone size-medium wp-image-594" title="desktop_vbs" src="http://www.devtrends.com/wp-content/uploads/2010/07/desktop_vbs-300x257.jpg" alt="" width="300" height="257" /></a></p>
<p>The migration will change the registry keys:</p>
<pre>HKCR\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders, Favorites
HKCR\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders, Desktop
HKCR\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders, Favorites
HKCR\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders, Desktop</pre>
<p>The new locations will look something like this:</p>
<p>Desktop: \\[server]\[share]\[username]\Desktop-[username]<br />
Favorites: \\[server]\[share]\[username]\Favorites-[username]<br />
e.g. \\fileserver\users\aaron\Desktop-aaron\ would be my desktop folder.</p>
<p><strong>Conclusion</strong></p>
<p>Implementing this solution is fairly easy. First modify the VB.NET application and compile it. Place the .exe file in a shared location that your login script for users has permissions. Place the VBScript file in a similar location, as you specified in the VB.NET application, and modify the serverpath variable and email variables. Test with your user account in your login script batch file using something similar to below:</p>
<pre>if %USERNAME% == yourusername (
 rem lets make sure the user has a my documents folder prior to trying to migrate their desktop
 if exist "\\[server]\Users\%USERNAME%" (
  if exist "C:\Documents and Settings\%USERNAME%\Desktop" (
   rem lets make sure they have the 2.0 Framework installed.
   if exist "C:\Windows\Microsoft.Net\Framework\v2.0*" (
    start "Desktop Migration" "\\[server]\[share]\blankscreen.exe"
   )
  )
 )
)</pre>
<p>As a disclaimer, the application AND script are provided without any warranty and is only for educational / informational purposes. If you choose to use it in your environment, production or not, the outcome is your responsibility.</p>
<p><strong>desktop.vbs Code</strong></p>
<pre>On Error Resume Next

'alert user of what is happening...
MsgBox "Please click OK to begin the backup process for your Desktop and Internet Explorer Favorites.  This process could take a few minutes, please be patient and Do NOT Cancel anything, otherwise data could be lost!" &amp; vbCrLf &amp; vbCrLf &amp; "If you have Administrator privileges, your computer will restart after all of your files have been copied!"

'get the current user
user = getUser()
'set the server location, the following \ is required!
serverpath = "\\[server]\Users\"
'if you have a helpdesk solution with email capability:
useemailnotify = 0
smtpfrom = "email@domain.com"
smtpto = "email@domain.com"
smtpserver = "smtp.domain.com"

'create folders
m_cf = createFolderscopyFiles(user)
If m_cf &lt;&gt; 1 Then
	'update the registry and reboot
	m_ur = updateRegistry(user)
	If m_ur &lt;&gt; 1 Then
		'rename the old folders so it does not use them anymore!
		m_rof = renameOldFolders(user)
		'reboot
		Set WSHShell = WScript.CreateObject("WScript.Shell")
		WshShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 0"
	End If
End If

Function getUser()
	'get the current user environment variable.
	Set oShell = CreateObject( "WScript.Shell" )
	user = oShell.ExpandEnvironmentStrings("%UserName%")

	If Err.Number &lt;&gt; 0 Then
		'we have failure!
		getUser = 1
		Err.Clear
	Else
		getUser = user
	End If
End Function

Function createFolderscopyFiles(user)
	Set objFSO = CreateObject("Scripting.FileSystemObject")

	'lets only do the folder create and file copy if that folder doesn't already exist. Otherwise, its just gonna set the registry and reboot.
	If Not objFSO.FolderExists(serverpath &amp; user &amp; "\Desktop-" &amp; user) Then
		'Create the new Desktop and the Favorites folder.
		objFSO.CreateFolder(serverpath &amp; user &amp; "\Desktop-" &amp; user)
		objFSO.CreateFolder(serverpath &amp; user &amp; "\Favorites-" &amp; user)

		Set oSHApp = CreateObject("Shell.Application")

		'Copy the Desktop files
		Set fromDFolder = oSHApp.Namespace("C:\Documents and Settings\" &amp; user &amp; "\Desktop")
		Set toDFolder = oSHApp.Namespace(serverpath &amp; user &amp; "\Desktop-" &amp; user)
		toDFolder.CopyHere fromDFolder.Items, 16+512

		'Copy the Favorites files
		Set fromFFolder = oSHApp.Namespace("C:\Documents and Settings\" &amp; user &amp; "\Favorites")
		Set toFFolder = oSHApp.Namespace(serverpath &amp; user &amp; "\Favorites-" &amp; user)
		toFFolder.CopyHere fromFFolder.Items, 16+512
	End If

	If Err.Number &lt;&gt; 0 Then
		'we have failure!
		SendEmail user,"failed at createFolderscopyFiles",Err.Description
		createFolderscopyFiles = 1
		Err.Clear
	Else
		createFolderscopyFiles = 0
	End If
End Function

Function updateRegistry(user)
	'lets work on the registry now
	Const HKEY_CURRENT_USER = &amp;H80000001
	Const HKEY_LOCAL_MACHINE = &amp;H80000002

	'update the registry for Desktop and Favorites (User Shell Folders)
	'set Favorites
	Set oFReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &amp; "." &amp; "\root\default:StdRegProv")
	strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
	strValueName = "Favorites"
	strValue = serverpath &amp; user &amp; "\Favorites-" &amp; user
	oFReg.SetExpandedStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
	'set Desktop
	Set oDReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &amp; "." &amp; "\root\default:StdRegProv")
	strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
	strValueName = "Desktop"
	strValue = serverpath &amp; user &amp; "\Desktop-" &amp; user
	oDReg.SetExpandedStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

	'update the registry for Desktop and Favorites (Shell Folders)
	'set Favorites
	Set otFReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &amp; "." &amp; "\root\default:StdRegProv")
	strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
	strValueName = "Favorites"
	strValue = serverpath &amp; user &amp; "\Favorites-" &amp; user
	otFReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
	'set Desktop
	Set otDReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &amp; "." &amp; "\root\default:StdRegProv")
	strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
	strValueName = "Desktop"
	strValue = serverpath &amp; user &amp; "\Desktop-" &amp; user
	otDReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

	If Err.Number &lt;&gt; 0 Then
		'we have failure!
		SendEmail user,"failed at updating registry",Err.Description
		updateRegistry = 1
		Err.Clear
	Else
		updateRegistry = 0
	End If
End Function

Function renameOldFolders(user)
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	objFSO.MoveFolder "C:\Documents and Settings\" &amp; user &amp; "\Desktop", "C:\Documents and Settings\" &amp; user &amp; "\DesktopOLD"
	objFSO.MoveFolder "C:\Documents and Settings\" &amp; user &amp; "\Favorites", "C:\Documents and Settings\" &amp; user &amp; "\FavoritesOLD"

	If Err.Number &lt;&gt; 0 Then
		'we have failure!
		SendEmail user,"failed at renaming old folders",Err.Description
		renameOldFolders = 1
		Err.Clear
	Else
		renameOldFolders = 0
	End If
End Function

Sub SendEmail(user,contnt,contntB)
	'if the setting is to send an email, then do it, otherwise...nada.
	If useemailnotify = 1 Then
		'send an email!!
		Set objEmail = CreateObject("CDO.Message")
		objEmail.From = smtpfrom
		objEmail.To = smtpto
		objEmail.Subject = "Desktop and Favorites Migration Issue for " &amp; user
		objEmail.Textbody = "User had a Desktop files migration error." &amp; vbCrLf &amp; vbCrLf &amp; contnt &amp; vbCrLf &amp; vbCrLf &amp; contntB
		objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
		objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpserver
		objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
		objEmail.Configuration.Fields.Update
		objEmail.Send

		If Err.Number &lt;&gt; 0 Then
			'we have failure!
			MsgBox "Your Desktop migration didn't go very well."
			Err.Clear
		Else
			'YAY!
		End If
	End If
End Sub
</pre>
<p><strong>Downloads</strong></p>
<p><a href="http://www.devtrends.com/downloads/BlankScreen.zip" target="_self">BlankScreen.zip</a> &#8211; VB.NET application</p>
<p><a href="http://www.devtrends.com/downloads/desktop.zip" target="_self">desktop.zip</a> &#8211; VBScript</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devtrends.com/index.php/centralized-desktop-and-favorites-with-a-centralized-my-documents/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add Printers Using VBScript on an Intranet Site</title>
		<link>http://www.devtrends.com/index.php/add-printers-using-vbscript-on-an-intranet-site/</link>
		<comments>http://www.devtrends.com/index.php/add-printers-using-vbscript-on-an-intranet-site/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 20:11:12 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[WScript]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Windows Script]]></category>

		<guid isPermaLink="false">http://www.devtrends.com/?p=348</guid>
		<description><![CDATA[Recently I had been tasked to create an Intranet site that simplified the process of adding printers to workstations. In addition, the process had to be completely in the user’s...]]></description>
			<content:encoded><![CDATA[<p>Recently I had been tasked to create an Intranet site that simplified the process of adding printers to workstations. In addition, the process had to be completely in the user’s control, eliminating the need for IT to be involved with workstation printer drivers. If a user moved or a printer went down, the user would be responsible for adding a new network printer to his or her workstation. Below is a variation of the script I used&#8230;</p>
<p><strong>VBScript</strong></p>
<p>Adding VBScript to a web page is as easy as adding JavaScript; you will use the &lt;script&gt; tag with a different language type defined. The only additional requirement for getting VBScript to run properly is to ensure that your Intranet site is set as a Trusted Site in Internet Explorer &#8212; for obvious reasons this is required.</p>
<pre style="white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;">&lt;SCRIPT&gt;
&lt;!--
      Function addPrinter(mynum)
          Dim mypath, myname

          On Error Resume Next

          Select Case (mynum)
               Case "1"
                   mypath = <a href="file://\\PRINTSERVER\PRINTER1">\\PRINTSERVER\PRINTER1</a>
                   myname = "PRINTER1"
               Case "2"
                   mypath = <a href="file://\\PRINTSERVER\PRINTER2">\\PRINTSERVER\PRINTER2</a>
                   myname = "PRINTER2"
          End Select

          Dim WSHNet
          Set WSHNet = CreateObject("WScript.Network")
          WSHNet.AddWindowsPrinterConnection mypath

          strMbox = MsgBox("Would you like to make " &amp; myname &amp; " your default printer?",3,"Printer")
          If strMbox = 6 Then
              WSHNet.SetDefaultPrinter mypath
          Else
              'nothing to do...
          End If

          If Err &lt;&gt; 0 Then
                MsgBox "Please contact the IT HelpDesk."
                Err.Clear
                Exit Function
          End If

          MsgBox "The printer, " &amp; myname &amp; ", has been installed on your workstation!"

      End Function

//--&gt;
&lt;/SCRIPT&gt;</pre>
<p><strong>Broken Down</strong></p>
<p>For the people that are more than interested of just plagiarizing my code, I’ll explain the function in a little bit of detail. First, the function is called as you would call a JavaScript function, such as by using an onclick=”” method. Second, pass a variable to the function to define which printer should be installed.</p>
<p>In my case, I had drawn out simple floor plans of our facilities and placed printer icons in &lt;div&gt; tags throughout the map. When the user clicked on the icon, the VBScript function is initiated and the printer is installed. For those that are unfamiliar with the onclick method, you can apply it to many HTML objects, such as &lt;a href&gt; tag or &lt;img&gt; or &lt;div&gt;. If you apply onclick to a div statement, you will probably want to assign the style “cursor:hand;” to change the cursor to a hand pointer, letting the user know this is a clickable area.</p>
<p>The code uses a Windows Scripting namespace called WScript.Network. A defined method in WScript.Network, AddWindowsPrinterConnection, allows the scripter to easily add a networked printer to the workstation. In my script, the only variable passed to this method is the UNC path of the network printer. Shortly following the printer add method, the user is asked if he or she would like to make it the default printer. If true, then a defined method is WScript.Network, SetDefaultPrinter, is called with one variable passed to it, the UNC path to the network printer. On any type of error a generic message is display to contact the IT HelpDesk.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devtrends.com/index.php/add-printers-using-vbscript-on-an-intranet-site/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

