Wednesday, September 5, 2012

It's all about the Script.....VBScript that is...

Hey Everyone,

       Sorry about the late post this week. To be honest, starting on Friday last week my work has thrown a huge curve ball at me and I've been plugging away at it. Basically, they have been planning on upgrading some business software that is a hosted web based solution, and regardless of the questions we asked about preparations and client based changes, we were assured that everything would transition smoothly. We'll the go live date was Friday of last week and the bombshell went off that the links used for majority of our users had to be changed. Just to give you and idea of the scale, we're talking roughly a little over 300 users and not only do they have shortcuts in their favorites, they also have desktop icons. Now some of you may be thinking, no big deal, use group policy, do this, do that.. and forgive me for being short but, yes that would work and in part I did but, this isn't my forte and was a new challenge for me. So, introductions and background out of the way, I wanted to share my experience and what I was able to discover after researching multiple websites for solutions.

       Basically, I had 3 goals: my first goal was to remove the old website shortcuts on Windows XP and Windows 7 through all the users profiles to eliminate confusing and false error reports to the help desk. Secondly, I needed to update the link that was applied through group policy across our domain to point to the new link, and finally, I had to create a new shortcut for every user pointing to the correct address.

      So, the second goal was the easiest to address and was knocked out before the end of business on Friday. I hopped onto the group policy manager and edited the link that was already applied through group policy. The other two goals were the more difficult ones. Remember to keep the scale of deployment in mind, this is 300 users that need this link, and that have old links. My approach was to utilize a process that I was familiar with and which is actually my tech tool of the week. I utilized PDQ Deploy by AdminArsenal to distribute a batch file that creates the shortcut for me. Now covering PDQ Deploy is a bit out of the scope of this blog today but, I plan to cover it in a video tutorial.

Here's the batch file contents that was deployed to all of the desktops:
-------------------------------------------------------------------------------------------------
@echo off
REM Providing network location
:_Create
IF NOT EXIST "X:" (
  goto :_X32
) else (
NET USE X: /Delete
goto :_Create
)
:_X32
NET USE X: \\Server\Shared\Drive\Folder
IF EXIST "C:\Documents and Settings" (xcopy /Y "X:\Internet Shortcut Needed.url" "C:\Documents and settings\All Users\Desktop\*.*") ELSE (goto :_X64)
IF EXIST "C:\Documents and Settings\All Users\Desktop\Internet Shortcut Needed.url" DEL "C:\Documents and Settings\All Users\Desktop\Internet Shortcut Needed.url"
goto :_eof
:_X64
IF Exist "C:\Users" (xcopy /Y "X:\Internet Shortcut Needed.url" "C:\Users\Public\Public Desktop\*.*") ELSE (goto :_X32)
IF Exist "C:\Users\Public\Public Desktop\Internet Shortcut Needed.url" DEL "C:\Users\Public\Public Desktop\Internet Shortcut Needed.url"
goto :_eof

:_eof
REM Remove Network Drive
NET USE X: /Delete
-----------------------------------------------------------------------------------------------------------------------

In case you're not familiar with scripts let me walk you through this. The first section checks if a drive is mapped to the drive letter 'X:', if so it disconnects the drive to assure it is available. If the drive doesn't exist, it jumps to :_X32. The first thing we do is map a drive to the network share that I stored the Internet shortcut, and assign the drive letter 'X'. It then checks to verify if this system is a Windows XP system by checking if 'C:\Documents and Settings' exists, if it does, it copies the Internet shortcut to the All Users profile on the desktop, which assures every user on the system now has the new link on their desktop. If it doesn't exist, it skips to :_X64. Continuing the process that 'C:\Documents and Settings' exists, it then checks the all users profile to see if the old icon exists on the desktop, if so it deletes it. The process is then complete and calls :_eof to cleanup and exit. :_eof disconnects the mapped drive since it is no longer needed and the user may require the X: drive for other functions. The :_X64 functions in the exact same manner however, it checks to verify it is a Windows 7 system and checks a different folder structure.

I don't know if this was the easiest way to deliver the shortcut but it worked for me. I had this process deployed by early morning Tuesday. Here was now the most difficult task, removing the old shortcuts in every users profile. Here's how I accomplished that task:
-----------------------------------------------------------------------------------
Dim fso, f, f1,strPath1,strComputer
Set fso = CreateObject("Scripting.FileSystemObject")
Set network = WScript.CreateObject("WScript.Network")
strComputer = network.ComputerName
Set strPath1 = fso.GetFolder("\\" & network.ComputerName & "\c$\Documents and Settings")
'WScript.Echo "strPath1: " & strPath1
Set f = strPath1.subFolders

For Each f1 in f
If fso.FileExists(f1 & "\Desktop\Internet Shortcut.url") Then
  fso.DeleteFile (f1 & "\Desktop\Internet Shortcut.url")
End IF
Next
For Each f1 in f
If fso.FileExists(f1 & "\Desktop\Internet Shortcut-2.url") Then
  fso.DeleteFile (f1 & "\Desktop\Internet Shortcut-2.url")
End If
Next
For Each f1 in f
If fso.FileExists(f1 & "\Desktop\Internet Shortcut-3.url") Then
  fso.DeleteFile (f1 & "\Desktop\Internet Shortcut-3.url")
End If
NEXT
For Each f1 in f
If fso.FileExists(f1 & "\Desktop\Internet Shortcut-4.url") Then
  fso.DeleteFile (f1 & "\Desktop\Internet Shortcut-4.url")
End If
NEXT
For Each f1 in f
If fso.FileExists(f1 & "\Desktop\Internet Shortcut-5.url") Then
  fso.DeleteFile (f1 & "\Desktop\Internet Shortcut-5.url")
End If
NEXT
WScript.Quit(0)
---------------------------------------------------------------------------------------------

I used active directory and deployed this script to all of my Windows XP systems. This script starts by defining all of the variables needed in the script. It then assigns the computer name to strComputer, and sets the path using the computer name and path to the location of user profiles. The 'For Each' command then runs through each profile deleting the Internet shortcut specified. I have multiple 'For Each' commands to address multiple variations that the old links could be titled as.

The Windows 7 script is the same way just with different paths. Here is a sample to compare the differences:
------------------------------------------------------------------
Dim fso, f, f1,strPath, strComputer
Set fso = CreateObject("Scripting.FileSystemObject")
Set network = WScript.CreateObject("WScript.Network")
strComputer = network.ComputerName
Set strPath = fso.GetFolder("\\" & strComputer & "\c$\Users")
'WScript.Echo "strPath: " & strPath
Set f= strPath.subFolders
For Each f1 in f
If fso.FileExists(f1 & "\Desktop\Internet Shortcut.url") Then
  fso.DeleteFile (f1 & "\Desktop\Internet Shortcut.URL")
End IF
NEXT
-----------------------------------------------------------------------

Well that's it, I don't know if that was the best solution but, that's what worked for me. What would you have done differently to solve this problem? Be Blessed!

No comments:

Post a Comment

You've heard my opinion, let's hear yours, I'm listening. :-)