Archive

Archive for the ‘VBScript’ Category

VBScript: Bypass the "Open File – Security Warning" dialog from VbScript

April 29, 2011 Leave a comment

This is awesome! Helped me in a big way. The link below has the information to temporarily disable the “Open File – Security Warning” dialog and then set it back when done via VBScript.

http://bartvdw.wordpress.com/2008/03/30/bypass-the-open-file-security-warning-dialog-from-vbscript/

Categories: VBScript

VBScript – Standardizing Default Fonts in Microsoft Outlook

January 15, 2010 Leave a comment
Our marketing department wanted us to change the default fonts in Outlook for all users.  Below is how I decided to handle it since GP does not appear to work here – this is very suprising to me.
  1. Loaded our standard image
  2. Logged in and opened Outlook 2007
  3. Opened Tools – Options
  4. Selected the "Mail Format" tab
  5. Clicked on "Stationary and Fonts…"
  6. Modified the font for "Mew mail messages", "Replying or forwarding messages", "Composing and reading plain text messages"
  7. Exited out of Outlook
  8. Open the registry and extracted the following key:
    1. HKEY_CURRENT_USERSoftwareMicrosoftOffice12.0CommonMailSettings
  9. I then made a VBScript that imports this registry key and repeated the process for the other version we have in production (still working on standardization)

This process works just the same for other versions of Outlook.

Categories: VBScript

VBScript – Poor Man’s “Energy Consumption Reducer”

January 11, 2010 Leave a comment
***Critical Note***  This is to be used this at your own risk and this is not a supported solution.  This includes a framework to provide a solution – you as the IT Professional are responsible for making the appropriate changes to the script and determining any deployment methods if any.  Remember – this will shutdown a device without user intervention.
 
Overview
So the back story on this one is that the company I work for has a couple things going on that need attention and this "Green" idea came from a coworker of mine.  So I took the concept and developed the script and did a business case (still pending approval).  I am not saying that this is better than anything else out there and I certainly do not put any claims on how much energy this could save – I just like the branding and control I have with it (at MMS last year Microsoft discussed that users tend to be more responsive to alerts or deployments that have your company logo).
 

List of things that took me down this path:

  1. We use SCCM to deploy software updates and some users, let’s say 1%-3% of them, were getting frustrated with the "Mandatory" installs and reboots (this is not a very large number but their titles make it high profile)
  2. We need to enforce reboots after the software updates because the Windows Installer will hold the rebooting pending and other deployments will fail
  3. Users complain that they do not get an alert for the install or the reboot (not likely because the balloons are enabled but the customer is always right)
  4. Although we tell the users that they should log off each night they do not so we cannot expect them to do the reboots and have them supressed in SCCM Software Updates

Proposed Solution
I devloped the script and HTA below that executes a shutdown (long-term the devices can be started back up by the End-User, Wake-On-LAN, or ideally Intel AMT).  The script calls the HTA if it is executed within the specified window.  If it does fall in the window then the HTA, with the company logo, is launched and the End-User is prompted with a 30 minutes countdown and a way to stop the shutdown or speed it up.

We use this with SCCM and have it published to run from the DP vs. chached so if a user is not on the internal network it does not run.  At this point we are still in Mixed Mode so IBCM is not a factor.

There is a folder structure that you will need to have which is outlined below:

  • File: WindowsClientShutdown.HTA
  • File: WindowsClientShutdown.VBS
  • Folder: Images
  • File: ImagesCOMPANY_LOGO.gif
  • File: Imageswinicon.ico

WindowsClientShutdown.VBS
(***NOTE***  The specified windows can be modified in the script by changing the "strStart" and "strEnd" variables.)

*************************************************************************
option explicit

‘————————————————–
‘Set Variables
‘————————————————–
Dim strDirectory, strFileName, WshShell, objFSO, objShell, objTextFile, strProgramFiles, strStartup, objWMIService
Dim colComputer, strComputer, objComputer, strLogonUser, objNetwork, strCurrentDir, strStart, strEnd

Set WshShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("Wscript.Network")
Set objShell = CreateObject("WScript.Shell")

strComputer = "."
strCurrentDir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")

strStart = "08:00:00 PM"
strEnd = "08:30:00 PM"

‘————————————————–
‘Body
‘————————————————–

If Time > strStart Then
 Wscript.Echo "It is after " & strStart & "."
 If Time < strEnd Then
  ‘Wscript.Echo strCurrentDir & "WindowsClientShutdown.HTA"
  WshShell.Run (chr(34) & strCurrentDir & "WindowsClientShutdown.HTA" & chr(34))
 Else
  ‘Wscript.Echo Time & " is later than " & strEnd & ".  It is too late to run this."
 End If
Else
 ‘Wscript.Echo Time & " is less than " & strStart
 ‘Wscript.Echo "Not time yet."
End If
*************************************************************************

 

WindowsClientShutdown.HTA
*************************************************************************

<script language="vbscript" type="text/vbscript">
<!–

‘————————————————–
‘ Global Variables
‘————————————————–
Dim strComputer, strAppName, objFSO, objShell, objWMIService, colComputer, objRegistry, WshShell, objNetwork
Dim nmsecs
Const adOpenStatic1 = 3
Const adLockOptimistic1 = 3

strComputer = "."

Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = createObject("wscript.shell")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\" & strComputer & "rootdefault:StdRegProv")

‘————————————————–
‘ End of Global Variables
‘————————————————–

‘——————————————————————-
‘ Onload_Window
‘——————————————————————-
Sub Onload_Window

 Window.resizeTo 300,300
 window.moveTo (screen.width-300) ,(screen.height – 300)

 Shutdown

 intrespond = msgbox("Is now a good time to shut down?", vbYesNo + vbQuestion + vbSystemModal, "Shutdown")
 
 If intrespond = vbYes Then
  ShutdownNow
 Else
  If intrespond = vbNo Then
   NoShutdown
   CloseWindow
  End If
 End If
 

End Sub

 

‘——————————————————————-
‘ CloseWindow
‘——————————————————————-
Sub CloseWindow
 window.close
End Sub

‘——————————————————————-
‘ Shutdown
‘——————————————————————-
Sub Shutdown

objShell.Run("shutdown -s -f -t 1800"), 1, TRUE

End Sub

‘——————————————————————-
‘ ShutdownNow
‘——————————————————————-
Sub ShutdownNow

objShell.Run("shutdown -a"), 1, TRUE
iTimerID = window.setInterval("ReallyShutdown", 3000)

End Sub

‘——————————————————————-
‘ ReallyShutdown
‘——————————————————————-
Sub ReallyShutdown

objShell.Run("shutdown -s -f -t 30"), 1, TRUE

End Sub

‘——————————————————————-
‘ NoShutdown
‘——————————————————————-
Sub NoShutdown

objShell.Run("shutdown -a"), 1, TRUE

End Sub

–>
</script>

<html>
<head>

<title>Windows Client Shutdown</title>

<HTA:APPLICATION ID="HTA1" APPLICATIONNAME="WindowsClientShutdownHTA" ICON="imageswinicon.ico"
  BORDER="thin"
  BORDERSTYLE="normal"
  CAPTION="yes"
  INNERBORDER="no"
  SELECTION="no"
  MAXIMIZEBUTTON="yes"
  MINIMIZEBUTTON="yes"
  SHOWINTASKBAR="yes"
  SINGLEINSTANCE="yes"
  SYSMENU="yes"
  VERSION="1.0" /> 
 
</head>

<body onload=Onload_Window>

<p><td><center><img src="imagesCOMPANY_LOGO.gif" hspace=5 vspace=5></center></td></p>

<p><td><center><b><font face="Arial" size="4">DEPARTMENT</font></b></center></td></p>

</body>
</html>

*************************************************************************

 

Categories: VBScript

VBScript – vbSystemModal

December 16, 2009 1 comment
When using a msgbox you can use "vbSystemModal" to make the prompt stay on top of all windows.  You can try the code in the example below to give it a try.  When you run it and are asked "Is now a good time?" try opening other applications – this prompt should stay on top of the other windows.  You will notice that the "You answered yes" and the "You answered no" prompts will not stay on top because we did not tell them to.
 
If you use this with an HTA the prompt will stay on top but the HTA will go behind other windows.  But, once the prompt is answered the focus goes back to the HTA – keep in mind that other windows can still go on top of the HTA but the prompt should bring attention to it.
 
Example:
 intAnswer = Msgbox("Is now a good time?", vbYesNo  + vbSystemModal, "Question")
 If intAnswer = vbYes Then
  Msgbox "You answered yes."
 Else
  Msgbox "You answered no."
 End If
Categories: VBScript

How to write to a .txt file with VBScript

August 13, 2009 Leave a comment
‘**********************************************
‘Set Variables
‘**********************************************
Dim Wshshell, objFSO, objNetwork, objShell, strDirectory, strTemp, strFileName, objTextFile, objFolder
‘Constants used in opening files.
Const ForReading = 1, ForWriting = 2, ForAppending = 8
StrTemp = "C:Temp"
Set WshShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("Wscript.Network")
Set objShell = CreateObject("WScript.Shell")
‘**********************************************
‘Create logs & Temp folders if they do not exist
‘**********************************************
If objFSO.FolderExists (strTemp) Then
 ‘WScript.Echo "No need to create " & strTemp
Else
 Set objFolder = objFSO.CreateFolder(strTemp)
 ‘WScript.Echo "Created " & strTemp
End If
strDirectory = "c:Logs"
strFileName = "c:Logstest.txt"
If objFSO.FolderExists (strDirectory) Then
 ‘WScript.Echo "No need to create " & strDirectory
Else
 Set objFolder = objFSO.CreateFolder(strDirectory)
 ‘WScript.Echo "Created " & strDirectory
End If
‘**********************************************
‘Write Script Execution Time
‘**********************************************
Set objTextFile = objFSO.OpenTextFile (strFileName, ForAppending, True)
 objTextFile.WriteLine
 objTextFile.Write ("Admin script ran at ")
 objTextFile.WriteLine Now
objTextFile.Close
wscript.quit
Categories: VBScript

How to use VBScript to search and replace in a file

August 13, 2009 Leave a comment
In this example  I have a .WS file used for IBM Client Access that is being imported into Citrix.  We need to change the .KMP pointer in the file to a network location.
 
Create a folder in C: called "Search" and then file in that folder called "TEST.WS" (it can be .txt or anything else Notepad can open) and past the following in it…
 
[Profile]
ID=WS
Description=
Version=9
[Translation]
IBMDefaultView=Y
DefaultView=
IBMDefaultDBCS=Y
DefaultDBCS=
[Communication]
AutoConnect=Y
Link=telnet5250
Session=5250
ForceConfigPanel=N
[Telnet5250]
HostName=testserver.test.com
Security=CA400
SSLClientAuthentication=Y
CertSelection=AUTOSELECT
[5250]
HostCodePage=037-U
PrinterType=IBM3812
[Keyboard]
CuaKeyboard=2
Language=United-States
IBMDefaultKeyboard=N
DefaultKeyboard=C:Documents and SettingsAdministratorApplication DataIBMClient AccessEmulatorprivateAS400.KMP
[LastExitView]
A=3 22 29 648 553 3 8 18 400 0 IBM3270— 37
 
 
The script is below
 
Const ForReading = 1
Const ForWriting = 2
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "KMP"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:SearchTest.ws", ForReading)
Do Until objFile.AtEndOfStream
    strSearchString = objFile.Readline
    Set colMatches = objRegEx.Execute(strSearchString) 
 If colMatches.Count > 0 Then
  For Each strMatch in colMatches  
   Wscript.Echo strSearchString
   TexttoChange=strSearchString
 
   ‘This is the new string you want to add:
   NewText="DefaultKeyboard=\TestUNCShareChanged.KMP"
 
   ‘wscript.echo TexttoChange
   ‘wscript.echo NewText
   ‘objFile.Close
   Set objFile = objFSO.OpenTextFile("d:Searchtest.ws", ForReading)
   strText = objFile.ReadAll
   objFile.Close
   
   strNewText = Replace(strText, TexttoChange, NewText)
   
   Set objFile = objFSO.OpenTextFile("C:SearchTest.ws", ForWriting)
   objFile.WriteLine strNewText
   objFile.Close
   wscript.quit
  Next
 END IF
‘wscript.echo strSearchString
Loop
objFile.Close
Categories: VBScript
%d bloggers like this: