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 Test {
- #Allow Verbose
- [cmdletbinding()]
- param(
- #Make computer name parameter mandatory
- [parameter(Mandatory=$True,
- Valuefrompipeline=$True)]
- #allow user to use hostname alias
- [Alias('hostname')]
- [string[]]$computername,
- #log errors
- [switch]$ErrorLog
- )
- BEGIN{
- #If errorlog siwtch is on start
- if ($ErrorLog) {
- #use [cmdletbinding()] to display write-verbose
- Write-Verbose "Error Log On"
- $i = 0
- #Do this loop if Errorlog switch is on
- Do {
- $LogFile = "names-$i.txt"
- $i++
- #Check for path if exist create new file
- } While (Test-Path $LogFile)
- Write-Verbose "LogFile name is $LogFile"
- } else {
- #If Errorlog switch is off
- Write-Verbose "Error Log Off"
- }
- }
- PROCESS{
- Write-Verbose "Connecting to $Computer"
- #log file to $logfile
- if ($ErrorLog) {
- $Computer | Out-File $LogFile -Append
- }
- #set specific output to this variable
- $adminpasswordstatus = Get-WmiObject -class Win32_ComputerSystem | Select-Object -ExpandProperty AdminPasswordStatus
- switch ($adminpasswordstatus)
- {
- 1 {'Disabled'}
- 2 {'Enabled'}
- 3 {'NA'}
- 4 {'Unknown'}
- default {'Try Again'}
- }
- #for each computer grab this information
- foreach ($comps in $computername) {
- $cs = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computername | Select-Object Domain
- $os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computername | Select-Object Version,ServicePackMajorVersion
- $bios = Get-WmiObject -Class Win32_Bios -ComputerName $computername | Select-Object SerialNumber
- $properties = @{'ComputerName' =$computername;
- 'Domain' =$cs.Domain;
- 'AdminPassword'=$AdminPasswordStatus;
- 'OSVersion' =$os.version;
- 'ServicePack' =$os.servicepackmajorversion;
- 'SerialNumber' =$bios.SerialNumber}
- #take objects from hash table and convert to object
- $obj = New-Object -TypeName PSObject -Property $properties
- }
- }
- END{
- #write object named $obj
- Write-Host "$obj"
- }
- }
- test -computername localhost -errorlog -verbose
Advertisement
Add Comment
Please, Sign In to add comment