· 8 years ago · Mar 10, 2018, 08:08 PM
1
2Lab 4 – Scripting Log Files
3
4This lab will teach you two basic methods of scripting a log file. A log file is a text file that records network events, documenting what was done and when (and sometimes by whom). Knowing how to script a log file is a basic skill all administrators. Log files provide accurate and timely reporting to management, and record network and system performance and security at a point in time. They are also legally required under federal legislation, when changes are made to customer information. In this lab, we will use Common Information Module (CIM) and Windows Management Instrumentation (WMI) to access disk, bios and installed applications on a localhost and output that information in a formated manner to create a report on the fly.
5WMI is Microsoft's implementation of Web-Based Enterprise Management (WBEM); this is an industry standard to communicate with hardware and software installed on enterprise workstations and servers. WMI is very large and is organized into namespaces which are like virtual databases. The default namespace for all Windows machines is Root/CIMV2 (Common Information Model); because WMI namespaces are hierarchical, searches are always recursive and start at the root. To view all of the CIMV2 classes
6Type: Get-CIMClass * -root/CIMV2 (PowerShell 4.0)
7Type: Get-WMIObject –list * (PowerShell 2.0)
8The CIM is a repository storing all of the classes used to access resources. You can think of CIM as a virtual hard drive and the classes are like folders on the drive. Each folder is a tool to access hardware or software. CIM server providers (actually Windows DLLs) are used to access the information in a standardized format. Since you will need to review the classes related to BIOS, Disk, and Applications, we will write a short script to help in the search. To view CIM classes and search for a particular class was easy using the new Get-CIMclass cmdlet. WMI does not have this capability, so we wil write a short script that will allow you to search the WMI classes.
9Exercise 1 - Searching WMI Classes and Setting up the Script Template
10
111. Open a new tab in PowerShell ISE and copy and paste the following script. Name the file _ LearnName Lab4_WMI_Search.ps1
12$SearchItem = "$args"
13$colItems = get-wmiobject -list | where-object {$_.name -match "$SearchItem"}
14$colItems | Select-Object -Property "Name"
15$class = Read-Host "Enter a class name" | ForEach-Object -process {$_}
16Get-WMIObject $class | Format-List
17This program uses the $args array to enter an argument on the command line specifying search criteria such as “diskâ€, “bios†or “productâ€. The argument is saved in the $SearchItem variable. Then a WMI object is created and piped to the where-object cmdlet which filters class names displaying only those that match the command line argument, the result is stored in the variable $colItems which uses the Select-Object cmdlet to display the property class names. When your script is working correctly you should get output like the screen shot above.
18
192. Run the script and review the class names. Look for classes that begin with “Win32_â€. Enter the class name at the prompt. The Get-WMIObject then displays the properties of the class in a list format. The properties we will be using in the Exercise 2 are listed below.
203. Now use the Get-CIMclass to find the same information:
21Type: Get-CIMclass *disk* -repeat with bios and product
224. Try and find all classes that have a property “versionâ€. Take a screen shot of your output and name the file LearnName_Lab4_version.jpeg
23Exercise 2: Accessing hardware using CIM
24
25The properties will be searching for are listed below. For the local hard drive you want to report the model and size, for the installed Bios, you want the manufacturer and Bios version and for the installed products you want the description name of the software and the version installed.
26• Disk
27o Model
28o Size • BIOS
29o Manufacturer
30o Version • Product
31o Description
32o Version
33Table 1: WMI Properties for Disk Bios and Product
341. Copy and paste the script template into a new tab. Name the file LearnName_Lab4_LogFile.ps1. Each comment field describes the code to be written. Write it in the space below the comment field. The next section specify the code to write.
35#Create Variables for Log Path Directory, Log Path File, Disk Information, Bios #Information #and Installed Products (Applications) Information and Today's Date. Use CIM cmdlets
36########################################################################
37
38#Create a Message “Gathering Machine Informationon <computername>. Please wait…â€
39#########################################################################
40
41#Create Log file and output Log File Title with Computer Name and Date of Report
42########################################################################
43
44 #Create a Disk Drive Summary Header, Get Disk Information and Output to log file
45########################################################################
46
47 #Create a Bios Summary Header, Get Bios Information and Output to log file
48########################################################################
49
50#Create an Installed Software Summary Header, Get Product Information and Output to #log file
51#######################################################################
52
53#Display a Message Log File Created.
54########################################################################
55
562. Copy the variable names below and past into the LearnName_Lab4_LogFile.ps1 program. Complete the empty variables. For date use a cmdlet and use CIM for the disk bios and app variables. Use Get-Help to search for the correct syntax.
57Note: Creating two variables, one for the path to the log directory and one for the path to the log file is standard practice when working with log files. These two variables simplify the writing of the paths at the end of each line, and allow the script to be used to work in different directories by changing the first two values.
58 #Create Variables for Log Path Directory, Log Path File, Disk Information, Bios Information and
59 #Installed Products (Applications) Information and Today's Date
60 $LogPath = "$HOME\Documents\Win213\Lab4" (make sure this folder exists)
61 $LogFilePath = "$HOME\Documents\Win213\Lab4\LearnName_Lab4_LogFile.log"
62 $Date =
63 $DiskInfo = (use CIM cmdlet and pipe to Select-object.)
64 $BiosInfo =
65 $AppInfo =
662. For the message use an environment variable to give the computername and write a message to the console to tell the user to wait.
673. Copy the code below to complete the next section. Replace computer name with the environment variable and date with the date variable.
68 #Create Log file and output Log File Title with Computer Name and Date of Report
69Write-Output "Summary Information for computer <COMPUTERNAME> on <Date>" | Out-file -FilePath "$LogFilePath"
70Write-Output "================================================== " | Out-file -FilePath "$LogFilePath" -append
71Notice that the Write-Output is creating the Log File Header and is piped to Out-File to create the log file. Then all other content is appended to the Log file using the –append parameter at the end of the pipe. We need the –encoding parameter to ensure that the format is compatible.
724. Copy and paste the code to the next section. Notice we are passing four new line characters “`n†to the file to improve its appearance. The Out-file cmdlet with the append parameter will append to the next line by default.
73 #Create a Disk Drive Summary Header, Get Disk Information and Output to log file
74 Write-Output "`n`n`n`n"| Out-File "$LogFilePath" -append
75 Write-Output "Disk Drive Summary" |Out-file "$LogFilePath" -append
76 Write-Output "============================================" | Out-file "$LogFilePath" -append
77Repeat same code for other sections, changing the header and WMI properties as required.
785. Display a message to the console “Log file <insert logfile path> is createdâ€.
79Note: The Product section will take a 30-60 seconds to complete.
80When running correctly, your Log File output should be formatted as below. Note: you will need to increase the length of the “==========†which was truncated to allow the code to be on one line. Run the program and experiment with the formatting. Name the file LearnName_lab4_Logfile.jpeg
81
82Figure 3: Lab5 Log File Output
83Exercise 3: Challenge – Scripting a Web Page
84
85The above procedure is the new PowerShell way of creating a log file, using the pipeline and the cmdlet Out-File. The Add-Content cmdlet could also have been used. Add-Content will automatically create the file if it does not exist, and it appends by default. The second common method to create a log file is to use file redirection. To demonstrate this technique we will script a web page and display it in the browser. We will create a PowerShell script called LearnName_Lab4_WWW which will create a web page and then call the browser to display the page. (Do not use a Here-string)
861. Copy and paste the following code into PowerShell ISE. Name the file LearnName_Lab6_WWW.ps1
87$WebFilePath = "$HOME\Documents\Win213\wk6\Lab6_WebPage.html"
88<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
89<HTML>
90<Head><style>BODY{background-color:peachpuff;}</style></HEAD>
91<BODY>
92<H1>PowerShell Scripted Web Page</H1>
93</BODY>
94</HTML>
95Invoke-Expression $WebFilePath
962. Modify the HTML code STARTING WITH THE “<!DOC …. to </HTML>â€. You want the output to appear as written above to display the web page. (Hint: remember how PowerShell quote marks work) Use only file redirection operators “ >†and “>>â€
97When your script is running correctly, you should have output like the screenshot below:
98
99Figure 4: Output of Lab6_Webpage and Lab6_WWW.ps1
100
101
102For grading:
103• LearnName Lab4_WMI_Search.ps1
104• LearnName_Lab4_version.jpeg
105• LearnName_Lab4_LogFile jpeg
106• LearnName_lab4_Logfile.ps1
107• LearnName_lab4_WebPage.htm
108• Learnname_Lab4_ WWW.ps1
109Zip all files together and submit to the link in the Graded Work folder on My Seneca.