Title: Automate Windows Administration
1Automate Windows Administration
- David Lundell, MBA
- MCITP Database Administrator, Database
Developer, MCDBA MCT MCSE MCSD - Mutually Beneficial Inc
- David_at_MutuallyBeneficial.com
- www.MutuallyBeneficial.com
For updated slides go to www.mutuallybeneficial.co
m/index_files/articles.htm
2Why Automate
- Automate administration
- Free up your time for serious work
- Make your job fun
- Make time for training
- Make time for being proactive
3Keys to Automating Windows
- Know a scripting language
- VBScript
- Jscript
- Perl
- Know ADSI
- Understand logon scripts
- Know your Script environment
- Understand WMI
- Understand the best command line tools
4VBScript
- Simplified Version of Visual Basic
- Has the most scripting examples
- Often the easiest for non-programmers to
assimilate
Dim oOU Set oDom GetObject("LDAP//dcmbi,dcco
m") Set oOU oDom.Create("organizationalUnit",
"oulopsa") oOU.Description League of
Professional System Administrators oOU.SetInfo
5JScript
- Also provided with Windows Script Host
- Similar to C and Java
- More powerful but more difficult
- var oDom GetObject("LDAP//dcmbi,dccom")
- var oOU oDom.Create("organizationalUnit",
"oulopsa") - oOU.Description "League of Professional System
Administrators" - oOU.SetInfo()
6Perl
- Must download from ActiveState
- use strict
- use Win32OLE('in')
- use Win32OLEVariant support for OLE data
types - use Win32OLEConst ('Active DS') Load ADSI
Constants - my objDom Win32OLE-gtGetObject("LDAP//dcm
bi,dccom") - my objOU objDom-gtCreate("organizationalUnit
","oulopsa") - objOU-gtDescription "League of
Professional System Administrators" - objOU-gtSetInfo()
7ADSI and VBScript -- Create an OU explained
- Dim oOU in VBscript dimension variables is not
required - Serverless Binding to ADSI it uses DNS to
find a DC - Set oDom GetObject("LDAP//dcmutuallybeneficial
,dccom") - Create the OU
- Set oOU oDom.Create("organizationalUnit",
"oulopsa") - Set some attributes
- oOU.Put Description, League of Professional
System Administrators - Until SetInfo is called changes are in local
cache only - oOU.SetInfo
8ADSI
- Simplified Interface for accessing LDAP
(including AD), NT, NDS - Binding
- Server
- Server less
- Root DSE
- Easy way to create new objects
- Remember to SetInfo after making changes
9Lopsa OU created
10Create a User
- Dim oOu, oUsr
- Set oOU _ GetObject("LDAP//OUlopsa,dcmbi,dcc
om") - We bind to the object we need and then call a
method - Set oUsr oOU.Create("User", cn
SysAdofTheYear") - oUsr.Put Description, Sys Admin of the Year
- oUsr.Put "sAMAccountName", "SysAdminOTY
- oUsr.SetInfo
11Microsoft Identity Integration Server
- Identity Management/MetaDirectory solution
- Integrate identities from AD, LDAP, Novell, IBM,
Sun - Provision Accounts based on HR Database
- Deprovision Accounts based on Status from HR
- Allows attributes, such as phone to be updated
in one place and flowed to the others
12Logon Scripts
- Assigned through AD Users and Computers
- OR through Group Policy
- Logon and Logoff scripts
- Startup and Shutdown
- Batch File can call script file
13Setting Logon Scripts
14Logon Scripts Common Tasks
- Inform/Ask User about upcoming installations
- WScript.Echo WScript.Shell.Popup or MsgBox
- Map Drives
- WScript.Network.MapNetworkDrive
- Map Printers
- WScript.Network.AddWindowsPrinterConnection
- Creating Shortcuts
- WScript.Shell.CreateShortcut
- Running other utilities
- WScript.Shell.Run
15Logon scripts without scripting (batch files)
- _at_net use U /del
- _at_net use P /del
- net use U \\SERVER1\users
- net use P \\SERVER1\public
- net time \\MY_PDC /set /yes
- copy \\MY_PDC\netlogon\notepad.exe.lnkc\windows\
sendto
16Logon Scripts with Scripting
- Still starts as a batch file
- Then calls a script using CScript.exe
- Can add more intelligence
- Look at group memberships
- Handle failures
- Double check assumptions
17Windows Script Host
- CScript.exe
- An executable for executing scripts from command
line - WScript.exe
- Executable from executing them in more of a
windows mode - Provides easy access to handy objects
- WScript
- WScript.Shell
- WScript.Network
18WScript
- WScript.Echo I really enjoyed LOPSA Phoenix
SysAdmin Days - WScript.Sleep
19WScript.Shell
- Set oShell CreateObject("WScript.Shell")
- Set oShort oShell.CreateShortcut("MyLink.lnk")
- oShort.TargetPath "C\MyLink.txt"
- oShort.Save
- Set oUrlLink oShell.CreateShortcut("MyWeb
Site.URL") - oUrlLink.TargetPath "http//www.myweb.com"
- oUrlLink.Save
20WScript.Network
- Set oNetwork WScript.CreateObject("WScript.Netwo
rk") - oNetwork.MapNetworkDrive U", \\Server1\Users
- oNetwork.AddWindowsPrinterConnection
"\\Server1\HP LaserJet"
212nd Half
- Windows Management Instrumentation
- Even greater automation capabilities
- Scriptomatic
- Generate Scripts in VBScript, Perl, Jscript
- WMI Code Creator
- Managing Event Logs
- LogParser tool
22WMI Windows Management Instrumentation
- Can be used to manage all sorts of services and
applications - Can be used to interrogate hardware
- With a WQL filter on GPO to limit which computers
will have a GPO apply.
23WMI Scriptomatic
- Microsoft Downloads
- Generate scripts using WMI
- VBScript
- Jscript
- Perl
- Python
- Run code right then and display in
- Text
- Excel
- HTML
- XML
24(No Transcript)
25WMI Code Creator
- Download from Microsoft
- Generate
- VBScript
- VB.NET
- C
- Easier to navigate
- Query Data
- Execute a Method
- Receive an Event
- Browse NameSpaces
26(No Transcript)
27(No Transcript)
28WMI Summary
- Providers
- Namespace
- Classes
29Copy Event Log to a Database
- Could use some complex WMI and ADO queries
- Instead use the Log Parser 2.2 from MS
- "C\Program Files\Log Parser 2.2\LogParser.exe"
"SELECT ComputerName, EventLog, EventID,
TimeGenerated, Message, EventTypeName,
EventCategoryName, SourceName - INTO dbo.ApplicationLog FROM Application"
- -oSQL -server. -databaseAppLog -driver"SQL
Server" -usernameAppLogger -passwordapplogger
-createTableON
30Backup and Clear Events from Event Log
- Set cLogFiles objWMIService.ExecQuery _
("Select from Win32_NTEventLogFile where
LogFileName IN ('Application', 'System)") - For Each oLogfile in colLogFiles
- iBackResult oLogFile.Backup(c\EvtLogs\oLogF
ile.Name) - If iBackResult 0 Then
- oLogFile.ClearEventLog()
- Else
- Wscript.Echo Could not backup event log and
did not clear it - End If
- Next
31Use LogParser from Code and grab log from backup
file
- Set oLogQuery CreateObject("MSUtil.LogQuery")
- Set oEVTInputFormat CreateObject("MSUtil.LogQuer
y.EventLogInputFormat") - Set oSQLOutputFormat CreateObject("MSUtil.LogQue
ry.SQLOutputFormat") - ' Set output format parameters
- oSQLOutputFormat.Server"."
- oSQLOutputFormat.database"AppLog"
- ' Create query text
- strQuery "SELECT ComputerName, EventLog,
EventID, TimeGenerated, Message, EventTypeName,
EventCategoryName, SourceName INTO
AppLogger.ApplicationLog FROM c\EvtLogs\Applicati
on" - ' Execute query
- oLogQuery.ExecuteBatch strQuery, oEVTInputFormat,
oSQLOutputFormat
32Copy and Clear
- Combine the LogParser from code script with the
clear log code - Or even better backup the log to file, clear it
and then load from the backup file
33Microsoft Operations Manager
- Can search your event logs for important events
- Consumes WMI Data
- Management packs for different apps are available
for download preconfigured with events to
monitor and WMI data to capture and monitor
34More Information
- Script Center Script Repository
- http//www.microsoft.com/technet/scriptcenter/scri
pts/default.mspx - Microsoft Identity Integration Server
- Microsoft Operations Manager
- Mindworks -- MS Gold Partner Learning Solutions
Scottsdale - MOC 2433 VB Scripting (3 days)
- MOC 2439 Windows Management Instrumentation (2
days)
35More info -- continued
- http//www.roth.net/books/handbook/
- Win32 Perl Scripting The Administrator's
Handbook - http//www.windowsnetworking.com/kbase/WindowsTips
/WindowsNT/AdminTips/Logon/WindowsNTLoginScriptTri
cksandTips.html
36Questions
- David_at_MutuallyBeneficial.com