420 likes | 682 Views
Logon Scripting. Insert Instructors name Here. The evolution of the logon script. In the past, we mapped everyone manually Used a simple how to pamphlet so the user could map themselves, but for some this was still difficult to follow
E N D
Logon Scripting Insert Instructors name Here
The evolution of the logon script • In the past, we mapped everyone manually • Used a simple how to pamphlet so the user could map themselves, but for some this was still difficult to follow • After hours of research, we began to discover and develop Batch files • After years of research, we began to use VBScript
The evolution of the logon script • Windows Server 2000 Vs Windows Server 2003 • 2000 did not have a GPO setting for the logon script, but it did have one for each user in ADUC. Problem was, when a person would hop sites, an admin would have to know about it and change the script setting. This was to much overhead, so we just had the user run the scripts on their own giving them simple instructions and a file path • 2003 did have the GPO setting for a user logon script, but due to legacy mindsets, it was not used and we used the old method, up until now • Currently, we have a redirector script linked via GPO to every user in ADUC. This determines the subnet and runs a script associated with that subnet. The subnet script is managed by the site admin, while the redirector is managed by the enterprise admin. This allows for the site admin to choose how to script his site , what kind of script he will use and the enterprise admin just lays the groundwork to make that happen. • Utilities used for scripting: • Text editors (notepad.exe) • Text editors specifically built for scripts (Notepad ++)
What we use scripts for • Site logon Script is used to: • To map network drives and network printers • To set basic environmental variables on the computer (WAVE radio settings, etc) • Copy over files (mIRC settings) • Add items to Favorites • Change Homepage • Redirector script: A VBS script that replaces the need for a GPO set to a site for the logon script • It determines the subnet the user is in, then determines what site script to run based on the subnet • Types of scripts we use • Batch Files (.bat) • Visual Basic Scripts (.vbs)
Components of a script • Remarks: Remarks are used in scripts as place holder, descriptions of code and credits, among other things. It is text that is not read by the scripting engine • Variables: a keyword or phrase that is linked to a value stored in the system's memory • Used to store numeric values that can be changed, or text values that can also be changed or manipulated, or used in code • Constant: a special kind of variable whose value cannot be altered during program execution • Array: a variable that can have multiple values Example, Variable(0) = "red", Variable(1) = "Blue"
Components of a script • Procedure: a portion of code with a larger program that performs a specific task • Conditional Statements: a line of code that decides what code to execute based on a whether a statement is true or false • If…Then statements • Case Statements • Looping: a portion of code that continually executes until a certain condition is met • Count till ten for example • Input: a portion of code that prompts the user for input • Output: a portion of code that is displays something for the user
SET Command and Windows Environmental Variables • Using the SET command in DOS Prompt will display all the environmental variables in windows and what they are associated to. • For Example, if you want to know what DC the computer is authenticating with, run the SET command and look through the list for logonserver • Use in scripts • All enviro. Variables in scripts are enclosed between 2 % signs • EX %logonserver% • You can use the variable names to determine things in your script. For example, if you want, in your script you can use an if statement to run code based on what logon server it has • BATCH ExampleIf %logonserver% EQU DC02 echo "Domain Controller 2"
Commonly used Enviro. Variables • %COMPUTERNAME% - the computer name • %LOGONSERVER% - the DC authenticating with • %NUMBER_OF_PROCESSSORS% - number of processors on the system • %ProgramFiles% - Program Files directory • %USERDOMAIN% - Users domain • %USERNAME% - Users name • %windir% - Windows Directory
Batch Files • Used to execute commands in DOS (usually pops up a DOS window) • Echo: a command used to display text (output)to the user in the DOS window • @echo off - when placed at the top of a batch file procedure, the batch does not display the lines of code to the user as it is being commanded in DOS UNLESS there is a specific call to • @echo on – will display the code as it is being commanded when placed at top of batch file procedure • Echo – Displays a message to the user in DOS • EX: Echo This Text will display, echo will not • Remarks: used to make remarks that will be disregarded by DOS • REM is used to declare a remark in batch file • Example REM This is a remark
Batch files • NET USE: a command used to map a network drive or printer • Map Network drive • NET USE [driveletter:] \\Compname\share /persistent:No • If Persistent is no, the drive will not remain mapped when the user logs off • ExampleNET USE I: \\FS01.domain.net\Share /P:No • Disconnect drive • NET USE [driveletter:] /delete • ExampleNET USE I: /delete • Map printer • NET USE lpt# \\PrintServer\Printer /Persistant:No • ExampleNET USE lpt1 \\PS01.domain.net\printer1 /p:no
Batch Files • Goto Label • Used to jump to a portion of the batch procedure. The computer ignores the code between the Goto statement and the label it's being directed to when executed • Example GotoEnd ECHO Skipping this line :END ECHO Done!
Batch Files • IF Command: Allows for the batch file to perform a conditional process • If EXIST – used to determine if a file exist • ExampleIF EXIST "C:\secfix.cmd" (ECHO Yes) ELSE (ECHO No)orIF EXIST "C:\secfix.cmd" (ECHO Yes) IF NOT EXIST "C:\secfix.cmd" (ECHO No) • The "if" command can also be used to compare variables • Comparing Variable commands:EQU - equalNEQ - not equalLSS - less thanLEQ - less than or equalGTR - greater thanGEQ - greater than or equal • Example:IF %Number_of_Processors% EQU 2 (ECHO 2 Processors)
Other Batch file Commands • CLS : Clears the DOS screen • EXIT: Exits DOS • PAUSE: Pauses the batch file till user presses any key to continue • CALL: used to run another batch file within a batch file. When the batch file that is called is completed, the remainder of the original batch file is completed. Note if the batch file does not exist it will give an error message.
Putting a batch file together CLS REM This is an example of a more complex batch file @echo off ECHO This will map your printers and drives for your location Pause CLS IF %LOGONSERVER% EQU \\DC02 (Goto LabDC02) IF %LOGONSERVER% EQU \\DC03 (Goto LabDC03) ELSE (gotoLabError) :LabDC02 IF EXIST H: (NET USE H: /DELETE) NET USE y: \\fs01.domain.net\share/P:no NET USE lpt2 \\ps01.domain.net\printer /p:no Goto End :LabDC03 IF EXIST I: (NET USE I: /DELETE) NET USE I: \\FS02.domain.net\Share /P:no NET USE lpt3 \\ps02.domain.net\printer /p:no Goto End :LabError ECHO I'm sorry, your Domain Controller is not defined. Please notify your site admin. Pause exit :End ECHO your printer and network drive is mapped pause Exit
PE: Build and test a working Batch File • Using the above batch file, build a batch file using your domain's environmental variables and map a printer and a network drive based on those variables.
Visual Basic Scripts (VBS) • VBScripts give the script more functionality than the Batch files, but can be more difficult to write and more difficult to troubleshoot. Most of the same concepts are used in Vbscript, however the language itself can be different • Remarks: All remarks are designated with a ' mark • Example:' This is a remark • Wscript.echo: Used to open a message box with output for the user • Example:wscript.echo "This is a message using wscript.echo"
VBScript: Setting Variables • Variables are used for a variety of task. They can be changed through out the script and displayed in output or be used to determine what to do next in the script when used with other code. But, you must first define a variable to use it. • Example of a string Variable:strName = "Johnny" • Example of an interger Variable:intNumber = 1 • Example of variables being changedintNumber = intNumber + 1 ' intNumber is now 2 strName = strName &" Doomsday" 'strName is now Johnny Doomsday, the & is used in front of new text wscript.echostrName &" "&intNumber wscript.quit'Quits the script
VBScript • Constants are variables that don't get changed in a script • Example:Const unchangable = 1 • Set Arrays – To declare an array, you must first declare the variable name and the number of "compartments" for the array Dim cars(2) cars(0)="Volvo" cars(1)="Saab" cars(2)="BMW" • Quitting the script • To end the script, you can simply let the script run to the end of the script and it will stop, or you can put the following command in where you want it to end wscript.quit
VBScript: If Then • If, Then, Else Statements: This statement can be placed on one line or encompass a number of lines of code. • When multiple lines are used, an "end if" statement needs to be used ‘Example of single line:intValue = 4 If intValue = 4 Then wscript.echo "four" ‘Example of a multiple lineintValue = 4 If intValue = 4 Then wscript.echo "four" ElseifintValue = 5 Then wscript.echo "five" Else wscript.echo "Not five or four" End if • Comparing Variable commands:= - equal<> - not equal< - less than<= - less than or equal> - greater than>= - greater than or equal
VBScript: Case Statements • Case statements act a lot in the same way that If Then statements do, BUT, the scripting engine does not read the parts of the case statement that does not pertain to the particular case. In an if then, it still reads it, but it just does not execute. So, it makes the script run faster. ExampleintValue = 4 Select Case intValue Case 4 wscript.echo "Four" Case 5 wscript.echo "Five" Case Else wscript.echo "not Five or Four" End Select
VBScript: Loops • For Next Loops -operate till a condition is met Example (will end the loop when I = 5)for I = 0 to 5 wscript.echo "Number " & I next • For Each loops –operates till each value in an array has been addressedDim cars(2) cars(0)="Volvo" cars(1)="Saab" cars(2)="BMW" For Each x In cars wscript.echo x Next
VBScript: Loops • Do While Loops: loops through code until a condition is met • Examplei=0 Do While i < 10 wscript.echoi i=i+1 Loop
VBScript: Delete Network Printers • A networked printer over a WAN link will slow down a computer system. It is important to try and delete these printers before mapping any new ones. Here's the code to do it Set objNetwork = CreateObject ("Wscript.network") Set objPrinter = objNetwork.enumprinterconnections Printerflag=false For I = 0 to objPrinter.count -1 Step 2 if left(objPrinter.item(i+1), 2) ="\\" Then on error resume next objNetwork.RemovePrinterConnectionobjPrinter.item(i+1) on error Goto 0 End if Next
VBScript: Mapping a printer • First, set the network script variable objectSet WshNetwork = CreateObject("WScript.Network") • Next, you add the printer and define the drivers used WshNetwork.AddWindowsPrinterConnection "\\PS01.domain.net\printer", "HP LaserJet 1200 Series PCL" • You can then set the printer as a default printer WshNetwork.SetDefaultPrinter "\\PS01.domain.net\printer"
VBScript: Remove Network Drives In a logon script, it is important to unmap all network drives prior to mapping the new ones. Reason being, a mapped network drive over a WAN link slows down the user computer. 'Dim wshNetwork Set wshNetwork = CreateObject("Wscript.network") Dim bForce, bUpdateProfile bForce = True bUpdateProfile = True On Error Resume Next ' Prevents the user from seeing an error if drive doesn't exist Do this code for every letter in the alphabet above FWshNetwork.RemoveNetworkDrive "Z:", bForce, bUpdateProfile Disable the error handlers current positionOn Error GoTo 0 'resets error handling
VBScript: Map Network Drives • Mapping network drives is a lot like in a batch file, but different language • wshNetwork.MapNetworkDrive [driveletter:], "\\compname\share", [pers T or F] • Example: Set wshNetwork = CreateObject("Wscript.Network") wshNetwork.MapNetworkDrive"I:", "\\FS01.domain.net\Share", true
VBScript: Registry Entries • For some programs, specific registry values are defined that can be changed by VBScript. For example, we can update the Wave server, username and password with the appropriate values for a site using this method so the user doesn't have to know it • Set the code variables strComputer= "." ConstHKEY_CURRENT_USER = &H80000001 Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\default:StdRegProv") • Set Registry Values • Create the key path • oReg.CreateKeyHKEY_CURRENT_USER,RegRunValPathoReg.CreateKey HKEY_CURRENT_USER, "Software/Yoursite" • Create the key and set the value • oReg.SetStringValueHKEY_CURRENT_USER,RegRunValPath,RegRunValName,strValueoReg.SetStringValue HKEY_CURRENT_USER, "Software/Yoursite", "Key Name", "Cat“ • Get Registry Values • oReg.GetStringValueHKEY_CURRENT_USER,RegRunValPath,RegRunValName,strValue oReg.GetStringValueHKEY_CURRENT_USER,"Software/Yoursite", "Key Name", strValue wscript.echostrValue • Value in this code of strValue is "Cat"
VBScript: Copy Objects Some programs use INI files to store information usually stored in the registry. Others have their own proprietary databases. You can use a script to copy these files from a network share to a persons computerConstOverwriteExisting = TRUE Set objFSO = CreateObject("Scripting.FileSystemObject") objFSO.CopyFile "\\fs01.domain.net\hidden$\your.ini", "C:\Program" & _ " Files\YourProg\your.ini", OverwriteExisting 'the & _ allows you to break a long line of code and continue it on to a new line strictly to make it look better in the text file
VBScript: Add a favorite • You can also add a favorite URL to the favorite menu in IE ConstADMINISTRATIVE_TOOLS = 6 Set objShell = CreateObject("Shell.Application") Set objFolder = objShell.Namespace(ADMINISTRATIVE_TOOLS) Set objFolderItem = objFolder.Self Set objShell = WScript.CreateObject("WScript.Shell") strDesktopFld= objFolderItem.Path Set objURLShortcut = objShell.CreateShortcut(strDesktopFld & "\W3 Schools.url") objURLShortcut.TargetPath="http://www.w3schools.com" objURLShortcut.Save
Vbscript: User input • Prompting a user with a yes or no question • First declare a constant or variable with a window titleconst POPUP_TITLE="Your Printers and Drives" • Setup a new variable to define a shell object (helps to simplify the code)Set objshell = wscript.createobject("wscript.shell") • Create new variable that receives user input from the yes/no popup that pops up iRetVal= objshell.popup("Would you like to setup your computer to the default" & _ " settings for Your Site?", 10, POPUP_TITLE, vbquestion + vbyesno) ' at the end of a line of code, the & _ will allow you to continue the line of code on the next line. This is done mainly for the appearance of the code and make it easier to read for the author
VBScript: User Input • You can also get a text from the user placed in a variable • Variable = InputName("your Text", "Box Title", “Default”, Vert Pos, Horz Pos) strUserInput= inputbox("Please enter your name:", "Name","Your name here", 300, 400) Wscript.echostrUserInput
VBScript: Odds and ends Run a program or other fileSet objShell= CreateObject("Wscript.Shell") objShell.run "C:\yourprog.exe" Sets then retrieves info in ADUC (for a computer) Set objSysinfo = CreateObject("ADSystemInfo") Set objComputer = GetObject("LDAP://" & objsysinfo.computername) objComputer.put "Description", "Your Desc" objComputer.Setinfo wscript.echoobjComputer.get("Description")
VBScript: Odds and ends • Set Homepage (Internet Explorer) Const HKEY_CURRENT_USER = &H80000001 strComputer= "." Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ strComputer& "\root\default:StdRegProv") strKeyPath= "Software\Microsoft\Internet Explorer\Main" oReg.CreateKeyHKEY_CURRENT_USER,strKeyPath strValueName= "Start Page" strValue= "http://blogs.technet.com/b/heyscriptingguy/" oReg.SetStringValueHKEY_CURRENT_USER,strKeyPath,strValueName,strValue
VBScript: Odds and ends • Retrieve system information Computer Distinguished name Set objSysinfo = CreateObject("ADSystemInfo") wscript.echoobjsysinfo.computername Model Number strComputer= "." Set objWMIService = GetObject(_ "winmgmts:\\" & strComputer & "\root\cimv2") set colitems=objWMIservice.Execquery("Select * from win32_Computersystem",,48) for each objItem in colItems ModelNum = objitem.model wscript.echoModelNum Next
VBScript: Odds and ends • Serial Number strComputer = "." Set objWMIService = GetObject(_ "winmgmts:\\" & strComputer & "\root\cimv2") set colitems=objWMIservice.Execquery("Select * from win32_BIOS",,48) for each objItem in colItems SerialNum = trim(objitem.serialnumber) wscript.echoSerialNum Next
VBScript: Putting it all together ' This is an example of a simple VBS Script 'Prompt user to run the script of not const POPUP_TITLE="Your Printers and Drives" Set objshell = wscript.createobject("wscript.shell") iRetVal = objshell.popup("Would you like to setup your " & _ "computer to the default settings for Your Site?", _ ,POPUP_TITLE,vbquestion + vbyesno) If iRetVal = vbNo THEN wscript.quit if iRetVal = vbYes Then ' Delete all network printers Set objNetwork = CreateObject ("Wscript.network") Set objPrinter = objNetwork.enumprinterconnections Printerflag=false For I = 0 to objPrinter.count -1 Step 2 if left(objPrinter.item(i+1), 2) ="\\" Then on error resume next objNetwork.RemovePrinterConnectionobjPrinter.item(i+1) on error Goto 0 End if Next 'Map a printer Set WshNetwork = CreateObject("WScript.Network") WshNetwork.AddWindowsPrinterConnection "\\PS01.domain.net\printer", "HP LaserJet 4350 PCL 6" WshNetwork.SetDefaultPrinter "\\PS01.domain.net\printer" 'Map Network Drive Set wshNetwork = CreateObject("Wscript.Network") wshNetwork.MapNetworkDrive "I:", "\\PS01.domain.net\share", true End if Wscript.echo "Your printer and drive is now mapped" Wscript.quit
PE: VBScript • Using the above script, put together a script that maps a printer and a drive. For extra credit, try to map drives and printers based on the DC (may have too google how to find the logon server in VBS)