<$BlogRSDUrl$>

Wednesday, May 26, 2004

Dealing with CScript and Wscript (How to restart a Wscript in Cscript) 

I just had a question regarding the IsCScript function - which ends a script if it's not run in CScript. How do you deal with this the very first time if you are in a login script. The simple answer is to fix CScript, and restart the script:
'----------------------------------------+----------------------------------------
' Login.vbs - Default login script for OU=Local 
'
'  Copyright© 2004, Christopher G. Lewis - HTTP://www.ChristopherLewis.com
'
' Date   Name    Comment
' ------ ------- -----------------------------------------------------------------
' 030507 CGL     Created
'----------------------------------------+----------------------------------------
Option Explicit

Dim objFSO
Dim WshShell
Dim WshNetwork

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell  = WScript.CreateObject ("WScript.shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")

'Check for CScript
If Not IsCScript() Then
  SetCScript()
  RestartWithCScript()
  Wscript.Quit 0
End If

Call Main()

'Clean up
Set objFSO = Nothing
Set WshShell  = Nothing
Set WshNetwork = Nothing

'----------------------------------------+----------------------------------------
' Main - Main sub
'
' Date   Name    Comment
' ------ ------- -----------------------------------------------------------------
' 030508 CGL     Created
'----------------------------------------+----------------------------------------
Sub Main()
  Wscript.echo "Do Stuff"
End Sub

'----------------------------------------+----------------------------------------
' IsCscript - Checks CScript vs. WScript
'
' Date   Name    Comment
' ------ ------- -----------------------------------------------------------------
' 020507 CGL     Created
'----------------------------------------+----------------------------------------
Function IsCScript()
  Dim objRegExp
  Set objRegExp = New RegExp
  objRegExp.IgnoreCase  = true
  objRegExp.Pattern = "cscript.exe$"
  IsCScript = objRegExp.Test(WScript.FullName)
  Set objRegExp = Nothing
End Function

'----------------------------------------+----------------------------------------
' SetCScript - Sets Cscript as the startup script
'
' Date   Name    Comment
' ------ ------- -----------------------------------------------------------------
' 040526 CGL     Created
'----------------------------------------+----------------------------------------
Function SetCScript()
  WshShell.Run "Cscript //H:CScript //S", 7, true
End Function

'----------------------------------------+----------------------------------------
' RestartWithCScript - Restarts this script with cscript.  
'   Note that this doesn't deal with script arguments.  I imagine you could 
'   do something using the following:
'   Dim strCMD
'   Dim iCount
'   strCMD = "Cscript //H:CScript " & chr(34) & Wscript.ScriptFullName & CHr(34)
'   if Wscript.Arguments.Count > 0 Then
'     For iCount = 0 to Wscript.Arguments.Count-1
'       'Add Argument
'       strCMD = strCMD & " " & Wscript.Arguments(iCount)
'     Next
'   End if 
'   WshShell.Run strCMD, 1, False
'
' Date   Name    Comment
' ------ ------- -----------------------------------------------------------------
' 040526 CGL     Created
'----------------------------------------+----------------------------------------
Function RestartWithCScript()
  WshShell.Run "Cscript //H:CScript " & chr(34) & Wscript.ScriptFullName & CHr(34), 1, False
End Function

'----------------------------------------+----------------------------------------
' End of Logon.vbs
'----------------------------------------+----------------------------------------
Copyright © 2003, Christopher G. Lewis
|

Monday, March 01, 2004

Remove a computer from a domain 

Here's a function to remove a computer from a domain. We're using it as part of a massive clean up effort.
'----------------------------------------+----------------------------------------
' RemoveComputer - Removes a computer from a domain
'
' Date   Name    Comment
' ------ ------- -----------------------------------------------------------------
' 040227 CGL     Created
'----------------------------------------+----------------------------------------
Function RemoveComputer( strDomain, strComputer )
  Dim objDC
  On error resume next
  Set objDC = getobject("WinNT://" & strDomain )
  objDC.Delete "Computer", strComputer
  if err = 0 Then
    RemoveComputer = true
  else
    RemoveComputer = false
  end if
  On error goto 0
End function
Copyright © 2003, Christopher G. Lewis
|

This page is powered by Blogger. Isn't yours? Weblog Commenting and Trackback by HaloScan.com