PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 89 - Simple but Elegant SendKeys

Contents for Ezine 89 - Simple but Elegant SendKeys

This week's secret

This week's script illustrates the best of learning about VBScript on the internet.  Jeremy H. wrote asking me for help with a scripting problem.  His scenario was that he wanted a VBScript which refreshed the desktop.  He researched a script which is widely available on the internet (not on my site.)  Unfortunately, the script did not do what Jeremy wanted.  I gave the script a quick look, but could not fix it.  Then Jeremy himself, came up with an elegant solution that we are pleased to share with you.


If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


Scenario for SendKeys

Jeremy wanted a script to refresh the desktop.  The scenario was that his users logged on just as a group policy finished delivering short cuts to the desktop.  What confused the users was that the icons only appeared after a refresh, such as pressing F5.  Jeremy wanted a VBScript to tag on at the end of a logon script, which would refresh the desktop automatically.

Version 1 - Problem Won't Refresh the Desktop.

 


'Refresh the desktop
set oShell= createobject("shell.application")
set oDesktop = oShell.Namespace(0)
oDesktop.self.invokeVerb "R&efresh"

 

 

Guy Learning Points

Note 1: I liked and admired 'self.invokeVerb', unfortunately in Jeremy's case, this method did not refresh the desktop.  Nevertheless, I will add self.invokeVerb to my notebook of useful commands.

ˆ

Version 2 - SendKeys Refreshes your Desktop

 


' Desktop.vbs - to Refresh Icons on the Desktop
' Example VBScript to re-activate the Desktop Shell
' Author Jeremy H. & Guy Thomas http://computerperformance.co.uk/
' Version 2.1 - October 2005
' -------------------------------------------------------------'
Option Explicit
Dim WSHShell, strDesktop
Set WSHShell = WScript.CreateObject("WScript.Shell")
strDesktop = WSHShell.SpecialFolders("Desktop")
WSHShell.AppActivate strDesktop
WSHShell.SendKeys "{F5}"
WScript.Quit

' End of VBScript to refresh the desktop
 

 

Guy Learning Points

Note 1: Where other scripts create a Network or an Active Directory object, so this script creates a shell object.  Jeremy declares his object and names it : WSHShell.  In other scripts I call the same object objShell, it just goes to show that there numerous ways of creating and naming VBScript objects.

Note 2: strDesktop = WSHShell.SpecialFolders("Desktop"), here is the clever way that the script gets a handle on the desktop.  In particular, note the central role of the property .SpecialFolders.

Note 3: WSHShell.SendKeys "{F5}".  Pay close attention to the syntax for passing the famous function key five command to SendKeys.

Version 3 - SendKeys with a Few Extras

In this version I added primitive error correcting code, combined with a confirmation message.  I also introduce a delay with WScript.Sleep.

 

 


' Desktop.vbs - to Refresh Icons on the Desktop
' Example VBScript with Sleep and err.number
' Author Jeremy H. & Guy Thomas http://computerperformance.co.uk/
' Version 2.3 - October 2005
' -------------------------------------------------------------'
Option Explicit
Dim WSHShell, strDesktop
Set WSHShell = WScript.CreateObject("WScript.Shell")
strDesktop = WSHShell.SpecialFolders("Desktop")
WSHShell.AppActivate strDesktop
WScript.Sleep 500
WSHShell.SendKeys "{F5}"
If err.number = 0 then
     WScript.Echo "Desktop Refreshed "
     Else WScript "Error " & err.number
     err.number = 0
End if
WScript.Quit

' End of VBScript to refresh the desktop
 

 

Guy Learning Points

Note 1:  I could not resist adding a little routing to confirm that the script had run successfully.  The key point is the If statement tests that there is no error: If err.number = 0

Note 2: Scripts involving SendKeys often work fine on my machine but fail on other people's systems.  To overcome this irritation, I add a WScript.Sleep 500 command, which tells VBScript to wait half a second before processing the next command.  This Sleep technique is particularly suitable for logon scripts.

Summary of SendKeys

I just love scripts that are instructive at more than one level.  The second script employs SendKeys to refresh the desktop.  Where as the third script adds error-correcting code.  Between them, the three scripts show a variety of ways to approach a scripting problem.  My hidden agenda is the power of the internet to research VBScript methods.

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

Their topics and material are ideal for getting you started with VBScript.  The videos are easy to follow and you can control the pace.  Try their free demo material and then see if you want to buy the full package. See more about VB Script Training CD.


 *


Google

Webcomputerperformance.co.uk

GFi Events Manager

Guy Recommends: GFi EventsManager

Here is a solution to monitor, manage and archive thousands of events that are generated by devices across your entire network.  Get your free evaluation copy of GFI EventsManager.

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.