Gwith

Lab 6

May 13th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. <#
  2. In this lab, we aren’t going to have you write any actual scripts or functions. Instead, we want you to think about the design aspect, something many people overlook. Let’s say you’ve been asked to develop the following PowerShell tools. Even though the tool will be running from PowerShell 3.0, you don’t have to assume that any remote computer is running PowerShell 3.0. Assume at least PowerShell v2.
  3.  
  4. Design a command that will retrieve the following information from one or more remote computers, using the indicated WMI classes and properties:
  5.  Win32_ComputerSystem:
  6. o Workgroup
  7. o AdminPasswordStatus; display the numeric values of this property as text strings.
  8.  For 1, display Disabled
  9.  For 2, display Enabled
  10.  For 3, display NA
  11.  For 4, display Unknown
  12. o Model
  13. o Manufacturer
  14.  From Win32_BIOS
  15. o SerialNumber
  16.  From Win32_OperatingSystem
  17. o Version
  18. o ServicePackMajorVersion
  19.  
  20. Your function’s output should also include each computer’s name.
  21. Ensure that your function’s design includes a way to log errors to a text file, allowing the user to specify an error file name but defaulting to C:\Errors.txt. Also plan ahead to create a custom view so that your function always outputs a table, using the following column headers:
  22. • ComputerName
  23. • Workgroup
  24. • AdminPassword (for AdminPasswordStatus in Win32_ComputerSystem)
  25. • Model
  26. • Manufacturer
  27. • BIOSSerial (for SerialNumber in Win32_BIOS)
  28. • OSVersion (for Version in Win32_OperatingSystem)
  29. • SPVersion (for ServicePackMajorVersion in Win32_OperatingSystem)
  30. Again, you aren’t writing the script only outlining what you might do..
  31. #>
  32.  
  33. Function Test {
  34. #Allow Verbose
  35. [cmdletbinding()]
  36. param(
  37. #Make computer name parameter mandatory
  38. [parameter(Mandatory=$True,
  39. Valuefrompipeline=$True)]
  40. #allow user to use hostname alias
  41. [Alias('hostname')]
  42. [string[]]$computername,
  43. #log errors
  44. [switch]$ErrorLog
  45. )
  46.  
  47. BEGIN{
  48. #If errorlog siwtch is on start
  49. if ($ErrorLog) {
  50. #use [cmdletbinding()] to display write-verbose
  51. Write-Verbose "Error Log On"
  52. $i = 0
  53. #Do this loop if Errorlog switch is on
  54. Do {
  55. $LogFile = "names-$i.txt"
  56. $i++
  57. #Check for path if exist create new file
  58. } While (Test-Path $LogFile)
  59. Write-Verbose "LogFile name is $LogFile"
  60. } else {
  61. #If Errorlog switch is off
  62. Write-Verbose "Error Log Off"
  63. }
  64.  
  65. }
  66. PROCESS{
  67. Write-Verbose "Connecting to $Computer"
  68. #log file to $logfile
  69. if ($ErrorLog) {
  70. $Computer | Out-File $LogFile -Append
  71. }
  72. #set specific output to this variable
  73. $adminpasswordstatus = Get-WmiObject -class Win32_ComputerSystem | Select-Object -ExpandProperty AdminPasswordStatus
  74. switch ($adminpasswordstatus)
  75. {
  76. 1 {'Disabled'}
  77. 2 {'Enabled'}
  78. 3 {'NA'}
  79. 4 {'Unknown'}
  80. default {'Try Again'}
  81. }
  82. #for each computer grab this information
  83. foreach ($comps in $computername) {
  84. $cs = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computername | Select-Object Domain
  85. $os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computername | Select-Object Version,ServicePackMajorVersion
  86. $bios = Get-WmiObject -Class Win32_Bios -ComputerName $computername | Select-Object SerialNumber
  87. $properties = @{'ComputerName' =$computername;
  88. 'Domain' =$cs.Domain;
  89. 'AdminPassword'=$AdminPasswordStatus;
  90. 'OSVersion' =$os.version;
  91. 'ServicePack' =$os.servicepackmajorversion;
  92. 'SerialNumber' =$bios.SerialNumber}
  93. #take objects from hash table and convert to object
  94. $obj = New-Object -TypeName PSObject -Property $properties
  95.  
  96. }
  97. }
  98.  
  99. END{
  100. #write object named $obj
  101. Write-Host "$obj"
  102.  
  103. }
  104.  
  105. }
  106.  
  107. test -computername localhost -errorlog -verbose
Advertisement
Add Comment
Please, Sign In to add comment