TechRepublic
recently asked members to submit their favorite Network Administration scripts
for possible publication. One of the first to make a submission was Lee Mason. For his effort, Lee earned $400
and the satisfaction of seeing his scripts published on TechRepublic.
Earn $100 for your admin script
Let us pay
you for your original scripts so that we can publish them as downloads on
TechRepublic and allow your fellow IT professionals to benefit from your
scripting savvy. We only ask that you put in the appropriate comments to your
scripts so that it’s easy to tell what the script is doing and which variables
might need to be customized. Send us your original admin scripts
and we’ll pay you $100 for each one
that we publish as a TechRepublic download.
In their own words
Chose wallpaper
Listing A
‘This script checks the current screen resolution (by width)
‘and picks a background file from a set by renaming one of the source files
‘to the default background Filename.
‘Useful for setting a corporate background at different resolutions.
On Error goto 1000
Dim fol
Dim fil
Dim fso
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set fol = fso.GetFolder(“C:\windows\springboard”)
Dim screenwidth
‘this version checks the local machine – replace with a remote name if you wish. make sure the user running the script has permissions!
strComputer = “localhost”
Set objWMIService = GetObject(“winmgmts:”& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colItems = objWMIService.ExecQuery (“Select * from Win32_DisplayConfiguration”)
For Each objItem in colItems
screenwidth = objItem.pelswidth
Next
‘deletes the original default background file
fso.deletefile”C:\windows\springboard\background.png”
‘creates the new background file.
Select Case screenwidth
Case “800”
fso.CopyFile “C:\windows\background800.png”, “C:\windows\background.png”
Case “1024”
fso.CopyFile “C:\windows\background1024.png”, “C:\windows\background.png”
case “1152”
fso.CopyFile “C:\windows\background1152.png”, “C:\windows\background.png”
case “1280”
fso.CopyFile “C:\windows\background1280.png”, “C:\windows\sbackground.png”
case “1600”
fso.CopyFile “C:\windows\background1600.png”, “C:\windows\background.png”
end select
1000 msgboxerr.name
Set tf = Nothing
Set fol = Nothing
Set fso = Nothing
Mirror files
Listing B
‘Compare and copy Changed Files Script
‘Written By L Mason 5/8/2005
‘v1.0
‘Purpose: Compares local file structure with a source location and downloads changed or new items
‘CAUTION: This script only works for TWO folder levels (ie main level and one subfolder level).
‘Can easily be updated to include further subfolders by duplicating the loop statements
‘declare filesystemobject
Set fso = CreateObject(“Scripting.FileSystemObject”)
‘set variables
sourcetoplevelpath = “mysopurcepath” ‘enter the folder path (preferably UNC) of the folder that contains the SOURCE files.
Set sourcetoplevelfolder = fso.GetFolder(sourcetoplevelpath)
localtoplevelpath = “c:\windows\springboard” ‘enter the folder path (preferably UNC) of the folder that contains the DESTINATION files.
‘Set local folder and declare subfolders – create it if it doesn’t exist.
if fso.FolderExists(localtoplevelpath) Then
else
fso.copyFoldersourcetoplevelpath, localtoplevelpath
End If
Set localtoplevelfolder = fso.GetFolder(localtoplevelpath)
‘loop through top level of local folder and check items
For Each file In sourcetoplevelfolder.files
‘check for existance of the local file – if it exists, compare – if not, copy to local.
iffso.fileExists(localtoplevelpath & “\” & file.name) Then
set localfile = fso.getfile(localtoplevelpath & “\” & file.name)
‘create strings for comparison – include parentfolder items to ensure uniqueness.
sourcestring = file.parentfolder.name & ” ” & file.name & ” ” & file.datelastmodified
localstring = localfile.parentfolder.name & ” ” & localfile.name & ” ” & localfile.datelastmodified
‘compare strings. If different, then update file. If equal, do nothing and iterate to to next file.
iflocalstring <> sourcestring thenfso.CopyFilesourcetoplevelpath & “\” & file.name, localtoplevelpath & “\” & file.name, true
else
fso.CopyFilesourcetoplevelpath & “\” & file.name, localtoplevelpath & “\” & file.name, true
End If
Next
‘loop through contents of subfolders and do as above.
For Each folder in sourcetoplevelfolder.SubFolders
if fso.folderexists(localtoplevelpath & “\” & folder.name) then
Set sourcefolder = fso.getfolder(folder.path)
else
fso.createfolderlocaltoplevelpath & “\” & folder.name
Set sourcefolder = fso.getfolder(folder.path)
end if
For Each file In folder.files
set sourcefile = fso.getfile(file.path)
msgboxsourcefile
if fso.fileExists(localtoplevelfolder & “\” & file.name) Then
Set localfolder = fso.getfolder(localtoplevelpath & “\” & folder.name)
Set localfile = fso.getfile(localtoplevelfolder & “\” & file.name)
sourcestring = file.parentfolder.name & ” ” & file.name & ” ” & file.datelastmodified
msgboxsourcestring
localstring = localfile.parentfolder.name & ” ” & localfile.name & ” ” & localfile.datelastmodified
msgboxlocalstring
if localstring <> sourcestring then fso.CopyFilesourcefile, localtoplevelpath & “\” & file.name,true
else
fso.Copyfilesourcefile, localtoplevelpath & “\” & folder.name & “\” & file.name, true
end if
Next
Next
‘reclaim memory from object variables
set sourcetoplevelfolder = nothing
set localtoplevelfolder = nothing
set sourcefile = nothing
set localfile = nothing
Set fso = Nothing
Kill printer references
Listing C
‘vsbscript – Windows Scripting Host
‘Network printer disconnection script
‘Written by Lee Mason v1.0
‘Purpose: Startup script that removes all printer references except for those you specify
‘Initialises network And printer objects
Set WshNetwork = WScript.CreateObject(“WScript.Network”)
Set oPrinters = WshNetwork.EnumPrinterConnections
‘loops through printer connections And removes them – excepts For specified ports.
For i = 0 to oPrinters.Count – 1 Step 2
If oPrinters.Item(i+1) <> “PDF995PORT” Then WshNetwork.RemovePrinterConnectionoPrinters.Item(i+1), true, true
Next
Remove network drive mappings
Listing D
‘Startup script
‘Remove network drive mappings
‘Written by Lee Mason
‘displays drives and mappings
Set WshNetwork = WScript.CreateObject(“WScript.Network”)
Set oDrives = WshNetwork.EnumNetworkDrives
Set oPrinters = WshNetwork.EnumPrinterConnections