Title: Scripting 101 for Network Administrators
1Scripting 101 for Network Administrators
- Jim Kent, Network Administrator
- Ave Maria Law School
2What is scripting ?
- Autoexec.bat, batch file scripts.
- Network login scripts.
- A script is a set of commands aimed at automating
a process. - Scripts are usually setup to solve a problem.
3How to turn off the computers in the lab at the
end of the day?
- shutdown -s -m \\hflyb01 -t 05 -f
- shutdown -s -m \\805x20b -t 05 -f
- shutdown -s -m \\535x20b -t 05 -f
- shutdown -s -m \\705x20b -t 05 f
- Shutdown.exe is an add on from the resource kit.
4What are we going to cover
- WSH (Windows Script Host)
- VBScript (Visual Basic Scripting)
- WMI (Windows Management Instrumentation)
- ADSI (Active Directory Service Interfaces)
5Simple Script
- Set objWMIService GetObject("winmgmts")
- Set objLogicalDisk objWMIService.Get
("Win32_LogicalDisk.DeviceID'c'") - Wscript.Echo objLogicalDisk.Freespace
6Free space on the local C drive
7Display Memory Script
- strComputer "."
- Set objSWBemServices GetObject ("winmgmts\\"
strComputer) - Set colSWbemObjectSet objSWbemServices.
InstancesOf("Win32_LogicalMemoryConfiguration") - For Each objSWBemObject in colSWbemObjectSet
- Wscript.Echo "Total Physical Memory (kb) "
objSWbemObject.TotalPhysicalMemory - next
8Output from Memory Script
9Output window
- Set ie WScript.CreateObject("InternetExplorer.Ap
plication", "IE_") - ie.Navigate "aboutblank"
- ie.ToolBar 0
- ie.StatusBar 0
- ie.Width 600
- ie.Height 500
- ie.Left 0
- ie.top 0
- ie.Visible 1
10Empty IE Window
11Display Services
- Use WMI to output all the services on the
computer. Also show the status of each service.
12- Do While (ie.Busy)
- Loop
- Set objDoc ie.Document
- objdoc.Open
- objdoc.Writeln "lthtmlgtltheadgtlttitlegtService Status
lt/titlegtlt/headgt" - objdoc.Writeln "ltbody bgcolor'white'gt"
- objdoc.Writeln "lttable width'100'gt"
- objdoc.Writeln lttrgtlttd width'50'gtltbgt
Servicelt/bgtlt/tdgt" - objdoc.Writeln "lttd width'50'gtltbgtState
lt/bgtlt/tdgtlt/trgt"
13- strComputer "."
- Set objWMIService GetObject("winmgmts"
"impersonationLevelimpersonate!\\"
strComputer "\root\cimv2") - Set colServicesobjWMIService.ExecQuery ("Select
from Win32_Service") - For Each objService in colServices
- objdoc.Writeln lttrgtlttd width'50'gt"
objService.DisplayName "lt/tdgt" - objdoc.Writeln "lttd width'50'gt"
objService.State "lt/tdgt" - objdoc.Writeln "lt/trgt"
- Next
- objdoc.Writeln lt/tablegtlt/bodygtlt/htmlgt"
- objdoc.Write()
- objdoc.Close
14(No Transcript)
15Display Info from a computer
- Use WMI to display the following stats.
- Display Computer Name
- Display the total physical ram in computer
- Display the time zone.
16- strComputer "."
- Set objWMIService GetObject("winmgmts"
"impersonationLevelimpersonate!\\"
strComputer "\root\cimv2") - Set colSettings objWMIService.ExecQuery
("Select From Win32_ComputerSystem") - For Each objComputer in colSettings
- objdoc.Writeln "lttrgtlttd width'50'gtComputger
Name lt/tdgt" - objdoc.Writeln "lttd width'50'gt"
objComputer.Name "lt/tdgtlt/trgt" - objdoc.Writeln "lttrgtlttd width'50'gtTotal Memory
lt/tdgt" - objdoc.Writeln "lttd width'50'gt"
int((objComputer.TotalPhysicalMemory)/1048576)
"lt/tdgtlt/trgt" - Next
- Set colSettings objWMIService.ExecQuery
("Select From Win32_TimeZone") - For Each objComputer in colSettings
- objdoc.Writeln "lttrgtlttd width'50'gtTimezone
lt/tdgt" - objdoc.Writeln "lttd width'50'gt"
objComputer.DayLightName "lt/tdgtlt/trgt" - Next
17(No Transcript)
18Display same info on multiple computers
- Add the ability to read a text file of computer
names. - Use IE window to output the data for each
computer.
19- Const ForReading 1
- Set objFSO CreateObject("Scripting.FileSystemObj
ect") - Set objFile objFSO.OpenTextFile("c\cpu.txt",
ForReading) - lt more code was heregt
- Do While objFile.AtEndOfStream false
- strComputer objFile.ReadLine
- ltcode for outputting data on strComputergt
- Loop
- objFile.Close
20(No Transcript)
21Local logged on user
- Use WMI to display the logged on user.
- Setup script to show the user on all lab
computers. - Use a text file list of computers to check.
22- Do While objFile.AtEndOfStream false
- strComputer objFile.ReadLine
- Set objWMIService GetObject("winmgmts"
"impersonationLevelimpersonate!\\"
strComputer "\root\cimv2") - Set colSettings objWMIService.ExecQuery
("Select From Win32_ComputerSystem") - For Each objComputer in colSettings
- objdoc.Writeln "lttrgtlttd width'50'gt"
strComputer "lt/tdgt" - objdoc.Writeln "lttd width'50'gt"
objComputer.username "lt/tdgtlt/trgt" - Next
23Users logged into lab computers
24WMI
- WMI comes standard preloaded and setup on Windows
2000/XP computers. - Make sure the WMI service is running.
- Key to WMI is finding the class you want to
query. - Must have admin rights on local PC or networked
pc to get any info back.
25Download Scriptomatic
26ADSI
- Released in 1997 as a set of generic interfaces
that access and manipulate different directory
services. - Admins and Developers can use ADSI to enumerate
and managed resources in a directory service. - Can Read, Modify, Create and Delete domain
objects.
27All Users Script
- Set Computer GetObject("WinNT//avemaria")
- Computer.Filter Array("User")
- For Each User in Computer
- objdoc.Writeln "lttrgt"
- objdoc.Writeln "lttd width'50'gtUserName lt/tdgt"
- objdoc.Writeln "lttd width'50'gt" User.Name
"lt/tdgt" - objdoc.Writeln "lt/trgt"
- Next
28All Users
29Display all Domain Groups
- Set Computer GetObject("WinNT//avemaria")
- Computer.Filter Array("Group")
- For Each Group in Computer
- objdoc.Writeln "lttrgtlttd width'50'gtGroupName
lt/tdgt" - objdoc.Writeln "lttd width'50'gt" Group.Name
"lt/tdgt" - objdoc.Writeln "lt/trgt"
- Next
30(No Transcript)
31Display members of Student Group
- Set Group GetObject("WinNT//avemaria/students,
group") - For Each User in Group.Members
- objdoc.Writeln lttrgtlttd width'50'gtUserName
lt/tdgt" - objdoc.Writeln "lttd width'50'gt" User.Name
"lt/tdgt" - objdoc.Writeln "lt/trgt"
- count count 1
- Next
32(No Transcript)
33Display all groups of each student
- Set Group GetObject("WinNT//avemaria/students,
group") - For Each User in Group.Members
- objdoc.Writeln "lttrgtlttd width'50'gt"
User.FullName "lt/tdgt" - objdoc.Writeln "lttd width'50'gtlt/tdgtlt/trgt"
- objdoc.Writeln "lttrgtlttd width'50'gt" User.Name
"lt/tdgt" - objdoc.Writeln "lttd width'50'gtlt/tdgtlt/trgt"
- Set User GetObject("WinNT//avemaria/"
User.Name ",user") - For Each Group in User.Group
- objdoc.Writeln "lttrgtlttd width'50'gtlt/tdgt"
- objdoc.Writeln "lttd width'50'gt"
Group.Name "lt/tdgtlttrgt - Next
- Next
34(No Transcript)
35User Properties
36Password Never Expires Flag?
- Set Group GetObject("WinNT//avemaria/students,
group") - For Each User in Group.Members
- objdoc.Writeln "lttrgtlttd width'50'gt" User.Name
"lt/tdgt" - Set User GetObject("WinNT//avemaria/"
User.Name ",user") - flags User.Get("UserFlags")
- If (Flags And H10000) 0 then
- objdoc.Writeln "lttd width'50'gtPassword
will expirelt/tdgt" - Else
- objdoc.Writeln "lttd width'50'gtPassword
does not expirelt/tdgt" - End If
- objdoc.Writeln "lt/trgt"
- Next
37(No Transcript)
38Force Password change flag
- Force user to change password on next logon flag
39- Set Group GetObject("WinNT//avemaria/students,
group") - For Each User in Group.Members
- objdoc.Writeln "lttrgtlttd width'50'gt" User.Name
"lt/tdgt" - Set User GetObject("WinNT//avemaria/"
User.Name ",user") - if User.passwordexpired 0 then
- objdoc.Writeln "lttd width'50'gtPassword
safelt/tdgt" - else
- objdoc.Writeln "lttd width'50'gtForce
change setlt/tdgt" - End If
- objdoc.Writeln "lt/trgt"
- Next
40Modify User Flags
41Create User Accounts
- Use text file for data source.
- Source reads one line of text at a time.
- Use character to separate fields
- Usernamepasswordfullname Descriptionloginscrip
t - kent1password1234kent, test1Test Account
student.bat
42- Do While objFile.AtEndOfStream false
- strdataline objFile.ReadLine
- myuser Split(strdataline,"")
- Set Computer GetObject("WinNT//avemaria")
- Set User computer.create("User",myuser(0))
- call User.SetPassword(myuser(1))
- user.fullname myuser(2)
- user.Descriptionmyuser(3)
- user.loginscriptmyuser(4)
- user.setinfo
- Wscript.echo "Created user " myuser(0)
- Loop
43ResourcesMicrosoft Scripting Guide
44Resources
- http//www.microsoft.com/technet/community/scriptc
enter/default.mspx - http//www.winscripter.com
- http//www.adsi4nt.com
- http//www.15seconds.com/focus/ADSI.htm