.: Computer Management Scripts
|
|
| |
Please select from the following vbscript examples:
|
|
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Block The Installation of XP Service Pack 2 - Requires Local Admin Rights
Description: Blocks the installation of XP Service Pack 2 either by SUS or Windows Update by setting a registry value
' Copyright (c) Microsoft Corporation 2004
' File: BlockXPSP2.vbs
' Contents: Remotely blocks or unblocks the delivery of
' Windows XP SP2 from Windows Update web site or via Automatic
' Updates.
' History: 8/20/2004 Peter Costantini Created
' Version: 1.0
On Error Resume Next
' Define constants and global variables.
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "." ' Can be changed to name of remote computer.
strKeyPath = "Software\Policies\Microsoft\Windows\WindowsUpdate"
strEntryName = "DoNotAllowXPSP2"
dwValue = 1
' Handle command-line arguments.
Set colArgs = WScript.Arguments
If colArgs.Count = 0 Then
ShowUsage
Else
If colArgs.Count = 2 Then
strComputer = colArgs(1)
End If
' Connect with WMI service and StdRegProv class.
Set objReg = GetObject _
("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
If Err = 0 Then
If (LCase(colArgs(0)) = "/b") Or _
(LCase(colArgs(0)) = "-b" ) Then
AddBlock
ElseIf (LCase(colArgs(0)) = "/u") Or _
(LCase(colArgs(0)) = "-u") Then
RemoveBlock
Else
ShowUsage
End If
Else
WScript.Echo "Unable to connect to WMI service on " _
& strComputer & "."
End If
Err.Clear
End If
'*************************************************************
Sub AddBlock
'Check whether WindowsUpdate subkey exists.
strParentPath = "SOFTWARE\Policies\Microsoft\Windows"
strTargetSubKey = "WindowsUpdate"
intCount = 0
intReturn1 = objReg.EnumKey(HKEY_LOCAL_MACHINE, _
strParentPath, arrSubKeys)
If intReturn1 = 0 Then
For Each strSubKey In arrSubKeys
If strSubKey = strTargetSubKey Then
intCount = 1
End If
Next
If intCount = 1 Then
SetValue
Else
WScript.Echo "Unable to find registry subkey " & _
strTargetSubKey & ". Creating ..."
intReturn2 = objReg.CreateKey(HKEY_LOCAL_MACHINE, _
strKeyPath)
If intReturn2 = 0 Then
SetValue
Else
WScript.Echo "ERROR: Unable to create registry " & _
"subkey " & strTargetSubKey & "."
End If
End If
Else
WScript.Echo "ERROR: Unable to find registry path " & _
strParentPath & "."
End If
End Sub
'*************************************************************
Sub SetValue
intReturn = objReg.SetDWORDValue(HKEY_LOCAL_MACHINE, _
strKeyPath, strEntryName, dwValue)
'If intReturn = 0 Then
' WScript.Echo "Added registry entry to block Windows XP " & _
' "SP2 deployment via Windows Update or Automatic Update."
'Else
' WScript.Echo "ERROR: Unable to add registry entry to " & _
' "block Windows XP SP2 deployment via Windows Update " & _
' "or Automatic Update."
'End If
End Sub
'*************************************************************
Sub RemoveBlock
intReturn = objReg.DeleteValue(HKEY_LOCAL_MACHINE, _
strKeyPath, strEntryName)
'If intReturn = 0 Then
' WScript.Echo "Deleted registry entry " & strEntryName & _
' ". Unblocked Windows XP SP2 deployment via Windows " & _
' "Update or Automatic Update."
'Else
' WScript.Echo "Unable to delete registry entry " & _
' strEntryName & ". Windows XP SP2 deployment via " & _
' "Windows Update or Automatic Update is not blocked."
'End If
End Sub
'*************************************************************
Sub ShowUsage
WScript.Echo "Usage:" & VbCrLf & _
" BlockXPSP2.vbs { /b | /u | /? } [hostname]" & VbCrLf & _
" /b = Block (deny) Windows XP Service Pack 2 " & _
"deployment" & VbCrLf & _
" /u = Unblock (allow) Windows XP Service Pack 2 " & _
"deployment" & VbCrLf & _
" /? = Show usage" & VbCrLf & _
" hostname = Optional. Name of remote computer. " & _
"Default is local computer" & VbCrLf & _
"Example:" & VbCrLf & _
" BlockXPSP2.vbs /b client1"
End Sub
Add Domain User to Local Administrators - Requires Local Admin Rights
Description: Takes a domain user account and adds it to the local administrators group on a PC
Option Explicit
On Error Resume Next
'Define Variables
Dim CompName, objGroup, objUser, OSName, WshShell
' Set up an Instance of the WScript.Shell Object
Set WshShell = CreateObject("WScript.Shell")
'set access to Env vars
CompName = WshShell.ExpandEnvironmentStrings("%ComputerName%")
OSName = WshShell.ExpandEnvironmentStrings("%OS%")
' If Windows 9x - Then Quit
IF NOT OSName = "Windows_NT" Then
WScript.Quit
End If
' Access Local Admin Group
Set objGroup = GetObject("WinNT://" & CompName & "/Administrators")
' Access Domain User SOE/Deploy
Set objUser = GetObject("WinNT://ChangetoDomainName/ChangeToRequiredUserName")
' Use ADsPath Method to add user: objUser to Local Admin group: objgroup
objGroup.Add(objUser.ADsPath)
Set CompName=Nothing
Set objGroup=Nothing
Set objUser=Nothing
Set OSname=Nothing
WScript.Quit
Check if Local User account Exists
Description: Checks to see if a local user account exists on a pc.
Option Explicit
'On Error Resume Next
'Define Variables
Dim CompName1, objEnvVar1, OSName1, RunNetUser, Fso, FileExist, Line1, CMD
Dim TempDir, OpenFile, Line1Test
' Set up an Instance of the WScript.Shell Object and access Env var Computer Name
Set objEnvVar1 = WScript.CreateObject("WScript.Shell")
OSName1 = objEnvVar1.ExpandEnvironmentStrings("%OS%")
CompName1 = objEnvVar1.ExpandEnvironmentStrings("%ComputerName%")
CMD = objEnvVar1.ExpandEnvironmentStrings("%ComSpec% /K")
TempDir = objEnvVar1.ExpandEnvironmentStrings("%Temp%")
RunNetUser = ObjEnvVar1.Run(CMD & " Net User ChangetoRequiredUserName > %Temp%\NetUser.txt" , 0, False)
WScript.Sleep 1000
Set Fso = Createobject("Scripting.FileSystemObject")
FileExist = Fso.FileExists(TempDir & "\NetUser.txt")
If FileExist = True Then
Set OpenFile = Fso.OpenTextfile(TempDir & "\NetUser.txt", 1)
Do While OpenFile.AtEndOfStream = False
Line1 = Openfile.ReadAll
Loop
If line1 = "" Then
MsgBox "User Account Does Not Exist"
Else
MsgBox "User Account Exists"
End If
End If
Set CompName1=Nothing
Set objEnvVar1=Nothing
Set OSname1=Nothing
Set RunNetUser=Nothing
Set Fso=Nothing
Set FileExist=Nothing
Set Line1=Nothing
Set CMD=Nothing
Set TempDir=Nothing
Set OpenFile=Nothing
Set Line1Test=Nothing
'On Error Goto 0
WScript.Quit
Add VBScript to New File Context Menu - Requires Local Admin Rights
filetype = ".vbs"
' connect to WScript.Shell for registry access:
set WSHShell = CreateObject("WScript.Shell")
' read in the name of the vbs-section:
prg = ReadReg("HKCR\" & filetype & "\")
' read in the official name for vbs-files:
prgname = ReadReg("HKCR\" & prg & "\")
' ask for a new name
ask = "What should be the name for new VBScript scripts?"
title = "New menu entry"
prgname = InputBox(ask, title, prgname)
' save the new program description:
WSHShell.RegWrite "HKCR\" & prg & "\", prgname
' add a New menu entry asking for an empty new file:
WSHShell.RegWrite "HKCR\" & filetype & "\ShellNew\NullFile", ""
' reads a registry key
' should the key be invalid, complain and stop:
function ReadReg(key)
on error resume next
ReadReg = WSHShell.RegRead(key)
if err.Number>0 then
' key could not be read: complain!
error = "Error: Registry-Key """ & key _
& """ could not be found!"
MsgBox error, vbCritical
WScript.Quit
end if
end function
Get Computers Version Number
Option Explicit
Dim objWMI, objItem, colItems
Dim strComputer, VerOS, VerBig, Ver9x, Version9x, OS, OSystem
On Error Resume NExt
strComputer = "."
' This is where WMI interrogates the operating system
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
' Here we filter Version from the dozens of properties
For Each objItem in colItems
VerBig = Left(objItem.Version,3)
Next
'5.0 = W2K
'5.1 = XP
'5.2 = Windows 2003
'4.0 = NT 4.0
Msgbox(VerBig)
On Error Goto 0
WScript.Quit
Find Service Pack Version
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
Wscript.Echo objOperatingSystem.ServicePackMajorVersion _
& "." & objOperatingSystem.ServicePackMinorVersion
Next
Web site contents © Copyright Alan Phipps 2006, All rights reserved.
Website templates |