| Login

Search this Blog


Links I like to keep around




Here are the most recent postings to this blog. Use the archive calendar or search to find other entries.
Nov17

Written by:R Teachout
11/17/2006 3:04 AM

'******************************************************************************
'scenario1.vbs
'Author: Peter Costantini, the Microsoft Scripting Guys
'Date: 9/2/04
'This script runs on the admin workstation. It gets a list of remote machines
'from a text file, copies the scripts to each machine, and
'then launches the first script on each machine.
'All scripts must be in the same folder.
'******************************************************************************

On Error Resume Next

'Initialize global constants and variables.
Const FOR_READING = 1
g_strHostFile = "computers.txt"

'Read computer names for install from text file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(g_strHostFile) Then
  Set objTextStream = objFSO.OpenTextFile(g_strHostFile, FOR_READING)
Else
  WScript.Echo "Input file " & g_strHostFile & " not found."
  WScript.Quit
End If

'Loop through list of computers and perform tasks on each.
Do Until objTextStream.AtEndOfStream
  g_strComputer = objTextStream.ReadLine
  Wscript.Echo VbCrLf & g_strComputer
  Wscript.Echo String(Len(g_strComputer), "-")
'Ping host to ensure that it is accessible.
  blnPing = PingHost
  If blnPing = True Then
    Wscript.Echo g_strComputer & " responded to ping."
'Call functions and sub-routines and handle logic.
    blnCopyFiles = CopyFiles
    If blnCopyFiles = True Then
      Wscript.Echo "Files copied. Running configuration and SP 2 " & _
       "installation."
      RunRemote
    Else
      Wscript.Echo "ERROR: Unable to copy files. Operation failed."
    End If
  Else
    WScript.Echo "ERROR: " & g_strComputer & " did not respond to ping."
  End If
Loop
objTextStream.Close

'******************************************************************************

Function PingHost

Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping -n 2 -w 1000 " & g_strComputer)
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
  PingHost = True
Else
  PingHost = False
End If

End Function

'******************************************************************************

Function CopyFiles

'Copies install.vbs and runonce.vbs from admin workstation to host.
'These scripts should be in same directory as this script.
'If not, edit the filenames in the array to include full path.

strRemoteFolder = "\\" & g_strComputer & "\c$\temp-ac"
arrFiles = Array("install.vbs", "runonce.vbs")
'If running Service Pack 2 setup from file to be copied to local computer,
'comment out the previous line and uncomment the following two lines:
'arrFiles = Array("install.vbs", "runonce.vbs", _
' "WindowsXP-KB835935-SP2-ENU.exe")
'In this case, you must also rename install-local.vbs to install.vbs.


'Check for folder on host and
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strRemoteFolder) Then
  objFSO.CreateFolder(strRemoteFolder)
  If Err = 0 Then
    WScript.Echo "Created folder " & strRemoteFolder & "."
  Else
    WScript.Echo "Unable to create folder " & strRemoteFolder & "."
    WScript.Echo "Error Number: " & Err.Number
    WScript.Echo "Error Source: " & Err.Source
    WScript.Echo "Error Description: " & Err.Description
    Err.Clear
    CopyFile = False
    Exit Function
  End If
End If

'Copy files.
intCount = 0
For Each strFile in arrFiles
  If objFSO.FileExists(strFile) Then
    objFSO.CopyFile strFile, strRemoteFolder & "\"
    If Err = 0 Then
      WScript.Echo "Copied file " & strFile & " to folder " & _
       strRemoteFolder & "."
      intCount = intCount + 1
    Else
      WScript.Echo "Unable to copy file " & strFile & "."
    End If
    Err.Clear
  Else
    WScript.Echo "File " & strFile & " not found."
  End If
Next

If (intCount = UBound(arrFiles) + 1) Then
  CopyFiles = True
Else
  CopyFiles = False
End If

End Function

'******************************************************************************

Sub RunRemote

'Runs Windows XP SP2 setup and configuration script on remote machine.
'Use cscript to insure that the script runs under cmd.exe
strScript = "cscript c:\temp-ac\install.vbs"

'Connect to WMI service on remote machine.
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
 & g_strComputer & "\root\cimv2")
If Err <> 0 Then
  WScript.Echo "Error connecting to WMI on \\" & g_strComputer & "."
  WScript.Echo "Error number: " & Err.Number
  WScript.Echo "Error source: " & Err.Source
  WScript.Echo "Error description: " & Err.Description
  Error.Clear
  Exit Sub
End If

'Get a Win32_Process object.
Set objProcess = objWMIService.Get("Win32_Process")

'Create a new process and run strScript in it.
intReturn = objProcess.Create(strScript, , , intProcessID)
If intReturn = 0 Then
  WScript.Echo "Launched " & strScript & " to install Windows XP " & _
   "Service Pack 2 and prepare the computer for configuration." & VbCrLf & _
   "Process ID: " & intProcessID & "."
Else
  WScript.Echo "Error: " & strScript & " could not be started." & VbCrLf & _
   "Return code: " & intReturn & "."
End If

End Sub

 

Download: RunAppOnRemoteMachine.vbs.txt

Tags:

The first rule of any technology used in a business is that automation applied to an efficient operation will magnify the efficiency. The second is that automation applied to an inefficient operation will magnify the inefficiency.
-Bill Gates

Inspired by Nina