1 / 24

WINDOWS SCRIPTING

WINDOWS SCRIPTING. CSE 535 – Operating Systems by AKHAN AKBULUT. Windows Scripting is easy.

whittakerc
Download Presentation

WINDOWS SCRIPTING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. WINDOWS SCRIPTING CSE 535 – Operating Systemsby AKHAN AKBULUT

  2. Windows Scripting is easy • Windows Script Host, or WSH for hereon in, can be thought of in the same way as old DOS batch files, because it allows you to directly script and automate parts of the core operating system using plain text files. By using these scripts you can automate anything from the Windows registry, through to files and shortcuts, through to network printers and shares, and because of the extensible nature of the language there is practically no limit to what it can do.

  3. Windows Scripting is easy WSH is really made up of three main components: • The core host that ties all the pieces together • The scripting engines • provides the scripting languages: • VBScript, • JScript • PerlScript • The Standard Scripting Hosts to execute the scripts: • WSCRIPT.EXE • a Windows GUI version • CSCRIPT.EXE • a text mode command-line version

  4. Windows Scripting is easy The VBScript and Jscript languages are both supported. To run a Windows Script use cscript command: cscriptExample1.vbs or cscriptExample1.js

  5. Hello World! • Let’s get started by writing a basic script. • Windows scripts are simply text files, • You cancreate windows scripts with a plain text editor • like Windows Notepad • open up a new Notepad window • type the following text: • WScript.Echo "Hello World!"

  6. Hello World! • Save the file into a directory • as “Example1.vbs", • double-click to execute it, • you should get a popup message box • saying "Hello World!" now click OK to close the box. • Congratulations! That’s your first script, easy wasn’t it! • We simply used the WScript.Echo method • to display the words "Hello World!" to the user. • Note: • The ".VBS" extension tells the scripting host • it’s dealing with a VBScript file, • the ".JS" extension represents a JScript file.

  7. Wscript Object • The WScript object • provides a wide range of capabilities to WSH scripts • regardless of the language in which that script is written. • As such, the WScript object • Ensures a WSH-compliant language • will always be able to carry out • binding to COM objects • and parsing command-line arguments.

  8. Wscript Object

  9. WshController Object • The WshController object • allows you to create a controller script  • run worker scripts against remote computers. • The controller script • initiates, • monitors, • and, if necessary, terminates the worker script. • The worker script, meanwhile, • simply carries out the administrative task • for example, adding a printer connection • or mapping a network drive.

  10. WshController Object

  11. WshNetwork Object • The WshNetwork object • exposes the network functionalities of Microsoft Windows, • making it easy to manage drives and printers on the network. • TheWshNetwork object • is not instantiated automatically upon script execution, • it must be instantiated explicitly • using CreateObject before it can be used.  • Through the WshNetwork object • you can query some of the network related information • about a local computer, • connect to drives and printers on the network, • map and unmap network drives, • set default printer, • disconnect from drives and printers on the network. 

  12. WshNetwork Object

  13. WshNetwork Object / Example • Example2.vbs Set WshNetwork = WScript.CreateObject("WScript.Network") WScript.Echo "Domain = " & WshNetwork.UserDomain WScript.Echo "Computer Name = " & WshNetwork.ComputerName WScript.Echo "User Name = " & WshNetwork.UserName • Example2.js var WshNetwork = Script.CreateObject("WScript.Network"); WScript.Echo("Domain = " + WshNetwork.UserDomain); WScript.Echo("Computer Name = " + WshNetwork.ComputerName); WScript.Echo("User Name = " + WshNetwork.UserName);

  14. WshShell Object • The WshShell object • gives your scripts the ability to work with the Windows shell. • Your scripts can use the WshShell object • to perform a number of system administration tasks, • running programs, • reading from and writing to the registry, • and creating shortcuts

  15. WshShell Object

  16. Interacting with Users • Example3.vbs 'Define the variable Dim strName 'Get the users name strName = InputBox("Enter your name:") 'Show the user the name entered WScript.echo "Hello " & strName

  17. Math and Arithmetic support • Example4.vbs Option Explicit 'Define the variables Dim Birthday, AgeInSeconds 'Get the users DOB Birthday = InputBox("Please enter your date of birth:") 'Calculate the difference using DateDiff AgeInSeconds = DateDiff("s", Birthday, Now) 'Display the results WScript.echo "You are " & AgeInSeconds & " seconds old."

  18. FileList.vbs Option Explicit Function Main Dim objFSO, objFile Dim strFolder, colFiles, strMessage ' Create the FileSystemObject object Set objFSO = WScript.CreateObject("Scripting.FileSystemObject”) ' Configure FSO to specific folder Set strFolder = objFSO.GetFolder("C:\\WINDOWS”) ' Get a collection of the files contained in that folder Set colFiles = strFolder.Files ' Iterate through the Collection object and add each file to the temp variable strMessage = "" For Each objFile in colFiles ' strMessage = strMessage & objFile.Name & VbCrLf ' Only File Name strMessage = strMessage & objFile.Path & VbCrLf ' Path and File Name Next ' Print all files in directory WScript.EchostrMessage WScript.Quit(0) End Function Main

  19. WhoAmI.vbs Function Main Dim objNetwork Dim strCurrentUserName, strCurrentDomainName, strFullUserName Dim strComputerName Set objNetwork = WScript.CreateObject("WScript.Network") strCurrentUserName = objNetwork.UserName strCurrentDomainName = objNetwork.UserDomain strComputerName = objNetwork.ComputerName strFullUserName = strCurrentDomainName & "\\" & strCurrentUserName WScript.Echo "Current logged in user: " & strFullUserName & " on " & strComputerName & VbCrLf End Function Main WScript.Quit(0)

  20. ShutDownPC.vbs Dim objShell, strComputer, strInput Dim strShutdown Do strComputer = (InputBox(" ComputerName to shutdown", "Computer Name"))  If strComputer <> "" Then strInput = True End if  Loop until strInput = True strShutdown = "shutdown -s -t 0 -f -m \\" & strComputer     set objShell = CreateObject("WScript.Shell") objShell.RunstrShutdown Wscript.Quit

  21. Services.vbs Option ExplicitDim objWMIService, objItem, objService, strServiceListDim colListOfServices, strComputer, strService'On Error Resume Next' ---------------------------------------------------------' Pure WMI commandsstrComputer = "."Set objWMIService = GetObject("winmgmts:" _& "{impersonationLevel=impersonate}!\\" _& strComputer & "\root\cimv2")Set colListOfServices = objWMIService.ExecQuery _("Select * from Win32_Service ")' WMI and VBScript loopFor Each objService in colListOfServicesIf UCase(Left(objService.name,1)) >"N" thenstrServiceList = strServiceList & vbCr & _objService.nameEnd ifNext WScript.EchostrServiceList

  22. RestartService.vbs ( Alerter Sevice ) Option ExplicitDim objWMIService, objItem, objServiceDim colListOfServices, strComputer, strService, intSleepstrComputer = "."intSleep = 15000WScript.Echo " Click OK, then wait " & intSleep & " milliseconds" 'On Error Resume Next' NB strService is case sensitive.strService = " 'Alerter' "Set objWMIService = GetObject("winmgmts:" _& "{impersonationLevel=impersonate}!\\" _& strComputer & "\root\cimv2")Set colListOfServices = objWMIService.ExecQuery _("Select * from Win32_Service Where Name ="_& strService & " ")For Each objService in colListOfServicesobjService.StopService()WSCript.SleepintSleepobjService.StartService()Next WScript.Echo "Your "& strService & " service has Started"  WScript.Quit

  23. ReadReg.vbs Option ExplicitDim objShellDim strCachedLogon, strShell, strPwdWarn, strWinLogonstrPwdWarn = "passwordexpirywarning"strShell = "Shell"strCachedLogon ="cachedlogonscount"strWinLogon = "HKLM\SOFTWARE\Microsoft\"_& "Windows NT\currentVersion\Winlogon\"Set objShell = CreateObject("WScript.Shell")strShell = objShell.RegRead(strWinLogon & strShell) strPwdWarn = objShell.RegRead(strWinLogon & strPwdWarn) strCachedLogon = objShell.RegRead(strWinLogon & strCachedLogon) Wscript.Echo "Shell Program " & vbTab & strShell & vbCr _& "Password Expires " & vbTab & strPwdWarn & vbCr _& "Cached Logons " & vbTab & strCachedLogon WScript.Quit

  24. END OF CHAPTER

More Related