Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- 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.
- Design a command that will retrieve the following information from one or more remote computers, using the indicated WMI classes and properties:
- Win32_ComputerSystem:
- o Workgroup
- o AdminPasswordStatus; display the numeric values of this property as text strings.
- For 1, display Disabled
- For 2, display Enabled
- For 3, display NA
- For 4, display Unknown
- o Model
- o Manufacturer
- From Win32_BIOS
- o SerialNumber
- From Win32_OperatingSystem
- o Version
- o ServicePackMajorVersion
- Your function’s output should also include each computer’s name.
- 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:
- • ComputerName
- • Workgroup
- • AdminPassword (for AdminPasswordStatus in Win32_ComputerSystem)
- • Model
- • Manufacturer
- • BIOSSerial (for SerialNumber in Win32_BIOS)
- • OSVersion (for Version in Win32_OperatingSystem)
- • SPVersion (for ServicePackMajorVersion in Win32_OperatingSystem)
- Again, you aren’t writing the script only outlining what you might do..
- #>
- Function Get-SystemInfo {
- param(
- #modified your $ComputerName variable's type casting to allow it to be an array. The difference is [string] versus [string[]]
- [string[]]$ComputerName = 'localhost'
- )
- begin{
- #making a new empty array named $comps to hold our objects
- $comps = @()
- $Error = 'C:\Scripts\Errors.txt'
- }
- process{
- #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)
- ForEach ($computer in $ComputerName){
- #here we use the Switch keyword/function to save space and make it easier to read
- $AdminPasswordStatus = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer | Select-Object -ExpandProperty AdminPasswordStatus
- switch ($AdminPasswordStatus)
- {
- 1 {$AdminPasswordStatus = "Disabled"}
- 2 {$AdminPasswordStatus = "Enabled"}
- 3 {$AdminPasswordStatus = "Not Available"}
- 4 {$AdminPasswordStatus = "Unknown"}
- default {"Possible Error"}
- }
- #storing these values in strings to cast them into a custom object
- $OS = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $computer
- $BIOS = Get-CimInstance -ClassName Win32_BIOS -ComputerName $computer
- #casting these into a custom object to then store...
- $obj = [pscustomObject]@{
- ComputerName = $computer
- AdminPasswordStatus = $AdminPasswordStatus
- OSVersion = $OS.Version
- OSPatch = $OS.ServicePackMajorVersion
- Bios = $BIOS.SerialNumber
- }
- #end of the ForEach loop, we now store this $obj in $comps
- $comps += $obj
- }
- }
- end{
- #end of the function, we now display all of the contents of $comps
- Write-host ("`$comps contains " + ($comps.Count) + " Objects")
- $comps | Format-Table
- }
- }
- #Some Examples
- Get-SystemInfo
- #$comps contains 1 Objects
- #
- #ComputerName AdminPasswordStatus OSVersion OSPatch Bios
- #------------ ------------------- --------- ------- ----
- #localhost Not Available 6.3.9600 0 System Serial Number
- $computername = "localhost","localhost"
- Get-SystemInfo -ComputerName $computername
- #$comps contains 2 Objects
- #ComputerName AdminPasswordStatus OSVersion OSPatch Bios
- #------------ ------------------- --------- ------- ----
- #localhost Not Available 6.3.9600 0 System Serial Number
- #localhost Not Available 6.3.9600 0 System Serial Number
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement