Gwith

test

May 15th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 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 Lab6 {
  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. #set specific output to this variable
  69. $adminpasswordstatus = Get-WmiObject -class Win32_ComputerSystem -ComputerName $computername -EA Stop | Select-Object -ExpandProperty AdminPasswordStatus
  70. #for each computer grab this information
  71. foreach ($comps in $computername) {
  72. $cs = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computername -EA Stop | Select-Object Domain
  73. $os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computername -EA Stop | Select-Object Version,ServicePackMajorVersion
  74. $bios = Get-WmiObject -Class Win32_Bios -ComputerName $computername -EA Stop | Select-Object SerialNumber
  75. }
  76.  
  77. switch ($adminpasswordstatus)
  78. {
  79. 1 {$AdminPasswordStatus = 'Disabled'}
  80. 2 {$AdminPasswordStatus = 'Enabled'}
  81. 3 {$AdminPasswordStatus = 'NA'}
  82. 4 {$AdminPasswordStatus = 'Unknown'}
  83. default {'Try Again'}
  84. }
  85.  
  86. $Computer | Out-File $LogFile -Append
  87.  
  88. $properties = @{'ComputerName' =$computername;
  89. 'Domain' =$cs.Domain;
  90. 'AdminPassword'=$AdminPasswordStatus;
  91. 'OSVersion' =$os.version;
  92. 'ServicePack' =$os.servicepackmajorversion;
  93. 'SerialNumber' =$bios.SerialNumber}
  94. #take objects from hash table and convert to object
  95. $obj = New-Object -TypeName PSObject -Property $properties
  96.  
  97. }
  98. }
  99.  
  100. END{
  101. #write object named $obj
  102. $obj | ft
  103.  
  104. }
  105. }
  106.  
  107.  
  108.  
  109.  
  110. Lab6 -computername localhost -errorlog -verbose
Advertisement
Add Comment
Please, Sign In to add comment