Upload
elinor-park
View
218
Download
0
Tags:
Embed Size (px)
Citation preview
Scripting 101 for Scripting 101 for Network AdministratorsNetwork Administrators
Jim Kent, Network AdministratorJim Kent, Network Administrator
Ave Maria Law SchoolAve Maria Law School
What is scripting ?What is scripting ?
Autoexec.bat, batch file scripts.Autoexec.bat, batch file scripts.
Network login scripts.Network login scripts.
A script is a set of commands aimed at A script is a set of commands aimed at automating a process.automating a process.
Scripts are usually setup to solve a Scripts are usually setup to solve a problem.problem.
How to turn off the computers in the How to turn off the computers in the lab at the end of the day?lab at the end of the day?
shutdown -s -m \\hflyb01 -t 05 -fshutdown -s -m \\hflyb01 -t 05 -fshutdown -s -m \\805x20b -t 05 -fshutdown -s -m \\805x20b -t 05 -fshutdown -s -m \\535x20b -t 05 -fshutdown -s -m \\535x20b -t 05 -fshutdown -s -m \\705x20b -t 05 –fshutdown -s -m \\705x20b -t 05 –f
Shutdown.exe is an add on from the Shutdown.exe is an add on from the resource kit.resource kit.
What are we going to cover:What are we going to cover:
WSH (Windows Script Host)WSH (Windows Script Host)
VBScript (Visual Basic Scripting)VBScript (Visual Basic Scripting)
WMI (Windows Management Instrumentation)WMI (Windows Management Instrumentation)
ADSI (Active Directory Service Interfaces)ADSI (Active Directory Service Interfaces)
Simple ScriptSimple Script
Set objWMIService = GetObject("winmgmts:")Set objWMIService = GetObject("winmgmts:")
Set objLogicalDisk = objWMIService.Get Set objLogicalDisk = objWMIService.Get ("Win32_LogicalDisk.DeviceID='c:'")("Win32_LogicalDisk.DeviceID='c:'")
Wscript.Echo objLogicalDisk.FreespaceWscript.Echo objLogicalDisk.Freespace
Free space on the local C: driveFree space on the local C: drive
Display Memory ScriptDisplay Memory ScriptstrComputer = "."strComputer = "."
Set objSWBemServices = GetObject Set objSWBemServices = GetObject ("winmgmts:\\" & strComputer)("winmgmts:\\" & strComputer)
Set colSWbemObjectSet = objSWbemServices. Set colSWbemObjectSet = objSWbemServices. InstancesOf("Win32_LogicalMemoryConfiguration")InstancesOf("Win32_LogicalMemoryConfiguration")
For Each objSWBemObject in colSWbemObjectSetFor Each objSWBemObject in colSWbemObjectSet
Wscript.Echo "Total Physical Memory (kb): " & Wscript.Echo "Total Physical Memory (kb): " & objSWbemObject.TotalPhysicalMemoryobjSWbemObject.TotalPhysicalMemory
nextnext
Output from Memory ScriptOutput from Memory Script
Output windowOutput window
Set ie = Set ie = WScript.CreateObject("InternetExplorer.ApplicatiWScript.CreateObject("InternetExplorer.Application", "IE_")on", "IE_")ie.Navigate "about:blank"ie.Navigate "about:blank"ie.ToolBar = 0ie.ToolBar = 0ie.StatusBar = 0ie.StatusBar = 0ie.Width = 600ie.Width = 600ie.Height = 500ie.Height = 500ie.Left = 0ie.Left = 0ie.top = 0ie.top = 0ie.Visible = 1ie.Visible = 1
Empty IE WindowEmpty IE Window
Display ServicesDisplay Services
Use WMI to output all the services on the Use WMI to output all the services on the computer. Also show the status of each computer. Also show the status of each service.service.
Do While (ie.Busy)Do While (ie.Busy)
LoopLoop
Set objDoc = ie.DocumentSet objDoc = ie.Document
objdoc.Openobjdoc.Open
objdoc.Writeln "<html><head><title>Service Status objdoc.Writeln "<html><head><title>Service Status </title></head>"</title></head>"
objdoc.Writeln "<body bgcolor='white'>"objdoc.Writeln "<body bgcolor='white'>"
objdoc.Writeln "<table width='100%'>"objdoc.Writeln "<table width='100%'>"
objdoc.Writeln “<tr><td width='50%'><b> objdoc.Writeln “<tr><td width='50%'><b> Service</b></td>" Service</b></td>"
objdoc.Writeln "<td width='50%'><b>State objdoc.Writeln "<td width='50%'><b>State </b></td></tr>"</b></td></tr>"
strComputer = "."strComputer = "."Set objWMIService = GetObject("winmgmts:" & Set objWMIService = GetObject("winmgmts:" &
"{impersonationLevel=impersonate}!\\" & strComputer& "\"{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2")root\cimv2")
Set colServices=objWMIService.ExecQuery ("Select * from Set colServices=objWMIService.ExecQuery ("Select * from Win32_Service")Win32_Service")
For Each objService in colServicesFor Each objService in colServicesobjdoc.Writeln “<tr><td width='50%'>" & objdoc.Writeln “<tr><td width='50%'>" &
objService.DisplayName & "</td>"objService.DisplayName & "</td>"objdoc.Writeln "<td width='50%'>" & objService.State & "</td>"objdoc.Writeln "<td width='50%'>" & objService.State & "</td>"objdoc.Writeln "</tr>"objdoc.Writeln "</tr>"NextNextobjdoc.Writeln “</table></body></html>"objdoc.Writeln “</table></body></html>"objdoc.Write()objdoc.Write()objdoc.Closeobjdoc.Close
Display Info from a computerDisplay Info from a computer
Use WMI to display the following stats.Use WMI to display the following stats.
Display Computer NameDisplay Computer Name
Display the total physical ram in computerDisplay the total physical ram in computer
Display the time zone.Display the time zone.
strComputer = "."strComputer = "."Set objWMIService= GetObject("winmgmts:" & Set objWMIService= GetObject("winmgmts:" &
"{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")"{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery ("Select * From Set colSettings = objWMIService.ExecQuery ("Select * From Win32_ComputerSystem")Win32_ComputerSystem")
For Each objComputer in colSettingsFor Each objComputer in colSettings
objdoc.Writeln "<tr><td width='50%'>Computger Name: </td>"objdoc.Writeln "<tr><td width='50%'>Computger Name: </td>"objdoc.Writeln "<td width='50%'>" & objComputer.Name & "</td></tr>"objdoc.Writeln "<td width='50%'>" & objComputer.Name & "</td></tr>"
objdoc.Writeln "<tr><td width='50%'>Total Memory: </td>"objdoc.Writeln "<tr><td width='50%'>Total Memory: </td>"objdoc.Writeln "<td width='50%'>" & objdoc.Writeln "<td width='50%'>" &
int((objComputer.TotalPhysicalMemory)/1048576) & "</td></tr>"int((objComputer.TotalPhysicalMemory)/1048576) & "</td></tr>"NextNextSet colSettings = objWMIService.ExecQuery ("Select * From Set colSettings = objWMIService.ExecQuery ("Select * From
Win32_TimeZone")Win32_TimeZone")For Each objComputer in colSettingsFor Each objComputer in colSettings
objdoc.Writeln "<tr><td width='50%'>Timezone: </td>"objdoc.Writeln "<tr><td width='50%'>Timezone: </td>"objdoc.Writeln "<td width='50%'>" & objComputer.DayLightName & "</td></tr>"objdoc.Writeln "<td width='50%'>" & objComputer.DayLightName & "</td></tr>"NextNext
Display same info on multiple Display same info on multiple computerscomputers
Add the ability to read a text file of Add the ability to read a text file of computer names.computer names.
Use IE window to output the data for each Use IE window to output the data for each computer.computer.
Const ForReading = 1Const ForReading = 1
Set objFSO = Set objFSO = CreateObject("Scripting.FileSystemObject")CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\cpu.txt", Set objFile = objFSO.OpenTextFile("c:\cpu.txt", ForReading)ForReading)
< more code was here>< more code was here>
Do While objFile.AtEndOfStream = falseDo While objFile.AtEndOfStream = false
strComputer = objFile.ReadLinestrComputer = objFile.ReadLine
<code for outputting data on strComputer><code for outputting data on strComputer>
LoopLoop
objFile.CloseobjFile.Close
Local logged on userLocal logged on user
Use WMI to display the logged on user.Use WMI to display the logged on user.
Setup script to show the user on all lab Setup script to show the user on all lab computers.computers.
Use a text file list of computers to check.Use a text file list of computers to check.
Do While objFile.AtEndOfStream = falseDo While objFile.AtEndOfStream = falsestrComputer = objFile.ReadLinestrComputer = objFile.ReadLine
Set objWMIService= GetObject("winmgmts:" & Set objWMIService= GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")"\root\cimv2")
Set colSettings = objWMIService.ExecQuery ("Select * Set colSettings = objWMIService.ExecQuery ("Select * From Win32_ComputerSystem")From Win32_ComputerSystem")
For Each objComputer in colSettingsFor Each objComputer in colSettings
objdoc.Writeln "<tr><td width='50%'>" & strComputer & objdoc.Writeln "<tr><td width='50%'>" & strComputer & "</td>""</td>"
objdoc.Writeln "<td width='50%'>" & objdoc.Writeln "<td width='50%'>" & objComputer.username & "</td></tr>"objComputer.username & "</td></tr>"
NextNext
Users logged into lab computersUsers logged into lab computers
WMIWMI
WMI comes standard preloaded and setup WMI comes standard preloaded and setup on Windows 2000/XP computers.on Windows 2000/XP computers.
Make sure the WMI service is running.Make sure the WMI service is running.
Key to WMI is finding the class you want Key to WMI is finding the class you want to query.to query.
Must have admin rights on local PC or Must have admin rights on local PC or networked pc to get any info back.networked pc to get any info back.
Download ScriptomaticDownload Scriptomatic
ADSIADSI
Released in 1997 as a set of generic Released in 1997 as a set of generic interfaces that access and manipulate interfaces that access and manipulate different directory services.different directory services.
Admins and Developers can use ADSI to Admins and Developers can use ADSI to enumerate and managed resources in a enumerate and managed resources in a directory service.directory service.
Can Read, Modify, Create and Delete Can Read, Modify, Create and Delete domain objects.domain objects.
All Users ScriptAll Users Script
Set Computer = GetObject("WinNT://avemaria")Set Computer = GetObject("WinNT://avemaria")Computer.Filter = Array("User")Computer.Filter = Array("User")For Each User in ComputerFor Each User in Computerobjdoc.Writeln "<tr>"objdoc.Writeln "<tr>"objdoc.Writeln "<td width='50%'>UserName: </td>"objdoc.Writeln "<td width='50%'>UserName: </td>"objdoc.Writeln "<td width='50%'>" & User.Name & objdoc.Writeln "<td width='50%'>" & User.Name &
"</td>""</td>"objdoc.Writeln "</tr>"objdoc.Writeln "</tr>"NextNext
All UsersAll Users
Display all Domain GroupsDisplay all Domain GroupsSet Computer = Set Computer =
GetObject("WinNT://avemaria")GetObject("WinNT://avemaria")Computer.Filter = Array("Group")Computer.Filter = Array("Group")For Each Group in ComputerFor Each Group in Computerobjdoc.Writeln "<tr><td objdoc.Writeln "<tr><td
width='50%'>GroupName: </td>"width='50%'>GroupName: </td>"objdoc.Writeln "<td width='50%'>" & objdoc.Writeln "<td width='50%'>" &
Group.Name & "</td>"Group.Name & "</td>"objdoc.Writeln "</tr>"objdoc.Writeln "</tr>"NextNext
Display members of Student GroupDisplay members of Student GroupSet Group = GetObject("WinNT://avemaria/students, Set Group = GetObject("WinNT://avemaria/students,
group")group")
For Each User in Group.MembersFor Each User in Group.Members
objdoc.Writeln “<tr><td width='50%'>UserName: objdoc.Writeln “<tr><td width='50%'>UserName: </td>"</td>"
objdoc.Writeln "<td width='50%'>" & User.Name & objdoc.Writeln "<td width='50%'>" & User.Name & "</td>""</td>"
objdoc.Writeln "</tr>"objdoc.Writeln "</tr>"
count = count + 1count = count + 1
NextNext
Display all groups of each studentDisplay all groups of each studentSet Group = GetObject("WinNT://avemaria/students, group")Set Group = GetObject("WinNT://avemaria/students, group")For Each User in Group.MembersFor Each User in Group.Membersobjdoc.Writeln "<tr><td width='50%'>" & User.FullName &"</td>"objdoc.Writeln "<tr><td width='50%'>" & User.FullName &"</td>"objdoc.Writeln "<td width='50%'></td></tr>"objdoc.Writeln "<td width='50%'></td></tr>"objdoc.Writeln "<tr><td width='50%'>" & User.Name &"</td>"objdoc.Writeln "<tr><td width='50%'>" & User.Name &"</td>"objdoc.Writeln "<td width='50%'></td></tr>"objdoc.Writeln "<td width='50%'></td></tr>"
Set User = GetObject("WinNT://avemaria/" & User.Name & Set User = GetObject("WinNT://avemaria/" & User.Name & ",user")",user")
For Each Group in User.GroupFor Each Group in User.Group objdoc.Writeln "<tr><td width='50%'></td>"objdoc.Writeln "<tr><td width='50%'></td>" objdoc.Writeln "<td width='50%'>" & Group.Name & "</td><tr>“ objdoc.Writeln "<td width='50%'>" & Group.Name & "</td><tr>“ NextNextNextNext
User PropertiesUser Properties
Password Never Expires Flag?Password Never Expires Flag?Set Group = GetObject("WinNT://avemaria/students, group")Set Group = GetObject("WinNT://avemaria/students, group")For Each User in Group.MembersFor Each User in Group.Membersobjdoc.Writeln "<tr><td width='50%'>" & User.Name & "</td>"objdoc.Writeln "<tr><td width='50%'>" & User.Name & "</td>"
Set User = GetObject("WinNT://avemaria/" & User.Name & Set User = GetObject("WinNT://avemaria/" & User.Name & ",user")",user")
flags = User.Get("UserFlags")flags = User.Get("UserFlags")If (Flags And &H10000) = 0 thenIf (Flags And &H10000) = 0 then objdoc.Writeln "<td width='50%'>Password will objdoc.Writeln "<td width='50%'>Password will expire</td>"expire</td>"ElseElse objdoc.Writeln "<td width='50%'>Password does not objdoc.Writeln "<td width='50%'>Password does not expire</td>"expire</td>"End IfEnd If
objdoc.Writeln "</tr>"objdoc.Writeln "</tr>"NextNext
Force Password change flagForce Password change flag
Force user to change password on next Force user to change password on next logon flaglogon flag
Set Group = GetObject("WinNT://avemaria/students, group")Set Group = GetObject("WinNT://avemaria/students, group")For Each User in Group.MembersFor Each User in Group.Membersobjdoc.Writeln "<tr><td width='50%'>" & User.Name & objdoc.Writeln "<tr><td width='50%'>" & User.Name &
"</td>""</td>"
Set User = GetObject("WinNT://avemaria/" & User.Name & Set User = GetObject("WinNT://avemaria/" & User.Name & ",user")",user")
if User.passwordexpired = 0 thenif User.passwordexpired = 0 then objdoc.Writeln "<td width='50%'>Password safe</td>"objdoc.Writeln "<td width='50%'>Password safe</td>" elseelse objdoc.Writeln "<td width='50%'>Force change set</td>"objdoc.Writeln "<td width='50%'>Force change set</td>" End IfEnd Ifobjdoc.Writeln "</tr>"objdoc.Writeln "</tr>"NextNext
Modify User FlagsModify User Flags
Create User AccountsCreate User Accounts
Use text file for data source.Use text file for data source.
Source reads one line of text at a time.Source reads one line of text at a time.
Use ~ character to separate fieldsUse ~ character to separate fields
Username~password~fullname~ Username~password~fullname~ Description~loginscriptDescription~loginscript
kent1~password1234~kent, test1~Test kent1~password1234~kent, test1~Test Account~ student.batAccount~ student.bat
Do While objFile.AtEndOfStream = falseDo While objFile.AtEndOfStream = falsestrdataline = objFile.ReadLinestrdataline = objFile.ReadLinemyuser = Split(strdataline,"~")myuser = Split(strdataline,"~")
Set Computer = GetObject("WinNT://avemaria")Set Computer = GetObject("WinNT://avemaria")Set User = computer.create("User",myuser(0))Set User = computer.create("User",myuser(0))call User.SetPassword(myuser(1))call User.SetPassword(myuser(1))user.fullname = myuser(2)user.fullname = myuser(2)user.Description=myuser(3)user.Description=myuser(3)user.loginscript=myuser(4)user.loginscript=myuser(4)
user.setinfouser.setinfoWscript.echo "Created user: " & myuser(0)Wscript.echo "Created user: " & myuser(0)LoopLoop
ResourcesResources
Microsoft Scripting GuideMicrosoft Scripting Guide
ResourcesResources
http://www.microsoft.com/technet/community/http://www.microsoft.com/technet/community/scriptcenter/default.mspxscriptcenter/default.mspx
http://www.winscripter.comhttp://www.winscripter.com
http://www.adsi4nt.comhttp://www.adsi4nt.com
http://www.15seconds.com/focus/ADSI.htmhttp://www.15seconds.com/focus/ADSI.htm