Advertisement
1RedOne

Chapter 6, modified approach

May 2nd, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 Get-SystemInfo {
  34.     param(
  35.         #modified your $ComputerName variable's type casting to allow it to be an array.  The difference is [string] versus [string[]]
  36.         [string[]]$ComputerName = 'localhost'
  37.     )
  38.  
  39.    
  40.     begin{
  41.         #making a new empty array named $comps to hold our objects
  42.         $comps = @()
  43.         $Error = 'C:\Scripts\Errors.txt'
  44.     }
  45.  
  46.     process{
  47.     #the process group runs once for every object in the pipeline, but in our example we're still using a for loop to enumerate the objects within $ComputerName (to run this bit on every object)
  48.     ForEach ($computer in $ComputerName){  
  49.        
  50.         #here we use the Switch keyword/function to save space and make it easier to read
  51.         $AdminPasswordStatus = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer | Select-Object -ExpandProperty AdminPasswordStatus
  52.         switch ($AdminPasswordStatus)
  53.             {
  54.                 1 {$AdminPasswordStatus = "Disabled"}
  55.                 2 {$AdminPasswordStatus = "Enabled"}
  56.                 3 {$AdminPasswordStatus = "Not Available"}
  57.                 4 {$AdminPasswordStatus = "Unknown"}
  58.                 default {"Possible Error"}
  59.             }
  60.         #storing these values in strings to cast them into a custom object
  61.         $OS   = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $computer
  62.         $BIOS = Get-CimInstance -ClassName Win32_BIOS -ComputerName $computer
  63.  
  64.         #casting these into a custom object to then store...
  65.         $obj = [pscustomObject]@{
  66.             ComputerName = $computer
  67.             AdminPasswordStatus = $AdminPasswordStatus
  68.             OSVersion = $OS.Version
  69.             OSPatch   = $OS.ServicePackMajorVersion
  70.             Bios      = $BIOS.SerialNumber
  71.             }
  72.  
  73.         #end of the ForEach loop, we now store this $obj in $comps
  74.         $comps += $obj
  75.         }
  76.     }        
  77.        
  78.     end{
  79.     #end of the function, we now display all of the contents of $comps
  80.     Write-host ("`$comps contains " + ($comps.Count) + " Objects")
  81.     $comps | Format-Table
  82.     }
  83. }
  84.  
  85. #Some Examples
  86.  
  87. Get-SystemInfo
  88.  
  89. #$comps contains 1 Objects
  90. #
  91. #ComputerName                               AdminPasswordStatus                        OSVersion                                                                     OSPatch Bios                                      
  92. #------------                               -------------------                        ---------                                                                     ------- ----                                      
  93. #localhost                                  Not Available                              6.3.9600                                                                            0 System Serial Number
  94.  
  95. $computername = "localhost","localhost"
  96. Get-SystemInfo -ComputerName $computername
  97. #$comps contains 2 Objects
  98.  
  99. #ComputerName                               AdminPasswordStatus                        OSVersion                                                                     OSPatch Bios                                      
  100. #------------                               -------------------                        ---------                                                                     ------- ----                                      
  101. #localhost                                  Not Available                              6.3.9600                                                                            0 System Serial Number                      
  102. #localhost                                  Not Available                              6.3.9600                                                                            0 System Serial Number
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement