Overcoming hurdles, jumping through hoops, walking on a tight rope: Scripting IT pros do it all! We walk a fine line between efficiency and utility. The more efficient the script, the more difficult it is to write, to troubleshoot, and to modify—at times. At other times, inefficient scripts can be just as difficult to write, to troubleshoot, and to modify. When a script finally runs without error, we often feel our work is done.
But what happens when the script runs without error, but is neither efficient, nor inefficient. What happens when the script does not appear to run at all? Now you have some serious hurdles to overcome. For this event you will use a script that is broken. Here is the VBScript version of the broken script:
Beg_6.vbs
'=================================================================
'
' VBScript: AUTHOR: Ed Wilson , msft, 6/15/2009
'
' NAME: Beg_6.vbs
'
' problem script for Beginner Event 6.
' 2009 Summer Scripting Games
'=================================================================
Option Explicit
On Error Resume Next
Dim objShell 'Instance of the WshShell object
Dim strDesktop 'Pointer to desktop special folder
Dim objShortCut 'Used to set properties of the shortcut. Comes from using CreateShortcut
Dim objURL 'Used to set properties of webshortcut.
Dim objNotepad
Dim wshNetwork
set objShell = CreateObject("WScript.Shell")
strDesktop = objShell.SpecialFolders("Desktop")
set objShortCut = objShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
objShortCut.TargetPath = WScript.ScriptFullName
objShortCut.WindowStyle = 0
objShortCut.Hotkey = "CTRL+SHIFT+F"
objShortCut.IconLocation = "notepad.exe, 0"
objShortCut.Description = "Shortcut Script"
objShortCut.WorkingDirectory = strDesktop
objShortCut.Save()
Set objShell = CreateObject("Wscript.Shell")
set objURL = objShell.CreateShortcut(strDesktop & "\The Microsoft Scripting Guys.url")
objURL.TargetPath = "http://www.ScriptingGuys.com"
objURL.Save()
Set wshNetwork = CreateObject("WScript.Network")
set objShortCut = objShell.CreateShortcut(strDesktop & "\notepad.lnk")
objShortCut.TargetPath = "notepad.exe"
objShortCut.IconLocation = "notepad.exe, 0"
objShortCut.description = "notepad"
objShortCut.Save
Here is the Windows PowerShell version of the script:
Beg_6.ps1
#==========================================================================
#
# PowerShell: AUTHOR: Ed Wilson , msft, 6/15/2009
#
# NAME: Beg_6.ps1
#
# COMMENT: Key concepts are listed below:
#1. Uses wscript.shell to create three shortcuts on the desktop. The first is a shortcut
#2. to this actual script. It uses the scriptfullName property to assign the path.
#3. The second is a simple Web site URL shortcut. The third one is a shortcut to
#4. Notepad.
#==========================================================================
$ErrorActionPreference = "SilentlyContinue"
Set-PSDebug -Strict
New-Variable -Name obShell #instance of the wshSHell object
New-Variable -Name strDesktop #pointer to desktop special folder
New-Variable -Name objShortCut #used to set properties of the shortcut. Comes from using createShortCut
New-Variable -Name objURL #used to set properties of webshortcut.
$oShell = New-Object -ComObject ("WScript.Shell")
$strDesktop = $objShell.SpecialFolders.item("Desktop")
$objShortCut = $objShell.CreateShortcut($strDesktop + "\Shortcut Script.lnk")
$objShortCut.TargetPath = $MyInvocation.ScriptName
$objShortCut.WindowStyle = 0
$objShortCut.Hotkey = "CTRL+SHIFT+F"
$objShortCut.IconLocation = "notepad.exe, 2"
$objShortCut.Description = "Shortcut Script"
$objShortCut.WorkingDirectory = $strDesktop
$objShortCut.Save
$wshShell = New-Object -ComObject wscript.shell
$objURL = $objShell.CreateShortcut($strDesktop & "\The Microsoft Scripting Guys.url")
$objURL.TargetPath = "http://www.ScriptingGuys.com"
$objURL.Discription = "Scripting Guys"
$objURL.Save()
$wshNetwork = New-Object -ComObject wscript.network
$objShortCut = $objShell.CreateShortcut($strDesktop + "\notepad.link")
$objShortCut.TargetPath = "notpad.exe"
$objShortCut.IconLocation = "notepad.exe, 0"
$objShortCut.description = "notepad"
$objShortCut.Save()
The scripts are essentially the same, and even have similar errors. We can tell you that whoever wrote these scripts had very little idea about what they were doing. There is more than one error. There are more than two errors. Actually, there are quite a few errors. Troubleshooting is an essential skill for beginning scripters. It is also an essential skill for advanced scripters. As Plato once said, "To script is to troubleshoot." (Okay, we missed that class.)